James Sefton James Sefton - 1 year ago 121
Node.js Question

node.js crypto streams not giving output

I am struggling to understand what I am doing wrong here.

I am trying to write something to encrypt data coming in and out of a TCP socket but I am struggling to get any output from the crypto cipher stream.

Example: (Stripped down to keep it as simple as possible)

crypto = require('crypto');

data.copy(cipherKey,0,5,133); //buffer filled with 128 bytes of data for cipher password. (source of data stripped out to keep this simple)
//test value (in hex bytes) of cipherKey = abcecfa8c752b72d784db7ddbc30db7da2b22ba7eab9000f2615e26d55a6b27f1ad97239151e1b6398afa055e347571aa018332bef041d032b73e5c23e48407d1e288f8c8edcadd6a70f6f1031cf4778b037b8beaed4863d5ac2e6f4cf454a87ece5051a49d11a2b9a89bf955cbf54a22f05405c43f20f4d4bf26bd2e928189d

var credentials = { algorithm: "aes128", password: cipherKey.toString() };

var decipher = crypto.createDecipher(credentials.algorithm, credentials.password);
var cipher = crypto.createCipher(credentials.algorithm, credentials.password);



I did manage to get some output (although very little) by repeating the cipher.write("Hello!"); line about 10 times which makes me wonder if you have to input a minimum amount of data before it 'triggers' some output.

If this is the problem then I need to find a way around it since most of the packets that need to go through this will be between 4 and 32 bytes. (although some could be large too, but most not)

Any suggestions?

(I am new to node so I apologise in advance for any stupidity!)

Answer Source

Yes, there is buffering at play here. Just let your crypto stream know there won't be any more data, and it will flush itself.

In your example, just add

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download