تقنية

كيفية تشفير وفك تشفير السلاسل النصية باستخدام جافا سكريبت


في أحد مشاريع الويب الخاصة بي، أحتاج إلى مكتبة جافا سكريبت للتشفير وفك التشفير بسيطة وسهلة التنفيذ يمكنها تشفير جزء من النص ثم فك تشفير السلسلة المشفرة على جانب الخادم.

الخيار الأسهل هو نظام التشفير base64 الذي يمكن تنفيذه بسهولة في كل من JavaScript الأصلي وGoogle Apps Script.

ترميز Base64 باستخدام Google Apps Script

const base64Encode = (text) => {
  const base64data = Utilities.base64Encode(text, Utilities.Charset.UTF_8);
  return base64data;
};

const base64Decode = (base64data) => {
  const decoded = Utilities.base64Decode(base64data, Utilities.Charset.UTF_8);
  const input = Utilities.newBlob(decoded).getDataAsString();
  return input;
};

ترميز Base64 باستخدام جافا سكريبت

const CryptoJS = require('crypto-js');

const encrypt = (text) => {
  return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
};

const decrypt = (data) => {
  return CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8);
};

الجانب السلبي الواضح هو أن Base64 يقوم بالتشفير (وليس التشفير) ويمكن فك تشفير سلاسل Base64 بسهولة.

إذا كنت تبحث عن خوارزمية تشفير آمنة تتطلب عبارة مرور سرية لفك تشفير النص المشفر، فانتقل إلى AES. يقوم بإنشاء سلاسل أطول ولكن لا يمكن فك تشفيرها بدون كلمة المرور.

AES تشفير النص العادي وفك التشفير

const CryptoJS = require('crypto-js');

const encryptWithAES = (text) => {
  const passphrase = '123';
  return CryptoJS.AES.encrypt(text, passphrase).toString();
};

const decryptWithAES = (ciphertext) => {
  const passphrase = '123';
  const bytes = CryptoJS.AES.decrypt(ciphertext, passphrase);
  const originalText = bytes.toString(CryptoJS.enc.Utf8);
  return originalText;
};

تشفير وفك تشفير AES باستخدام Google Apps Script

إذا كنت ترغب في استخدام AES خوارزمية التشفير باستخدام Google Apps Script، استخدم Apps Script Starter لاستيراد CryptoJS الحزمة في مشروعك كما هو موضح في هذا المثال.

import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';

const encryptWithAES = (text, passphrase) => {
  return AES.encrypt(text, passphrase).toString();
};

const decryptWithAES = (ciphertext, passphrase) => {
  const bytes = AES.decrypt(ciphertext, passphrase);
  const originalText = bytes.toString(Utf8);
  return originalText;
};

global.testAES = () => {
  const inputText = 'Hello World';
  const passphrase = 'My Secret Passphrase';

  Logger.log({ inputText });

  const encryptedText = encryptWithAES(inputText, passphrase);
  Logger.log({ encryptedText });

  const decryptedText = decryptWithAES(encryptedText, passphrase);
  Logger.log({ decryptedText });
};

وبدلاً من ذلك، بالنسبة لبرنامج Google Apps Script، يمكن أيضًا استخدام مكتبة cCryptoGS لتنفيذ تشفير AES في مشاريعك ووظائف Suite الإضافية. للبدء، انتقل إلى الموارد -> المكتبات وأضف الملف MSJnPeIon6nzdLewGV60xWqi_d-phDA33 مكتبة لمشروع Google Script الخاص بك.

const encryptedMessage = cCryptoGS.CryptoJS.AES.encrypt('message', 'passphrase').toString();
Logger.log(encryptedMessage);

const decryptedMessage = cCryptoGS.CryptoJS.AES.decrypt(encryptedMessage, 'passphrase').toString(CryptoJS.enc.Utf8);
Logger.log(decryptedMessage);



Source link


اكتشاف المزيد من مرابع التكنولوجيا

اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى

اكتشاف المزيد من مرابع التكنولوجيا

اشترك الآن للاستمرار في القراءة والحصول على حق الوصول إلى الأرشيف الكامل.

Continue reading