2

Knowing that we have EncryptSymmetric and DecryptSymmetric in Ampscript, both with the ability of referencing password, vector and salt, from Key Management - is there anything similar in SSJS?

If a value is stored, encrypted, in a data extension, I need to fetch it and decrypt it with SSJS and not only with Ampscript. I am yet to find a suitable solution here, despite decent research.

1
  • 1
    To my knowledge, SFMC SSJS would require you to build your own functions to handle this - which can be a significant task, depending on what you want to do. I usually just recommend using the var functions in SSJS to pass to/from AMPscript to utilize those functions instead. – Gortonington Oct 17 '19 at 20:01
4

Here's what I use in SSJS. You'd need to use your own password, vector and salt, obviously.

Encrypt

  function encryptString(decStr) {

     Variable.SetValue("decStr", decStr);

     </script>
      %%[

      var @encStr
      set @encStr = ""

      if not empty(@decStr) then
        set @encStr = Base64Encode(EncryptSymmetric(@decStr,"aes", @null, "ABCD", @null, "0000072000000000", @null, "00000000000000000000001230000000"))
      endif

     ]%%
    <script runat="server">

    return Variable.GetValue("@encStr");

  }

Decrypt

  function decryptString(encStr) {

     Variable.SetValue("encStr", encStr);

     </script>
      %%[

      var @decStr
      set @decStr = ""

      if not empty(@encStr) then
        set @decStr = DecryptSymmetric(Base64Decode(@encStr),"aes", @null, "ABCD", @null, "0000072000000000", @null, "00000000000000000000001230000000")
      endif

     ]%%
    <script runat="server">

    return Variable.GetValue("@decStr");

  }
2

Adam's answer will work for CloudPages, but not for a Script Activity in an Automation, as the following error will be returned when validating the Script Activity:

AMP Script is not currently supported in SSJS Activities.

To workaround this, you need to disguise your AMPscript further. Refer to the following code examples below if you would like to use EncryptSymmetric or DecryptSymmetric in a Script Activity.

Note, rather than hardcoding the password, IV and Salt values, I've created keys in SFMC Key Management and pass the external key values for each key, which are prefixed with INTEGRATION_ below.

Encrypt

  function encryptString(decStr) {

    var encodedStr = Platform.Function.Base64Encode(decStr); // encoding as argument may include ' or " chars

     var amp = '%' + "%[ Output(Concat(EncryptSymmetric('" + encodedStr + "', 'AES',";
         amp += "'INTEGRATION_KEY', @null,";
         amp += "'INTEGRATION_SALT', @null,";
         amp += "'INTEGRATION_IV', @null)))";
         amp += ']%' + '%';

     var val = Platform.Function.TreatAsContent(amp);
       return val;

  }

Decrypt

  function decryptString(encStr) {

     var amp = '%' + "%[ Output(Concat(Base64Decode(DecryptSymmetric('" + encStr + "', 'AES',";
         amp += "'INTEGRATION_KEY', @null,";
         amp += "'INTEGRATION_SALT', @null,";
         amp += "'INTEGRATION_IV', @null))))";
         amp += ']%' + '%';

     var val = Platform.Function.TreatAsContent(amp);
       return val;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.