Simple SuiteScript 1.0 Encryption  

in , , June 27th, 2024
icon

What is Encryption?

Encryption is a powerful security measure that should be included in scripts, especially when dealing with sensitive data such as passwords. Unfortunately, the documentation for encryption in SuiteScript 1.0 (SS1) is limited. Therefore, I am providing the code below as a baseline for how to use it.

Note: The below method is known to function for strings. Be sure and stringify objects before attempting to encrypt them.

Encryption Code Example

//this function is not required, but is an example of generating a random hex key.
function generateHexKey(length) {
    var result = "";
    var characters = "0123456789abcdef"; // Hexadecimal characters
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}
var dataToBeEncoded = "Encrypt Me";
var secretKey = generateHexKey(32);
var encodedData = nlapiEncrypt(data, "aes", secretKey);
//NOTE: "data" must be stringified
var decryptedData = nlapiDecrypt(encodedData, "aes", secretKey);
//decryptedData = "Encrypt Me"

Methods for Loading Secret Keys 

There are a variety of ways to load in secret keys in SuiteScript 1.0. I recommend pursuing one of the following methods for loading your secret keys:

  1. Load the key from a custom record
  2. Generate the key on the fly and save it to a custom record temporarily

Be sure to choose the method that best suits your use case.

Author: Sam Gagliardi


Get stuck in a step during this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes there is a related feature to enable or field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!

 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart