得到错误“错误的最终块长度";解密AES256密码时 [英] Getting error "wrong final block length" when decrypting AES256 cipher

查看:12
本文介绍了得到错误“错误的最终块长度";解密AES256密码时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临 此线程同时使用 AES 加密和解密.

I'm facing exactly the same problem mentioned in this thread while encrypting and decrypting using AES.

crypto.js:202
var ret = this._handle.final();^错误:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex:最终块长度错误
在错误(本机)
在 Decipher.Cipher.final (crypto.js:202:26)

crypto.js:202
var ret = this._handle.final(); ^ Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Error (native)
at Decipher.Cipher.final (crypto.js:202:26)

这些是我的加密和解密函数:

These are my encrypt and decrypt functions:

var config = {
cryptkey: crypto.createHash('sha256').update('Nixnogen').digest(),
iv: "a2xhcgAAAAAAAAAA"
};

function encryptText(text){
    console.log(config.cryptkey);
        var cipher = crypto.createCipheriv('aes-256-cbc', config.cryptkey, config.iv);
        var crypted = cipher.update(text,'utf8','binary');
        crypted += cipher.final('binary');
    crypted = new Buffer(crypted, 'binary').toString('base64');
        return crypted;
}

function decryptText(text){
    console.log(config.cryptkey);
        if (text === null || typeof text === 'undefined' || text === '') {return text;};
    text = new Buffer(text, 'base64').toString('binary');
        var decipher = crypto.createDecipheriv('aes-256-cbc', config.cryptkey, config.iv);
        var dec = decipher.update(text,'binary','utf8');
        dec += decipher.final('utf8');
        return dec;
}

我在 package.json 中设置了 "node": ">=0.10.0".

谁能告诉我如何解决它?我已经尝试了线程中提到的解决方案,但没有一个对我有用.

Can anyone tell me how to fix it? I have tried solutions mentioned in the thread but none of them are working for me.

更新:

我尝试了线程中提到的解决方案,但没有一个对我有用.我认为对此可能有不同的解决方案,因此,与其污染现有线程,不如决定创建一个新线程.此外,如果我在现有线程上继续此操作,可能会混淆未来的候选者以获取正确的解决方案.

I've tried solutions mentioned in the thread but none of them are working for me. I think there might be a different solution for this and hence, rather than polluting the existing thread, decided to create a new one. Also, if I continue this on the existing thread it might confuse future candidates for the right solution.

更新 2:

对于线程中提到的第二种解决方案,我有以下代码,但它也给了我同样的错误:

For the second solution mentioned in the thread, I have the following code, but it is also giving me the same error:

function encryptText(text){
    console.log(config.cryptkey);
        var cipher = crypto.createCipheriv('aes-256-cbc', config.cryptkey, config.iv);
    return Buffer.concat([
        cipher.update(text),
        cipher.final()
    ]);
}

function decryptText(text){
    console.log(config.cryptkey);
        if (text === null || typeof text === 'undefined' || text === '') {return text;};
        var decipher = crypto.createDecipheriv('aes-256-cbc', config.cryptkey, config.iv);
    return Buffer.concat([
        decipher.update(text),
        decipher.final()
    ]);
}

另外,我使用 mongoose 将这些函数用作我的 mongodb 数据库中的字段的 getter 和 setter.我不认为这样做会导致任何问题,但我仍然认为提及这一点是个好主意.

Also, I'm using these functions as getters and setters for a field in my mongodb database using mongoose. I don't think doing so will cause any issues, but I still thought it would be a good idea to mention this.

更新 3:

我正在尝试加密 Facebook 访问令牌并解密加密的 Facebook 访问令牌.

I'm trying to encrypt a Facebook access token and decrypt an encrypted Facebook access token.

要重现错误,您可以尝试加密此令牌:

To reproduce the error, you can try encrypting this token:

ASDFGHJKLO0cBACJDJoc25hkZAzcOfxhTBVpHRva4hnflYEwAHshZCi2qMihYXpS2fIDGsqAcAlfHLLo273OWImZAfo9TMYZCbuZABJkzPoo4HZA8HRJVCRACe6IunmBSMAEgGVv8KCLKIbL6Gf7HJy9PplEni2iJ06VoZBv0fKXUvkp1k7gWYMva1ZAyBsWiDiKChjq3Yh1ZCdWWEDRFGTHYJ

ASDFGHJKLO0cBACJDJoc25hkZAzcOfxhTBVpHRva4hnflYEwAHshZCi2qMihYXpS2fIDGsqAcAlfHLLo273OWImZAfo9TMYZCbuZABJkzPoo4HZA8HRJVCRACe6IunmBSMAEgGVv8KCLKIbL6Gf7HJy9PplEni2iJ06VoZBv0fKXUvkp1k7gWYMva1ZAyBsWiDiKChjq3Yh1ZCdWWEDRFGTHYJ

加密可以正常工作,您将获得一个加密字符串.

Encryption will work fine and you will get an encrypted string.

现在尝试解密您在上一步中获得的加密字符串.你会得到错误.

Now try decrypting the encrypted string which you got in the previous step. You will get the error.

推荐答案

这个问题在写这篇文章的时候已经有两年了,但它有很多观点,所以我希望这个答案仍然对那些有兴趣的人有用可能会遇到.

This question is two years old at the time of this writing, but it has quite a few views, so I hope this answer will still prove useful to someone who might come across it.

这里的问题是 encryptText 工作正常,但它没有返回字符串.它返回一个 Buffer.decryptText 需要一个字符串,而不是 Buffer,因此它会尝试读取它,就好像它是 Buffer 一样,您会收到错误收到了.

The problem here is that encryptText works fine, but it's not returning a string. It's returning a Buffer. decryptText is expecting a string, not a Buffer, so it tries to read it as though it were a Buffer and you get the error that you received.

这是一个简单的修复.我们只需要在加密文本时将 Buffer 序列化为字符串,并在解密文本时将收到的加密字符串反序列化.

This is a simple fix. We just need to serialise the Buffer to a string when we encrypt text, and deserialise the encrypted string we receive when we decrypt text.

在本例中,我使用 base64 编码,因为它在表示二进制数据时相当紧凑.

In this example, I use base64 encoding because it is fairly compact when representing binary data.

var config = {
  cryptkey: crypto.createHash('sha256').update('Nixnogen').digest(),
  iv: 'a2xhcgAAAAAAAAAA'
}

function encryptText (text) {
  console.log(config.cryptkey)
  var cipher = crypto.createCipheriv('aes-256-cbc', config.cryptkey, config.iv)
  return Buffer.concat([
    cipher.update(text),
    cipher.final()
  ]).toString('base64') // Output base64 string
}

function decryptText (text) {
  console.log(config.cryptkey)
  if (text === null || typeof text === 'undefined' || text === '') {
    return text
  }
  var decipher = crypto.createDecipheriv('aes-256-cbc', config.cryptkey, config.iv)
  return Buffer.concat([
    decipher.update(text, 'base64'), // Expect `text` to be a base64 string
    decipher.final()
  ]).toString()
}

var encrypted = encryptText('text') // 8xXuS7jLG6crqJFHHB5J5A==
var decrypted = decryptText(encrypted) // text

这篇关于得到错误“错误的最终块长度";解密AES256密码时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆