简体   繁体   中英

node crypto.createDecipher wrong final block length error

I'm using the following code to first encipher and then decipher.

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

I'm calling the code as:

echo "randomstring" | node crypt.js

But I keep getting the following error:

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)

I am just deciphering the output of createCipher, why am I still getting the wrong final block length error?

Contents of message.txt come out garbled on my terminal

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

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

You should declare readStream after message.txt has some content:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM