I'm using the google sheet webscript to manipulate spreadsheet data and I want to use the following function to encrypt certain cells:
var encrypted = CryptoJS.AES.encrypt("message", "Secret key");
There's an option to add libraries to the Google Sheet webscript but I have no idea how to get a library installed. According to the Google documentation you need the project key/script ID in order to use the library, but I have not been able to find this kind of information.
Can someone assist in how to actually import this CryptoJS library to use in the webscript.


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?
In this pattern, crypto-js is used.
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.
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);
}
myFunction(), the encrypted and decrypted values are returned.In this pattern, "cCryptoGS" which is a Google Apps Script library is used.
The project key for installing the library is 1IEkpeS8hsMSVLRdCMprij996zG6ek9UvGwcCJao_hlDMlgbWWvJpONrs.
Please install the GAS library using this project key.
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);
}
myFunction, the encrypted and decrypted values are returned.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With