node crypto.createDecipher错误的最终块长度错误

时间:2015-09-11 17:13:54

标签: javascript node.js encryption aes

我正在使用以下代码首先进行加密然后解密。

var fs = require('fs');
var crypto = require('crypto');

var ws = fs.createWriteStream('message.txt');
var rs = fs.createReadStream('message.txt');

var passPhrase = 'password';

process.stdin.pipe(crypto.createCipher('aes256', passPhrase)).pipe(ws);

ws.on('finish', function() {
    rs.pipe(crypto.createDecipher("aes256", passPhrase)).pipe(process.stdout); 
});

我将代码称为:

echo "randomstring" | node crypt.js

但我一直收到以下错误:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
    at Error (native)
    at Decipher.Cipher._flush (crypto.js:177:28)
    at Decipher.<anonymous> (_stream_transform.js:130:12)
    at Decipher.g (events.js:199:16)
    at Decipher.emit (events.js:104:17)
    at prefinish (_stream_writable.js:474:12)
    at finishMaybe (_stream_writable.js:482:7)
    at endWritable (_stream_writable.js:493:3)
    at Decipher.Writable.end (_stream_writable.js:459:5)
    at ReadStream.onend (_stream_readable.js:505:10)

我只是解密createCipher的输出,为什么我仍然得到错误的最终块长度错误?

message.txt的内容在我的终端上出现乱码

kartik@kartik-lappy:~/stream-adventure$ cat message.txt 
�[��Z�*3��Kv�

kartik@kartik-lappy:~/stream-adventure$ wc -c message.txt 
16 message.txt

1 个答案:

答案 0 :(得分:1)

您应该在message.txt具有一些内容之后声明readStream:

var fs = require('fs');
var crypto = require('crypto');

var ws = fs.createWriteStream('message.txt');

var passPhrase = 'password';

process.stdin.pipe(crypto.createCipher('aes256', passPhrase)).pipe(ws);

ws.on('finish', function() {
    var rs = fs.createReadStream('message.txt');
    rs.pipe(crypto.createDecipher("aes256", passPhrase)).pipe(process.stdout); 
});