Convert node.js into node-red

Hi All,

I have a node.js code which need to use in Node-red as below:

Example of DES decryption in javascript/node.js:

function decrypt(payload, key) {
        	var payloadstr = payload.toString();
        	var keyWords = cryptoJs.enc.Utf8.parse(key);
        	var ivWords = cryptoJs.enc.Utf8.parse(key);
   		var reb64 = cryptoJs.enc.Hex.parse(payloadstr);
   		var bytes = reb64.toString(cryptoJs.enc.Base64);
   		var decrypt = cryptoJs.DES.decrypt(bytes, keyWords, { iv: ivWords, padding: cryptoJs.pad.Pkcs7, mode: cryptoJs.mode.CBC});
   		return decrypt.toString(cryptoJs.enc.Utf8);
}

Which function i need to use after convert?

Thank you

I don't understand the question, please explain in more detail. Have you already implemented that function in a Function node or is that what you are asking how to do?

You can either implement the function in settings.js as a global context variable or you can (probably) implement it in a function node.

Either way, you will need to install whatever npm package that provides cryptoJs.

i have install the crytojs. then i use decrypt nodes and insert the secret key and select DES. but still unable to debug it.

You could simply use an existing node: node-red-contrib-crypto-js-dynamic (node) - Node-RED

However, if it is your goal to learn how to implement somthing like this in node-red function then simply add a reference to crypto in the function setup like this: Use any npm module in Node-RED • FlowFuse

1 Like