Basic Encryption/Crypto in SuiteScript 2.x   

in , , April 8th, 2024
a golden padlock sitting on top of a keyboard

NetSuite Encryption

Encryption is very useful for certain applications, but the NetSuite documentation can be confusing. This article will walk through using the crypto and encode module to easily encrypt and decrypt data.

function encrypt(stringToEncrypt) {
    // Create the secret key
    const skey = crypto.createSecretKey({
        secret: "custsecret_encrypt_entity_secret",
        encoding: encode.Encoding.UTF_8,
    });


    // Set up cipher
    const cipher = crypto.createCipher({
        algorithm: crypto.EncryptionAlg.AES,
        key: skey,
    });
    
    //Add string as input
    cipher.update({ input: id, });


    //Encrpyt string
    const cipherout = cipher.final({
        outputEncoding: encode.Encoding.HEX,
    });
    
    //Pass "cipherout" data to decrypt string
    decrypt(cipherout);
}
    
function decrypt(cipherout) {
    // Create the secret key
    var skey = crypto.createSecretKey({
        secret: "custsecret_encrypt_entity_secret,
        encoding: encode.Encoding.UTF_8,
    });


    // Set up cipher
    var decipher = crypto.createDecipher({
        algorithm: crypto.EncryptionAlg.AES,
        key: skey,
        iv: cipherout.iv,
    });


    //Add cipher text
    decipher.update({
        input: cipherout.ciphertext,
        inputEncoding: encode.Encoding.HEX,
    });


    //Decrypt
    var decipherout = decipher.final({
        outputEncoding: encode.Encoding.UTF_8,
    });
}

The two functions above show how you can encrypt a string, and then decrypt the output to retrieve the string again.

Clarifications Around NetSuite Data Encryption

  1. Make sure your encryption algorithm used is the same when setting up the cipher.
  2. The secret key:
  • Make sure you encode the secret key using the same encoding method when encrypting and decrypting.
  • The secret key “string” is the id of a secret set up under Setup > Company > API Secrets. Make sure it is allowed on scripts.

Author: Sam Gagliardi


Got stuck on a step in 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 a 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