node.js aes decipher.final错误

时间:2021-09-07 15:01:11

Im using the following sample. The problem I have is when decrypting:

我使用以下样本。我遇到的问题是解密时:

var decrypted = decipher.update(edata, 'binary') + decipher.final('binary');

var decrypted = decipher.update(edata,'binary')+ decipher.final('binary');

gets an error digital envelope routines:EVD_DecryptFinal_ex:wrong final block length. Ive searched but cant seem to figure it out. Im only referring to the node.js encrypt/decrypt code:

获取错误数字包络例程:EVD_DecryptFinal_ex:错误的最终块长度。我搜索过,但似乎无法弄明白。我只是指node.js加密/解密代码:

function AES()
{
}

AES.prototype.encrypt256 = function(input, password, callback)
{
   var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');

    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    var data = new Buffer(input, 'utf8').toString('binary');

    var cipher = crypto.createCipheriv('aes-256-cbc', key, iv.slice(0,16));
    var encrypted = cipher.update(data, 'binary') + cipher.final('binary');
    var encoded = new Buffer(encrypted, 'binary').toString('base64');
    callback(encoded);
}

AES.prototype.decrypt256 = function(input, password, callback)
{
    // Convert urlsafe base64 to normal base64
    var input = input.replace(/\-/g, '+').replace(/_/g, '/');
    // Convert from base64 to binary string
    var edata = new Buffer(input, 'base64').toString('binary')

    // Create key from password
    var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');

    // Create iv from password and key
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    // Decipher encrypted data
    var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv.slice(0,16));
    var decrypted = decipher.update(edata, 'binary') + decipher.final('binary');
    var plaintext = new Buffer(decrypted, 'binary').toString('utf8');

    callback(plaintext);
}


var data = "This is some test that I will use to remove";
var password = "test";
var aes = new AES();
aes.encrypt256(data, password, function(encrypted_data)
{
    console.log("Encrypted=> " + encrypted_data);

    aes.decrypt256(encrypted_data, password, function(decrypted_data)
    {
        console.log("Decrypted=> " + decrypted_data);
    });
});

Any help with be great.

任何帮助都很棒。

1 个解决方案

#1


1  

I was able to get it working. Here is the code in case anyone is interested:

我能够让它运作起来。以下是有兴趣的人的代码:

var crypto = require("crypto");
function encrypt(text, password){

   var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    var data = new Buffer(text, 'utf8').toString('binary');

    var cipher = crypto.createCipher('aes-256-cbc', key, iv.slice(0,16));

    var encrypted = cipher.update(data,'utf8','hex');
    encrypted += cipher.final('hex');
    var crypted = new Buffer(encrypted, 'binary').toString('base64');

  return crypted;
}

function decrypt(text, password){

    var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');
    // Create iv from password and key
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');
  var input = text.replace(/\-/g, '+').replace(/_/g, '/');
  var edata = new Buffer(input, 'base64').toString('binary');

  var decipher = crypto.createDecipher('aes-256-cbc', key, iv.slice(0,16));

    var decrypted = decipher.update(edata,'hex','utf8');
    decrypted += decipher.final('utf8');
    var dec = new Buffer(decrypted, 'binary').toString('utf8');
  return dec;
}

var pass = 'test';
var hw = encrypt("hello world the is one of the best one eight 333 43345 45654654", pass);
console.log(hw);
var dd = decrypt(hw, pass);
console.log(dd);

#1


1  

I was able to get it working. Here is the code in case anyone is interested:

我能够让它运作起来。以下是有兴趣的人的代码:

var crypto = require("crypto");
function encrypt(text, password){

   var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    var data = new Buffer(text, 'utf8').toString('binary');

    var cipher = crypto.createCipher('aes-256-cbc', key, iv.slice(0,16));

    var encrypted = cipher.update(data,'utf8','hex');
    encrypted += cipher.final('hex');
    var crypted = new Buffer(encrypted, 'binary').toString('base64');

  return crypted;
}

function decrypt(text, password){

    var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');
    // Create iv from password and key
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');
  var input = text.replace(/\-/g, '+').replace(/_/g, '/');
  var edata = new Buffer(input, 'base64').toString('binary');

  var decipher = crypto.createDecipher('aes-256-cbc', key, iv.slice(0,16));

    var decrypted = decipher.update(edata,'hex','utf8');
    decrypted += decipher.final('utf8');
    var dec = new Buffer(decrypted, 'binary').toString('utf8');
  return dec;
}

var pass = 'test';
var hw = encrypt("hello world the is one of the best one eight 333 43345 45654654", pass);
console.log(hw);
var dd = decrypt(hw, pass);
console.log(dd);