【问题标题】:Node.js crypto aes-256-cbc-hmac-sha1 doesn't workNode.js 加密 aes-256-cbc-hmac-sha1 不起作用
【发布时间】:2014-05-07 19:48:14
【问题描述】:

我正在尝试将 aes-256-cbc-hmac-sha1 算法与 Node.js crypto module 一起使用。

这是一个代码 sn-p 显示我正在尝试做的事情:

// adapted from http://stackoverflow.com/a/6046913

var crypto = require('crypto');
var data = "I am the clear text data";
console.log('Original cleartext: ' + data);

// //// WORKS
// var algorithm = 'aes-128-cbc';
// var keyBuffer = crypto.randomBytes(16);
// var ivBuffer = crypto.randomBytes(16);

// DOES NOT WORK
var algorithm = 'aes-256-cbc-hmac-sha1';
var keyBuffer = crypto.randomBytes(32);
var ivBuffer = crypto.randomBytes(16);

// var algorithm = 'aes-256-cfb8';       // ok
// var keyBuffer = crypto.randomBytes(32);
// var ivBuffer = crypto.randomBytes(16);

// var algorithm = 'aes-128-cbc-hmac-sha1'; // fail
// var keyBuffer = crypto.randomBytes(16);
// var ivBuffer = crypto.randomBytes(16);

var clearEncoding = 'utf8';
var cipherEncoding = 'hex';

var cipher = crypto.createCipheriv(algorithm, keyBuffer, ivBuffer);
var cipherChunks = [];
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));

console.log('ciphertext', cipherChunks.join(''));

var decipher = crypto.createDecipheriv(algorithm, keyBuffer, ivBuffer);
var plainChunks = [];

//// all at once
// var encrypted = cipherChunks.join('');
// plainChunks.push(decipher.update(encrypted, cipherEncoding, clearEncoding));

//// in pieces
for (var i = 0; i < cipherChunks.length;i++) {
  plainChunks.push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));
}
plainChunks.push(decipher.final(clearEncoding));

// var pt = plainChunks.join('');

var pt = '';
for (i = 0; i < plainChunks.length; i++) pt += plainChunks[i].toString(clearEncoding);

console.log("UTF8 plaintext deciphered: " + pt);
console.log('GOOD with ' + algorithm + '?', pt === data);

没有包含 HMAC 的算法可以工作,但 HMAC 算法不能。 它在decipher.update 步骤上失败。完整输出:

Original cleartext: I am the clear text data
ciphertext 364ddcface495bcc4e7c8c895443143a632a98d0942b8c844d53db7d770fabca

crypto.js:279
  var ret = this._binding.update(data, inputEncoding);
                          ^
TypeError: error:00000000:lib(0):func(0):reason(0)
    at Decipheriv.Cipher.update (crypto.js:279:27)
    at Object.<anonymous> (../../crypto-example.js:44:29)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

但是,如果我自己创建一个 HMAC,它可以正常工作:

var crypto = require('crypto');
var data = "I am the clear text data";
var algorithm = 'sha1';
var keyBuffer = crypto.randomBytes(32);
var hmac = crypto.createHmac(algorithm, keyBuffer);
hmac.update(data);
var hash = hmac.digest('hex');
console.log('hash', hash);

任何想法我做错了什么?或者这是加密模块中的错误? (用节点 0.10.26 和 0.10.28 测试,结果相同。)

谢谢

(注意,也将此作为错误发布:https://github.com/joyent/node/issues/7583)

【问题讨论】:

    标签: javascript node.js encryption node-crypto


    【解决方案1】:

    密码部分只是aes-256-cbc 部分。 hmac 部分只是hmac-sha1。

    您必须创建两个对象,密码/解密实例和 hmac 实例。使用 cipher/decipher 实例的输出和 hmac 实例来创建用于验证目的的哈希。

    aes-256-cbc-hmac-sha1 仅用于识别密码/验证/等。组合并且本身不是实际的密码。

    【讨论】:

    • "aes-256-cbc-hmac-sha1" 出现在 crypto.getCiphers() 返回的列表中。所以它肯定看起来是一个真正的密码......我认为你的说法是不正确的。
    • 另外,它加密得很好,只是解密有问题。如果它不是密码,我预计它也会在加密时失败。
    猜你喜欢
    • 2022-08-17
    • 1970-01-01
    • 2021-04-22
    • 2013-08-11
    • 1970-01-01
    • 2022-11-16
    • 2020-01-14
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多