使用OpenSSL加密的Node.js中的解密文件

时间:2017-06-11 09:08:42

标签: node.js encryption openssl aes

我正在使用以下命令加密openssl中的视频文件

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

并使用以下代码解密文件,但我不断解密错误我做错了什么?

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

这是我得到的错误

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)

我应该能够在openssl中加密视频并同时在节点和java中解密

1 个答案:

答案 0 :(得分:0)

正如 jww 所说,openssl -k-K 不一样。

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

知道keyiv

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