首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用AES256解密node.js返回错误的最终块长度

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

Stack Overflow用户
提问于 2014-01-22 19:27:17
回答 3查看 50K关注 0票数 19

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

TypeError:错误:0606506D:数字信封routines:EVP_DecryptFinal_ex:wrong最终块长度 在Decipheriv.Cipher.final (crypto.js:292:27)

下面是Gist的解密代码(为了方便起见,在这里显示):

代码语言:javascript
复制
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);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-22 19:48:29

好的,所以密码在开关中从0.8更改为0.10 Crypto methods return Buffer objects by default, rather than binary-encoded strings

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

这四行:

代码语言:javascript
复制
decoded = decipher.update(encryptdata);
decoded += decipher.final();
encryptdata = encipher.update(cleardata);
encryptdata += encipher.final();

改为:

代码语言:javascript
复制
decoded = decipher.update(encryptdata, 'binary', 'utf8');
decoded += decipher.final('utf8');
encryptdata = encipher.update(cleardata, 'utf8', 'binary');
encryptdata += encipher.final('binary');

这对我有效,但我愿意听取其他建议。

票数 27
EN

Stack Overflow用户

发布于 2014-01-22 20:18:41

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

代码语言:javascript
复制
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'));
票数 13
EN

Stack Overflow用户

发布于 2016-11-01 08:28:18

我的问题是,我传递给解密函数的字符串是空的。我构建了一个检查空字符串的方法,但是我没有再次收到消息。

代码语言:javascript
复制
decrypt: function(text){
                if(text.length == 0){
                    return text;
                }
                return this.decipher.update(text, 'hex', 'utf8') + this.decipher.final('utf8');
            }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21292142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档