AES (Rijndael) Encryption using Stanford Javascript Crypto Library SJCL for short (Works in scoped applications only)
Knowledge on how encryption works is a prerequisite.
Its hard to find a JS library that runs perfectly with ServiceNow JavaSript Engine which supports ECMAScript5.
Libraries like CryptoJS by Google wouldn't work right away without modifications.
Yet lot of Encryption libraries are built on native functions like Uint8Array which are not supported by ES5.
SJCL is developer by Standford, works perfectly to perform AES 256-bit encryption.
By Default CBC mode is disabled from the original source hosted by Standford on Github.
Find the modified SJCL library with CBC mode enabled from my GitHub. Here is the link sjcl-CBC-enabled.
If you are using the modified SJCL from my Github, following are some of the easy steps to get it working.
- Download sjcl.zip, extract the file to any folder.
- created a Script Include in a scoped application and removed the auto generated script.
- Name the Script Include with any name, paste the contents from the extracted file to script field and save it.
- From server script access it with 'sjcl' available as global variable in server script.
- find out how to use it from the official documentation page
Below is a sample code to perform AES 256-bit encryption with IV in CBC mode
`// tested in Jakarta, Kingston, London versions.// you can choose any format like sjcl.codec.base32 or sjcl.codec.hex or sjcl.codec.bytes key = sjcl.codec.base64.toBits(" Your Secret Key in Base64 Format "); iv = sjcl.codec.base64.toBits(" Initialization Vector in Base64 Format ");
message = sjcl.codec.utf8String.toBits("Message to be encrypted");
aes = new sjcl.cipher.aes(key);
// pass aes, message, iv as parameters. choose any mode, CBC is used in the below line of code
var cipher = sjcl.mode["cbc"].encrypt(aes, message, iv);// cipher contains encrypted information in bits. covert to any format with below codevar cipherHex = sjcl.codec.hex.fromBits(chipher) // returns encrypted information in hexadecimal string format.
var cipherBase64 = sjcl.codec.base64.fromBits(cipher) // returns
`
encrypted information in Base64 string format.
https://www.servicenow.com/community/developer-articles/aes-rijndael-encryption-using-stanford-javascript-crypto-library/ta-p/2319881