使用node.js解密AES256会返回错误的最终块长度

Jus*_*oud 13 encryption openssl aes node.js

使用此Gist,我能够在Node.js 0.8.7中成功解密AES256.然后当我升级到Node.js 0.10.24时,我现在看到这个错误:

TypeError:错误:0606506D:数字包络例程:EVP_DecryptFinal_ex:
Decipheriv.Cipher.final中的最终块长度错误(crypto.js:292:27)

以下是Gist的解密代码(为方便起见,此处显示):

var crypto = require('crypto');

var AESCrypt = {};

AESCrypt.decrypt = function(cryptkey, iv, encryptdata) {
encryptdata = new Buffer(encryptdata, 'base64').toString('binary');

var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(encryptdata);

decoded += decipher.final();
return decoded;
}

AESCrypt.encrypt = function(cryptkey, iv, cleardata) {
var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
encryptdata = encipher.update(cleardata);

encryptdata += encipher.final();
encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64');
return encode_encryptdata;
}

var cryptkey = crypto.createHash('sha256').update('Nixnogen').digest(),
iv = 'a2xhcgAAAAAAAAAA',
buf = "Here is some data for the encrypt", // 32 chars
enc = AESCrypt.encrypt(cryptkey, iv, buf);
var dec = AESCrypt.decrypt(cryptkey, iv, enc);

console.warn("encrypt length: ", enc.length);
console.warn("encrypt in Base64:", enc);
console.warn("decrypt all: " + dec);
Run Code Online (Sandbox Code Playgroud)

Jus*_*oud 25

好的,所以在0.8到0.10的交换机中有一个Crypto的变化Crypto方法默认返回Buffer对象,而不是二进制编码的字符串

这意味着上面的代码需要指定编码.

这四行:

decoded = decipher.update(encryptdata);
decoded += decipher.final();
encryptdata = encipher.update(cleardata);
encryptdata += encipher.final();
Run Code Online (Sandbox Code Playgroud)

改为:

decoded = decipher.update(encryptdata, 'binary', 'utf8');
decoded += decipher.final('utf8');
encryptdata = encipher.update(cleardata, 'utf8', 'binary');
encryptdata += encipher.final('binary');
Run Code Online (Sandbox Code Playgroud)

这对我有用,但我对其他建议持开放态度.


log*_*yth 11

正如您的回答所述,除非您指定编码,否则这些函数现在可以与Buffers一起使用.也就是说,你最好binary完全避免编码字符串并将所有内容视为缓冲区,直到你严格需要字符串为止.这样,您还可以使用加密助手来处理非文本内容.

var crypto = require('crypto');

var AESCrypt = {};

AESCrypt.decrypt = function(cryptkey, iv, encryptdata) {
    var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv);
    return Buffer.concat([
        decipher.update(encryptdata),
        decipher.final()
    ]);
}

AESCrypt.encrypt = function(cryptkey, iv, cleardata) {
    var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv);
    return Buffer.concat([
        encipher.update(cleardata),
        encipher.final()
    ]);
}

var cryptkey = crypto.createHash('sha256').update('Nixnogen').digest(),
iv = new Buffer('a2xhcgAAAAAAAAAA'),
buf = new Buffer("Here is some data for the encrypt"), // 32 chars
enc = AESCrypt.encrypt(cryptkey, iv, buf);
var dec = AESCrypt.decrypt(cryptkey, iv, enc);

console.warn("encrypt length: ", enc.length);
console.warn("encrypt in Base64:", enc.toString('base64'));
console.warn("decrypt all: " + dec.toString('utf8'));
Run Code Online (Sandbox Code Playgroud)


Lan*_*aas 6

我的问题是我传递给解密函数的字符串是空的。我内置了对空字符串的检查,但我没有再次收到该消息。

decrypt: function(text){
                if(text.length == 0){
                    return text;
                }
                return this.decipher.update(text, 'hex', 'utf8') + this.decipher.final('utf8');
            }
Run Code Online (Sandbox Code Playgroud)