AES Encryption and Decryption Using the N/crypto

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/crypto', 'N/encode', 'N/log'], function (crypto, encode, log) {

  function onRequest(context) {
    let secretKey = crypto.createSecretKey({
      guid: 'f3e6c67a-1a47-4fd2-8b09-cdbcf36a3ef7',
      encoding: encode.Encoding.UTF_8
    });

    let textToEncrypt = 'SensitiveData123';

    // Encrypt Data
    let encrypted = crypto.encrypt({
      algorithm: crypto.EncryptionAlg.AES,
      key: secretKey,
      input: textToEncrypt,
      inputEncoding: encode.Encoding.UTF_8,
      outputEncoding: encode.Encoding.BASE_64
    });

    log.debug('Encrypted:', encrypted);

    // Decrypt Data
    let decrypted = crypto.decrypt({
      algorithm: crypto.EncryptionAlg.AES,
      key: secretKey,
      input: encrypted,
      inputEncoding: encode.Encoding.BASE_64,
      outputEncoding: encode.Encoding.UTF_8
    });

    log.debug('Decrypted:', decrypted);

    context.response.write('Encrypted: ' + encrypted + '<br>Decrypted: ' + decrypted);
  }

  return {
    onRequest: onRequest
  };
});

How It Works

  • The crypto.encrypt() function encrypts the input text using AES encryption.
  • The crypto.decrypt() function reverses the process and retrieves the original data.
  • AES encryption ensures secure data storage and transmission.

Leave a comment

Your email address will not be published. Required fields are marked *