Node-Red and the Crypto-JS library

Just a quick install instructions to use the Crypto-Js library on Node-Red, so that we can use encryption/decryption functions and hash functions like AES and SHA256 on workflows.

Installation and configuration:
First move to the Node-Red install directory. The right directory can vary from platform to platform.
In my case I’m running Node-Red on my Odroid C1+ controlled by the PM2 process manager:

cd .node-red
npm install crypto-js

Edit the settings.js file to add the global defined library:

    functionGlobalContext: {
        // os:require('os'),
        // octalbonescript:require('octalbonescript'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})

I already have the node-gcm module to use it for Android notifications from Node-Red workflows.

Restart Node-Red. In my case as: pm2 restart node-red

How to use:
We can now use the Crypto-js functions on our workflow functions like this:

// Import the global Crypto-js module defined on Node-Red settings.js file
var cryptojs =;

// Move data to base64
var bdata = new Buffer(msg.payload).toString('base64');

// Encrypt the data with the device key.
var ciphertext = cryptojs.AES.encrypt(bdata, "2B7E151628AED2A6ABF7158809CF4F3C" );

// The payload is now the encrypted data
msg.payload = ciphertext.toString();

return msg;

That’s it. We can now access the provided functions by the Crypto-js library from our Node-Red workflows.

11 thoughts on “Node-Red and the Crypto-JS library

  1. I’m gettings an error on node-red that “Cannot read property ‘AES’ of undefined”
    I have installed Crypto-js and the folder appears in .node-red/node_modules.
    I have tried global.get as well
    Please help

    1. This means that the library is not correctly instantiated, hence it is undefined.

      In the above code, the library must be defined on the configuration file and the same name must be used to access, in your case, the AES function.

  2. Hi, PrimalCortex, I got same issue :

    in setting.js/ functionGlobalContext: {, i add :
    CryptoJS: require (‘crypto-js’)

    in NodeRed Function i use :
    var CryptoJS = global.get(‘crypto-js’);
    var iv = CryptoJS.enc.Hex.parse( plain_iv );

    The error is : TypeError: Cannot read property ‘enc’ of undefined

  3. Thanks for reply
    1/ i copy and paste all your Node-red configuration (with the comma at the end
    functionGlobalContext: {


    2/ i change my flow code with :
    var cryptojs = global.get(‘crypto-js’);

    var iv = cryptojs.enc.Hex.parse( plain_iv );

    3/ The error is again : TypeError: Cannot read property ‘enc’ of undefined

    4/ note : in the cryptojs-js node_module i saw :
    * var wordArray = CryptoJS.enc.Hex.parse(hexString);

    That’s why i’ve put “CryptoJS” instead of “cryptojs”

    it’s mysterious !!?? (i use Node_Red v0.19.5)

    1. Did you installed the Crypto-js module globally or into the node-modeles directory of Node-Red?

      Can you check if the Crypto-Js module is installed on the node-modules of node-red?

  4. this is the crypto-js folder : C:\Users\christian\.node-red\node_modules\crypto-js (i’m using node-red in win10. I try in Raspberry PI with same error)
    And this is the settings.js folder : C:\Users\christian\.node-red

    I install it with this :
    cd C:\Users\christian\.node-red
    npm install crypto-js
    => result :
    npm WARN node-red-project@0.0.1 No repository field.
    npm WARN node-red-project@0.0.1 No license field.
    + crypto-js@3.1.9-1
    updated 1 package and audited 618 packages in 3.343s
    found 0 vulnerabilities

  5. it’s good I got there:
    1 / install crypto-js with `npm install crypto-js`
    2 / added it to the settings.js file
      `functionGlobalContext: {
             cryptojs require ( ‘cryptojs’)
         } `

    3/and used `var CryptoJS = global.get (‘cryptojs’);` in the code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.