Sampath Dimmal1
Giga Contributor

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.

  1. Download sjcl.zip, extract the file to any folder.
  2. created a Script Include in a scoped application and removed the auto generated script.
  3. Name the Script Include with any name, paste the contents from the extracted file to script field and save it.
  4. From server script access it with 'sjcl' available as global variable in server script.
  5. 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 code
var 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.



Comments
lior grinberg2
Tera Expert

HI,

I'm getting an error message 

"sjcl" is not defined
Can you help?
Thanks,
Lior
Sampath Dimmal1
Giga Contributor

Hi Lior, can you provide some more information on this error?

did you follow the steps to create and paste code in script include.

peacefully
Kilo Explorer

Hi, 

Got the same error - "sjcl" is not defined. I have followed the instructions (Create Script include - remove content and paste SJCL.js)

I have tried to create a new application and repeated the steps in there. Tried to call the "sjcl" and got "not defined". It was also not possible to call it with a syntax like new ScriptIncludeName(). sjcl .....

I think something essential is missing in the instructions. Can you help out ?

Bratati Mondal
Tera Explorer

hi, i am getting the same error. did you figure out a solution?

Kamalini
Tera Contributor

Name the script include as sjcl.

Sampath Dimmal1
Giga Contributor

This was long time ago, let me check this again and get back in few days

Version history
Last update:
‎02-14-2019 09:33 AM
Updated by: