Issue and workaround:
Unfortunately, in the current stage, it seems that there are no built-in methods for directly achieving the AES encryption in Google Apps Script methods.
So in this case, how about the following workarounds?
Pattern 1:
In this pattern, crypto-js is used.
Usage:
1. Get crypto-js:
Please access to https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js. And copy and paste the script to the script editor.
2. Sample script:
After the copy and paste crypto-js, please copy and paste the following sample script.
function myFunction() {
var key = "sampleSecretKey";
var value = "sampleMessage";
var encryptedMessage = CryptoJS.AES.encrypt(value, key).toString();
var decryptedMessage = CryptoJS.AES.decrypt(encryptedMessage, key).toString(CryptoJS.enc.Utf8);
Logger.log(encryptedMessage);
Logger.log(decryptedMessage);
}
- When you run the function of
myFunction()
, the encrypted and decrypted values are returned.
Pattern 2: Updated on April 7, 2022.
In this pattern, "cCryptoGS" which is a Google Apps Script library is used.
Usage:
1. Install Google Apps Script library:
The project key for installing the library is 1IEkpeS8hsMSVLRdCMprij996zG6ek9UvGwcCJao_hlDMlgbWWvJpONrs
.
Please install the GAS library using this project key.
2. Sample script:
function myFunction() {
var key = "sampleSecretKey";
var value = "sampleMessage";
var cipher = new cCryptoGS.Cipher(key, 'aes');
var encryptedMessage = cipher.encrypt(value);
var decryptedMessage = cipher.decrypt(encryptedMessage);
Logger.log (encryptedMessage);
Logger.log (decryptedMessage);
}
- When you run the function of
myFunction
, the encrypted and decrypted values are returned.
References: