如何处理Node.jsencryptionstream中的块长度

我想encryption一个inputstream,并通过TCP发送给另一台服务器。 到现在为止还挺好。 一切运行顺利,直到连接closures。 几乎在任何情况下,192位所需的块大小都不符合,脚本崩溃的wrong final block length ,尽pipe我打开了自动填充。

看起来像自动填充只有在使用传统界面时才起作用。 我在这里做错了什么?

 var net = require("net") , crypto = require("crypto"); var credentials = { algorithm: "aes192", password: "password" } , decipher = crypto.createDecipher(credentials.algorithm, credentials.password) , cipher = crypto.createCipher(credentials.algorithm, credentials.password); decipher.setAutoPadding(true); cipher.setAutoPadding(true); net.createServer(function(socket) { socket.pipe(socket); }).listen(2000); var socket = net.connect(2000); socket.pipe(decipher).pipe(process.stdout); process.stdin.pipe(cipher).pipe(socket); socket.write("Too short."); socket.end(); 

在我理想的Node.js世界中,当源streamclosures时,(De-)密码stream将自动填充最后一个块。 我认为这是一个devise缺陷。

除了解决问题之外 ,我怎样才能规避这种行为呢? 我必须在Socket和(De-)密码stream之间放置一个字节计数器吗?

你已经设置你的pipe道是这样的:

 stdin | cipher | socket (loopback) | decipher | stdout 

但是你可以通过直接写入套接字来绕过encryption,像这样使用它们:

 socket (loopback) | decipher | stdout 

试试这个代码:

 var net = require("net") , crypto = require("crypto"); var credentials = { algorithm: "aes192", password: "password" } , decipher = crypto.createDecipher(credentials.algorithm, credentials.password) , cipher = crypto.createCipher(credentials.algorithm, credentials.password); decipher.setAutoPadding(false); //set to false to keep the padding cipher.setAutoPadding(true); //Loopback server = net.createServer(function(socket) { socket.pipe(socket); }) server.listen(2000); var socket = net.connect(2000); //cipher to the loopback socket, to decipher and stdout cipher.pipe(socket).pipe(decipher).pipe(process.stdout); //write some data cipher.write("Too short."); //Clean exit cipher.end(); server.unref(); 

为了演示的目的,我从Decryptor对象中删除了自动填充,所以你可以看到剩余的填充。 在xxd(在命令行中,不在节点中)pipe道程序给了我这个输出:

 $ nodejs so.js | xxd 0000000: 546f 6f20 7368 6f72 742e 0606 0606 0606 Too short....... 

0x06重复6次。