【发布时间】:2016-02-08 17:41:54
【问题描述】:
我正在使用 Node.js 的加密模块和 AES-256-CBC 密码算法编写自己的安全类。
但是,当我尝试解密一个从长度超过 15 个字符的输入数据加密的加密字符串时,失败并出现以下错误:
crypto.js:153
var ret = this._handle.final();
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
我认为问题在于加密或 IV 生成,实际上,加密的十六进制字符串总是 32 个字符长。
让我们一起回顾一下代码:
var crypto = require("crypto"),
password = "mySecureKey",
salt = "mySaltKey";
//generate the IV
crypto.pbkdf2(password , salt, 4096, 8, "sha1", function(err, key) {
if (err) throw err;
var cipher_iv = new Buffer(key.toString('hex'));
//encrypt the string
var input = "helloPrettyWorld";
cipher = crypto.createCipheriv("aes-256-cbc", new Buffer(password), cipher_iv);
cipher.update(input, "utf8", "hex");
var encrypted = cipher.final("hex"); //i.e: input = "hello"; encrypted = "2300743605fbdaf0171052ccc6322e96"
//decrypt the string
cipher = crypto.createDecipheriv("aes-256-cbc", new Buffer(password), cipher_iv); /* THE ERROR IS THROWN HERE */
cipher.update(encrypted, "hex", "utf8")
var decrypted = cipher.final("utf8");
});
我尝试调整密码/盐的长度,甚至使用固定长度(32、16 等)的字符串,但没有解决问题。
回顾:
像“helloNiceWorld”(14 个字符)这样的输入数据将被完美地加密和解密,而像“helloPrettyWorld”(16 个字符)这样的输入数据则不会。
【问题讨论】:
标签: node.js encryption cryptography aes