Crypto decrypt error using piped streams
- Version: Node 4.4.2
- Platform: Windows 10 64bit
Hi,
I ran into this issue while testing out the crypto api. Testing out the AES enc with piped stream. I've used the example in the docs to no avail. The error on decryption was:
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Error (native)
at Decipher.Cipher._flush (crypto.js:128:28)
at Decipher.
I've created a gist with the example I used: https://gist.github.com/danielfoord/1e22424b42bdfb9eeda93a71880a0ead
Cheers
The issue is that you're not waiting for node to finish writing to test.encrypted before trying to decrypt it. Try this instead:
const crypto = require('crypto');
const fs = require('fs');
const cipher = crypto.createCipher('aes192', 'a password');
const encInput = fs.createReadStream('test.txt');
const encOutput = fs.createWriteStream('test.encrypted');
encInput.pipe(cipher).pipe(encOutput).on('close', function() {
const decipher = crypto.createDecipher('aes192', 'a password');
const decInput = fs.createReadStream('test.encrypted');
const decOutput = fs.createWriteStream('test.decrypted');
decInput.pipe(decipher).pipe(decOutput);
});
Forgive me, it's Friday !
Thanks :)