在没有auth标签的NodeJS上解密aes-128-ccm
Decrypt aes-128-ccm on NodeJS without auth tag
我正在将一些加密代码从 Python 移植到 NodeJS。我有 Python 完美运行的示例:
from Cryptodome.Cipher import AES
key = bytes.fromhex("befe6acee59a8d3d3c97eeea8cdb9e99");
nonce = bytes.fromhex("5830b603313800002298c44124");
cipherpayload = bytes.fromhex("9f3f2d8dd339");
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4) # mac_len 4 or 8 or 16 keeps code working
cipher.update(bytes.fromhex("11")) # commenting this line keeps code working
print("result:" + "\"" + cipher.decrypt(cipherpayload).hex() + "\""); # prints result: "011003000104" as intended
但是 NodeJS 版本不工作:
const crypto = require('crypto');
let key = Buffer.from('befe6acee59a8d3d3c97eeea8cdb9e99', 'hex');
let nonce = Buffer.from('5830b603313800002298c44124', 'hex');
let cipherpayload = Buffer.from('9f3f2d8dd339', 'hex');
let cipher = crypto.createDecipheriv('aes-128-ccm', key, nonce, { authTagLength: 4 });
cipher.setAAD(Buffer.from('11', 'hex'), { plaintextLength: cipherpayload.length });
console.log('result: ' + '"' + cipher.update(cipherpayload).toString('hex') + '"'); // prints result: ""
所有找到的关于 aes-128-ccm 的示例和 NodeJS 使用带标签的 cipher.setAuthTag() ,但我没有 authTag 并且 Python 版本可以正常工作。我有什么办法可以在 NodeJS 中解密吗?
节点版本:v17.3.0
Python3.9.2
平台:2021-10-30-raspios-bullseye-armhf-lite
还在 64 位 Debian 上进行了测试,结果相同。
已编辑:
我从设备收到的这个问题的完整原始网络数据是 '95fe5830b603312298c44124f89f3f2d8dd3393800001a'
暴力破解标签(4 个字节让我这样做)并找到标签值 '07860841'
.
我相信,标签应该在网络数据中,但我不知道在哪里。
已编辑 2:
暴力破解另一个数据包,查看许多数据包,发现标签肯定不是从设备传输的。这是设备开发人员的错误。
确实没有办法通过完整性检查正确解密数据。
这个答案很危险,你应该检查 authTag,但如果你需要跳过这个检查,jscrypto (https://github.com/Hinaser/jscrypto) 可以帮助你做到这一点。
const JsCrypto = require('jscrypto');
var key = JsCrypto.Hex.parse("befe6acee59a8d3d3c97eeea8cdb9e99");
var nonce = JsCrypto.Hex.parse("5830b603313800002298c44124");
var cipherpayload = JsCrypto.Hex.parse("9f3f2d8dd339");
var cp = new JsCrypto.CipherParams({ cipherText: cipherpayload });
var decrypted = JsCrypto.AES.decrypt(cp, key, { iv: nonce, mode: JsCrypto.mode.CCM, padding: JsCrypto.pad.NoPadding });
console.log('result: ' + '"' + decrypted.toString() + '"');
我正在将一些加密代码从 Python 移植到 NodeJS。我有 Python 完美运行的示例:
from Cryptodome.Cipher import AES
key = bytes.fromhex("befe6acee59a8d3d3c97eeea8cdb9e99");
nonce = bytes.fromhex("5830b603313800002298c44124");
cipherpayload = bytes.fromhex("9f3f2d8dd339");
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4) # mac_len 4 or 8 or 16 keeps code working
cipher.update(bytes.fromhex("11")) # commenting this line keeps code working
print("result:" + "\"" + cipher.decrypt(cipherpayload).hex() + "\""); # prints result: "011003000104" as intended
但是 NodeJS 版本不工作:
const crypto = require('crypto');
let key = Buffer.from('befe6acee59a8d3d3c97eeea8cdb9e99', 'hex');
let nonce = Buffer.from('5830b603313800002298c44124', 'hex');
let cipherpayload = Buffer.from('9f3f2d8dd339', 'hex');
let cipher = crypto.createDecipheriv('aes-128-ccm', key, nonce, { authTagLength: 4 });
cipher.setAAD(Buffer.from('11', 'hex'), { plaintextLength: cipherpayload.length });
console.log('result: ' + '"' + cipher.update(cipherpayload).toString('hex') + '"'); // prints result: ""
所有找到的关于 aes-128-ccm 的示例和 NodeJS 使用带标签的 cipher.setAuthTag() ,但我没有 authTag 并且 Python 版本可以正常工作。我有什么办法可以在 NodeJS 中解密吗?
节点版本:v17.3.0
Python3.9.2
平台:2021-10-30-raspios-bullseye-armhf-lite
还在 64 位 Debian 上进行了测试,结果相同。
已编辑:
我从设备收到的这个问题的完整原始网络数据是 '95fe5830b603312298c44124f89f3f2d8dd3393800001a'
暴力破解标签(4 个字节让我这样做)并找到标签值 '07860841'
.
我相信,标签应该在网络数据中,但我不知道在哪里。
已编辑 2:
暴力破解另一个数据包,查看许多数据包,发现标签肯定不是从设备传输的。这是设备开发人员的错误。
确实没有办法通过完整性检查正确解密数据。
这个答案很危险,你应该检查 authTag,但如果你需要跳过这个检查,jscrypto (https://github.com/Hinaser/jscrypto) 可以帮助你做到这一点。
const JsCrypto = require('jscrypto');
var key = JsCrypto.Hex.parse("befe6acee59a8d3d3c97eeea8cdb9e99");
var nonce = JsCrypto.Hex.parse("5830b603313800002298c44124");
var cipherpayload = JsCrypto.Hex.parse("9f3f2d8dd339");
var cp = new JsCrypto.CipherParams({ cipherText: cipherpayload });
var decrypted = JsCrypto.AES.decrypt(cp, key, { iv: nonce, mode: JsCrypto.mode.CCM, padding: JsCrypto.pad.NoPadding });
console.log('result: ' + '"' + decrypted.toString() + '"');