node.js加密流不提供输出

时间:2020-11-24 18:26:59

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.

我正在尝试写一些东西来加密进出TCP套接字的数据,但我很难从加密密码流中获取任何输出。

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);

cipher.pipe(process.stdout);

cipher.write("Hello!");

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.

我通过重复cipher.write(“你好!”)来设法得到一些输出(尽管很少);大约10次行,这让我想知道你是否必须在“触发”某些输出之前输入最少量的数据。

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)

如果这是问题,那么我需要找到解决方法,因为大多数需要通过它的数据包将在4到32个字节之间。 (虽然有些可能也很大,但大多数都没有)

Any suggestions?

有什么建议么?

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

(我是节点新手所以我提前为任何愚蠢道歉!)

1 个解决方案

#1


3  

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

在您的示例中,只需添加

cipher.end()

#1


3  

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

在您的示例中,只需添加

cipher.end()