Main menu

Monthly Archives: July 2017

Need to send secure messages back and forth between Salesforce and Node.js? Whether it’s Heroku, AWS, or somewhere else, Node.js is an important platform to be able to communicate securely with Salesforce on.

Salesforce provides a fairly robust Crypto class. I like the AES256 with Managed initialization vector (aka IV) that Salesforce provides. The IV is 16 bytes of random/pseudorandom data that salts the encryption so the output is different every time even if the input is the same. This prevents hackers from being able to spot patterns in the encrypted data, and makes it very difficult to brute force. The IV gets prepended to the encrypted message so it is available on the other end when decrypting.

In Salesforce, Crypto.encryptWithManagedIV() and Crypto.decryptWithManagedIV() handle creating the IV, and prepending and parsing of the IV.

It’s pretty simple to build encryption and decryption using these methods on the Salesforce platform, see the example here.

But when it comes times to send and receive these encrypted messages on another platform, Node.js in this case, it becomes more difficult. Managing the IV must be handled differently. I found an article on how to do this with Java and a partial answer for Node.js. But I couldn’t seem to find a complete, working example for Salesforce <-> Node.js. So after I figured out how to make this work on my own, I thought I should blog about it to save others the trouble!

Saleforce

Here is example code of encrypt and decrypt, with some values from Node.js also included to be decrypted on Saleforce.

We can run this code in execute anonymous and we can see that we encrypt the same value twice, get two different encrypted values which decrypt to the same value. We also decrypt two different encrypted values which came from Node.js and they decrypt to the same value.

Node.js

Here is example code of encrypt and decrypt, with some values from Salesforce also included to be decrypted on Node.js.

We can run this code via command line and see that we encrypt the same value twice, get two different encrypted values which decrypt to the same value. We also decrypt two different encrypted values which came from Salesforce and they decrypt to the same value.

Now you can talk securely back and forth between Salesforce and Node.js to your heart’s content!