1

I'm using the following command to encrypt a video file in openssl

openssl aes-256-cbc -nosalt -a -in movie.mp4 -out movie.enc -k skdjfsldkfjsldkjfsldkf

And using the following code to decrypt the file but I keep getting bad decrypt error what am I doing wrong?

var crypto = require('crypto');

var fs = require('fs');
cipher_name   = 'aes-256-cbc';
password      = 'skdjfsldkfjsldkjfsldkf';
decoder       = crypto.createDecipher( cipher_name, password );
text_crypt    = fs.readFileSync( 'movie.enc' );
chunks        = [];
chunks.push(decoder.update( text_crypt, 'binary' ));
chunks.push(decoder.final( 'binary' ));
fs.writeFileSync( 'nodemovie.mp4',chunks.join('','binary') );

This is the error I'm getting

Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
    at Decipher.final (crypto.js:160:26)
    at Object.<anonymous> (F:\java\index.js:12:21)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

I should be able to encrypt video in openssl and decrypt in node and java at the same time

jww
  • 97,681
  • 90
  • 411
  • 885
  • why you tagged as java? – Ori Marko Jun 11 '17 at 09:11
  • because i want to decrypt the same file in java ... I've mentioned the same in my qn too – Sushil Sudhakaran Jun 11 '17 at 09:16
  • @SushilSudhakaran - Pick your poison - decrypt in Node.js, or decrypt in Java. Since the title indicates Node.js, how about if the answers focus on Node.js. When you are ready, ask another question specific to Java. – jww Jun 11 '17 at 10:01
  • `-k skdjfsldkfjsldkjfsldkf` is different than `-K skdjfsldkfjsldkjfsldkf`. Also see [EVP_BytesToKey](https://wiki.openssl.org/index.php/Manual:EVP_BytesToKey(3)) man page, and [EVP_DecryptFinal_ex:bad decrypt when using Node.js](https://stackoverflow.com/q/37997354/608639). – jww Jun 11 '17 at 10:05
  • 1
    Here are some similar questions and answers: [Decrypting AES256 with node.js returns wrong final block length](https://stackoverflow.com/q/21292142/608639), [Nodejs decrypt using crypto error wrong final block length](https://stackoverflow.com/q/23111388/608639), [Getting error wrong final block length while decrypting AES256](https://stackoverflow.com/q/32038267/608639), [How to decipher string in node js which is encrypted in crypto.js](https://stackoverflow.com/q/28359128/608639), [What's wrong with nodejs crypto decipher?](https://stackoverflow.com/q/12219499/608639) – jww Jun 11 '17 at 10:16
  • This depends on your OpenSSL version. Newer OpenSSL versions use EVP_BytesToKey with SHA-256 instead of MD5 like node.js does – Artjom B. Jun 11 '17 at 11:12
  • Possible duplicate of [Decrypting AES256 with node.js returns wrong final block length](https://stackoverflow.com/questions/21292142/decrypting-aes256-with-node-js-returns-wrong-final-block-length) – giodamelio Jul 03 '19 at 17:40

1 Answers1

0

as jww said, openssl -k and -K are not the same.

openssl aes-256-cbc -nosalt -in movie.mp4 -out movie.enc -k skdjfsldkfjsldkjfsldkf -p
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.
key=ED67064595132E4F1154C557A3C103999CF719B66855B6563C6B35D346CDE40E
iv =4FB8711CAD9659FF2F1DCE33A7D3E0B7

knowing key and iv

var crypto = require('crypto');

var fs = require('fs');
cipher_name   = 'aes-256-cbc';
//password      = 'skdjfsldkfjsldkjfsldkf';
//decoder       = crypto.createDecipher( cipher_name, password );
iv            = Buffer.from('4FB8711CAD9659FF2F1DCE33A7D3E0B7','hex');
key           = Buffer.from('ED67064595132E4F1154C557A3C103999CF719B66855B6563C6B35D346CDE40E','hex');

decoder       = crypto.createDecipheriv( cipher_name, key, iv );
text_crypt    = fs.readFileSync( 'movie.enc' );
chunks        = [];
chunks.push(decoder.update( text_crypt, 'binary' ));
chunks.push(decoder.final( 'binary' ));
fs.writeFileSync( 'nodemovie.mp4',chunks.join('','binary') );