NodeJS aes-256-cfb解密仅适用于前16个字节

时间:2015-05-22 09:14:07

标签: node.js encryption aes

下面的代码(摩卡测试)产生:

  1) crypto should work:
     AssertionError: "a secret message which has more than sixteen bytes" 
== "a secret message�\u001d\r�\u001dͫ\u0000 ~��x\u000f�\u001bZ\u001bp?\u0014\u0010\u0011b��\u0003kJ\u001dcV8\t"

很明显,消息的前16个字节已成功解密,但后续数据存在错误。

  • 我做错了什么?
  • 为什么我必须为AES256使用16字节(128位)的IV? (我尝试使用32个字节,但这导致了错误。)

(使用' aes-256-cfb'是一项要求,如果我选择aes-256-ctr,问题就会消失。)

代码/测试用例:

var assert = require('assert');
var crypto = require('crypto');

describe('crypto', function() {
  it('should work', function () {
    var message = new Buffer('a secret message which has more than sixteen bytes');
    var key = new Buffer('fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210', 'hex');
    var iv = new Buffer('0123456789abcdef0123456789abcdef', 'hex');      

    var encrypted = encrypt(message, key, iv);
    assert.equal(message.length, encrypted.length);

    assert.equal(message.length, encrypted.length);
    assert.equal('05c2aad7bac42ed084739340d47cec9f03d8e94ac7b1e11a56a6654f76ad2c8076bca162303e39b44d043732e98fdd28c52d', encrypted.toString('hex'));

    var decrypted = decrypt(encrypted, key, iv);

    assert.equal('a secret message which has more than sixteen bytes', decrypted.toString('utf8'));
  });
});

function encrypt(data, key, iv) {
  var cipher = crypto.createCipheriv('aes-256-cfb', key, iv);
  var encrypted = cipher.update(data);
  var encfinal = cipher.final();

  return Buffer.concat([encrypted, encfinal]);
}

var decrypt = encrypt;

1 个答案:

答案 0 :(得分:0)

问题是,与aes-256-cfb不同,aes-256-ctr不对称。

解决方法是使用crypto.createDecipheriv进行解密。