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

问题描述 投票:2回答:1

我使用以下代码首先加密然后解密。

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
javascript node.js encryption aes
1个回答
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); 
});
© www.soinside.com 2019 - 2024. All rights reserved.