Ascon.js (decryption for ascon)

Is there anyone here who knows how to decrypt ascon on Node-RED Docker Desktop? I tried looking in the node palette but there is no ascon.js. I am trying to decrypt ascon on the Node-RED side using Docker Desktop.


You can import libs in a function node - see: Use any npm module in Node-RED • FlowFuse

Demo:

Demo flow (use CTRL+I to import)

[{"id":"da6c913bd723cbac","type":"function","z":"a56117c9f31908fc","name":"Encrypt Payload","func":"\nconst { key, nonce, associatedData } = msg\nconst plaintext = msg.payload\n\n// Ensure all required fields are provided\nif (!plaintext || !key || !nonce || !associatedData) {\n    node.error('Missing required fields: payload, associatedData, ciphertext, key, nonce')\n    return null\n}\n\ntry {\n    const encrypted = JsAscon.encrypt(key, nonce, associatedData, plaintext)\n    msg.encrypted = encrypted\n    msg.payload = Buffer.from(encrypted).toString()\n\n    //  node.warn({key, nonce, associatedData, encrypted});\n    //  const decrypted = JsAscon.decrypt(key, nonce, associatedData, encrypted)\n    //  msg.decrypted = Buffer.from(decrypted).toString()\n    return msg\n} catch (error) {\n    node.error('Encryption failed: ' + error.message)\n    return null\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"JsAscon","module":"js-ascon"}],"x":1620,"y":120,"wires":[["5ce4d504f7baa593","3d896c5149b5cf9d"]]},{"id":"4a550a6826a7fa9c","type":"inject","z":"a56117c9f31908fc","name":"","props":[{"p":"key","v":"[32,117,219,188,84,65,45,141,195,177,115,35,86,210,103,131]","vt":"json"},{"p":"nonce","v":"[218,218,140,157,63,244,20,146,142,39,7,30,39,249,220,102]","vt":"json"},{"p":"associatedData","v":"Some data 😋 文 This data is not contained in the encrypt output. You must pass the same data to encrypt and decrypt in order to be able to decrypt the message.","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"plain text","payloadType":"str","x":1620,"y":60,"wires":[["da6c913bd723cbac"]]},{"id":"5ce4d504f7baa593","type":"debug","z":"a56117c9f31908fc","name":"encrypted","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":1660,"y":160,"wires":[]},{"id":"3d896c5149b5cf9d","type":"function","z":"a56117c9f31908fc","name":"Decrypt Payload","func":"\nconst { key, nonce, associatedData } = msg\n// const encryptedData = Buffer.from(msg.payload) // not working\nconst encryptedData = msg.encrypted\n\n// Ensure all required fields are provided\nif (!encryptedData || !key || !nonce) {\n    node.error('Missing required field: payload, key, nonce')\n    return null\n}\n\ntry {\n    const decrypted = JsAscon.decrypt(key, nonce, associatedData, encryptedData)\n    msg.payload = Buffer.from(decrypted).toString()\n    return msg\n} catch (error) {\n    node.error('Decryption failed: ' + error.message)\n    return null\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"JsAscon","module":"js-ascon"}],"x":1840,"y":120,"wires":[["3fb87d5488596fb0"]]},{"id":"3fb87d5488596fb0","type":"debug","z":"a56117c9f31908fc","name":"decrypted","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1920,"y":160,"wires":[]}]
2 Likes

What is the contents of your function node ? Perhaps you should import it first on the 'setup' tab.

1 Like

Thank you, you are right, but I still cannot fully decrypt it into purely JSON :slight_smile:
i believe this still refers to the function node.

Previously:

What is the contents of your function node ?

This is important - how do you instanciate the function.

Did you try the fully working self contained demo I posted above?

PROBLEM SOLVED GUYS! Thanks for help me.