Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

encryption - Decrypt file in Node.js encrypted using OpenSSL

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:javaindex.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...