我可以使用Node的加密库解密AES加密消息,如下所示
const crypto = require('crypto');
const encryptedData = 'b6ab428efbcb93c2f483178114ac0608530e54428f1378c6d3be108531b730d1888e562044cd3acb8844a04d9d7602d83b96f0a758248ffd07cd9c530b76c91c';
const decryptResponse2 = (data) => {
const key = 'F5:A4:F4:AB:BF:68:CF:86:51:B4:AA';
const iv = Buffer.from(data.substring(0, 32), 'hex');
const payload = data.substring(32);
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv).setAutoPadding(false);
const decipherFinal = decipher.update(payload, 'hex', 'utf8') + decipher.final('utf8');
console.log(decipherFinal);
};
decryptResponse2(encryptedData);
我使用crypto-js
库创建了一个脚本,因为该脚本可在浏览器中使用。我尝试的代码如下:
const crypto = require('crypto-js');
const encryptedData = 'b6ab428efbcb93c2f483178114ac0608530e54428f1378c6d3be108531b730d1888e562044cd3acb8844a04d9d7602d83b96f0a758248ffd07cd9c530b76c91c';
const decryptResponse = (data) => {
const key = 'F5:A4:F4:AB:BF:68:CF:86:51:B4:AA';
const iv = Buffer.from(data.substring(0, 32), 'hex');
const payload = data.substring(32);
let decryptedData = crypto.AES.decrypt(
payload,
key,
{
iv: iv,
mode: crypto.mode.CBC,
padding: crypto.pad.NoPadding
});
console.log(decryptedData.toString());
}
decryptResponse(encryptedData);
但是,不仅会生成错误的解密数据,而且解密后的消息甚至不一致。我不知道自己在做什么错,因为我对加密和解密不甚了解。
任何帮助都会得到帮助。
答案 0 :(得分:2)
由于@GrafiCode将我指向right place,所以我能够使用配置对象的format
属性来解决它。
以下是代码:
const crypto = require('crypto-js');
const encryptedData = 'b6ab428efbcb93c2f483178114ac0608530e54428f1378c6d3be108531b730d1888e562044cd3acb8844a04d9d7602d83b96f0a758248ffd07cd9c530b76c91c';
const decryptResponse = (data) => {
const key = crypto.enc.Utf8.parse('F5:A4:F4:AB:BF:68:CF:86:51:B4:AA');
const iv = crypto.enc.Hex.parse(data.substring(0, 32));
const payload = data.substring(32);
let decryptedData = crypto.AES.decrypt(
payload,
key,
{
iv: iv,
mode: crypto.mode.CBC,
// padding: crypto.pad.NoPadding,
format: crypto.format.Hex
});
console.log(crypto.enc.Utf8.stringify(decryptedData));
}
decryptResponse(encryptedData);
我将padding: crypto.pad.NoPadding
注释掉了,因为在启用解密数据的末尾有不可打印的字符。