Learn How to Encrypt and Decrypt Text Strings with JavaScript

Advertisements

In JavaScript and Google Apps Script, a simple mechanism for encrypting and decrypting text strings and passwords is provided.

In one of my online projects, I need a basic and easy-to-implement encryption and decryption JavaScript library that can encode text and then decode the encoded string on the server.

The base64 encoding technique is the most straightforward choice, as it can be simply implemented in both native JavaScript and Google Apps Script.

Advertisements

Base64 Encoding with 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 Encoding with JavaScript

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);
};

The apparent disadvantage is that Base64 is encoding rather than encryption, thus Base64 texts are easily deciphered.

AES is a good choice if you want a strong encryption technique that requires a secret passcode to decipher the encrypted content. Longer strings are generated, but they cannot be decoded without the password.

AES Plain Text Encryption & Decryption

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 Encrypt & Decryption with Google Apps Script

If you want to utilise the AES encryption algorithm with Google Apps Script, include the CryptoJS package in your project using the Apps Script Starter, as demonstrated in this example.

Advertisements
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 });
};

Alternatively, the cCryptoGS library for Google Apps Script may be used to implement AES encryption in your projects and Suite add-ons. Go to Resources -> Libraries and add the MSJnPeIon6nzdLewGV60xWqi d-phDA33 library to your Google Script project to get started.

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);

Leave a Comment