Crypto Tools: AES
This flow implements the capabilities for encrypting and decrypting a user provided JSON object with a secret key using the Advanced Encryption Standard.
[{"id":"fda40ddd2560b63d","type":"subflow","name":"AES","info":"This subflow implements the capabilities for encrypting and decrypting a user provided JSON object with a secret key using the *Advanced Encryption Standard*. The subflow supports the following AES cipher modes:\n- CBC\n- CFB\n- CTR\n- OFB\n\n## *Encryption Mode*\nThis subflow allows for the encryption of plaintext message to encrypted ciphertexts using a user provided key.\n\n### Inputs\n>\n> **msg.*function***: \"encrypt\"\n>\n> **msg.*mode***: Block cipher mode. \n>\n> **payload.*text***: Plaintext to encrypt.\n>\n> **payload.*key***: Encryption key. \n>\n\n### Outputs\n>\n> **payload.*ciphertext***: The encrypted text.\n>\n\n## *Decryption Mode*\nThis subflow allows for the decryption of ciphertext message to a decrypted plaintext using a user provided key.\n\n### Inputs\n>\n> **msg.*function***: \"decrypt\"\n>\n> **msg.*mode***: Block cipher mode. \n>\n> **payload.*ciphertext***: Plaintext to decrypt.\n>\n> **payload.*key***: Decryption key. \n>\n\n### Outputs\n>\n> **payload.*plaintext***: The decrypted text.\n>","category":"CryptoRed","in":[{"x":60,"y":80,"wires":[{"id":"e623e864e070b538"}]}],"out":[{"x":680,"y":80,"wires":[{"id":"4a0794ecf0809e38","port":0},{"id":"047435f1358b1bd1","port":0}]}],"env":[{"name":"function","type":"str","value":"encrypt","ui":{"icon":"font-awesome/fa-gears","label":{"en-US":"Function"},"type":"select","opts":{"opts":[{"l":{"en-US":"Encrypt"},"v":"encrypt"},{"l":{"en-US":"Decrypt"},"v":"decrypt"}]}}},{"name":"mode","type":"str","value":"CBC","ui":{"icon":"font-awesome/fa-gear","label":{"en-US":"Mode"},"type":"select","opts":{"opts":[{"l":{"en-US":"Cipher Block Chaining"},"v":"CBC"},{"l":{"en-US":"Cipher FeedBack"},"v":"CFB"},{"l":{"en-US":"Output FeedBack"},"v":"OFB"},{"l":{"en-US":"Counter"},"v":"CTR"}]}}},{"name":"key","type":"str","value":"","ui":{"icon":"font-awesome/fa-key","label":{"en-US":"Key"},"type":"input","opts":{"types":["str"]}}}],"meta":{"module":"node-red-contrib-crypto-tools-aes","version":"0.0.1","author":"Thodoris Ioannidis <[email protected]>","desc":"AES encryption and decryption functionalities","keywords":"aes, block, cipher","license":"Apache-2.0"},"color":"#3FADB5","inputLabels":["plaintext"],"outputLabels":["ciphertext"],"icon":"font-awesome/fa-cubes"},{"id":"4a0794ecf0809e38","type":"function","z":"fda40ddd2560b63d","name":"ENCRYPTION","func":"\nfunction encryptCBC(object,key){\n return cryptoJs.AES.encrypt(JSON.stringify(object),key,{\n mode: cryptoJs.mode.CBC\n }).toString();\n}\n\nfunction encryptCFB(object,key){\n return cryptoJs.AES.encrypt(JSON.stringify(object),key,{\n mode: cryptoJs.mode.CFB\n }).toString();\n}\n\nfunction encryptCTR(object,key){\n return cryptoJs.AES.encrypt(JSON.stringify(object),key,{\n mode: cryptoJs.mode.CTR\n }).toString();\n}\n\nfunction encryptOFB(object,key){\n return cryptoJs.AES.encrypt(JSON.stringify(object),key,{\n mode: cryptoJs.mode.OFB\n }).toString();\n}\n\nmessage = msg.payload.plaintext;\nlet ciphertext, mode = msg.payload.mode || env.get('mode'),key = msg.payload.key || env.get('key');\n\nswitch(mode){\n case 'CBC':\n ciphertext = encryptCBC(message, key)\n break;\n case 'CFB':\n ciphertext = encryptCFB(message, key)\n break;\n case 'CTR':\n ciphertext = encryptCTR(message, key)\n break;\n case 'OFB':\n ciphertext = encryptOFB(message, key)\n break;\n}\ndelete msg.payload.plaintext\nmsg.payload.enc = {\n mode: mode,\n ciphertext: ciphertext\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"cryptoJs","module":"crypto-js"}],"x":420,"y":60,"wires":[[]]},{"id":"047435f1358b1bd1","type":"function","z":"fda40ddd2560b63d","name":"DECRYPTION","func":"function isJSON(text) {\n try {\n JSON.parse(text);\n return true;\n }catch (e){\n return false;\n }\n}\n\nfunction decryptCBC(cipher,key){\n bytes = cryptoJs.AES.decrypt(cipher,key,{\n mode: cryptoJs.mode.CBC \n });\n return bytes.toString(cryptoJs.enc.Utf8);\n}\nfunction decryptCFB(cipher,key){\n bytes = cryptoJs.AES.decrypt(cipher,key,{\n mode: cryptoJs.mode.CFB \n });\n return bytes.toString(cryptoJs.enc.Utf8);\n}\nfunction decryptCTR(cipher,key){\n bytes = cryptoJs.AES.decrypt(cipher,key,{\n mode: cryptoJs.mode.CTR \n });\n return bytes.toString(cryptoJs.enc.Utf8);\n}\nfunction decryptOFB(cipher,key){\n bytes = cryptoJs.AES.decrypt(cipher,key,{\n mode: cryptoJs.mode.OFB \n });\n return bytes.toString(cryptoJs.enc.Utf8);\n}\n\nmessage = msg.payload.ciphertext;\nlet plaintext, mode = msg.payload.mode || env.get('mode'), key = msg.payload.key || env.get('key')\n\nswitch(mode){\n case 'CBC':\n plaintext = decryptCBC(message, key)\n break;\n case 'CFB':\n plaintext = decryptCFB(message, key)\n break;\n case 'CTR':\n plaintext = decryptCTR(message, key)\n break;\n case 'OFB':\n plaintext = decryptOFB(message, key)\n break;\n}\ndelete msg.payload.ciphertext\nmsg.payload.dec = {\n mode: mode,\n plaintext: isJSON(plaintext) ? JSON.parse(plaintext) : plaintext\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"cryptoJs","module":"crypto-js"}],"x":420,"y":120,"wires":[[]]},{"id":"e623e864e070b538","type":"switch","z":"fda40ddd2560b63d","name":"FUNCTION","property":"function","propertyType":"env","rules":[{"t":"eq","v":"encrypt","vt":"str"},{"t":"eq","v":"decrypt","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":80,"wires":[["4a0794ecf0809e38"],["047435f1358b1bd1"]]}]