【发布时间】:2021-01-05 19:03:07
【问题描述】:
我正在将一个 nodeJS 应用程序与一个 ColdFusion 应用程序合并。我使用下面的方法在 ColdFusion 中进行了加密,其中密钥是加密密钥字符串
key = 'nQw7y6QejwGFh/SNrul20Q=='
encrypt(value, key, "AES/CBC/PKCS5Padding", "HEX");
然后,在 NodeJS 中,我尝试使用加密对其进行解密
const crypto = require('crypto');
const key = "nQw7y6QejwGFh/SNrul20Q==";
const binaryEncryptionKey = new Buffer( key, "base64" );
decrypt = (value) => {
try {
var decipher = crypto.createDecipheriv( "AES-128-CBC", binaryEncryptionKey );
var value = (
decipher.update( value, "base64", "utf8" ) +
decipher.final( "utf8" )
);
return value;
} catch (err) {
console.log(err);
}
}
它首先为缓冲区返回一个警告:
DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
然后,我得到了错误:
TypeError [ERR_INVALID_ARG_TYPE]: The "iv" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
我没有“iv”,因为 ColdFusion 端没有使用它来加密。是否可以在 NodeJS 中解密?
- 当我更改为 Buffer.alloc 时,出现错误:
TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument must be of type number. Received type string ('nQw7y6QejwGFh/SNrul20Q==..)
例如,我有以下加密字符串:FB391CAAE5CD8FF47C55211ED8636D213C95F233B615D4E56CB7CD6B051D01DF356E1C45ED7AABAB5F9BCBB9EED6355B
谢谢
【问题讨论】:
-
不幸的是,您没有给我们一个示例数据集(明文、密文和密钥),所以我无法检查任何实现,但是当您使用“CBC”作为模式时,必须曾经是iv。根据文档“当 ColdFusion 自动创建 IV 时,它会生成一个安全的随机 IV 并将其添加到加密数据中。”密文的前 16 个字节是 iv - 将此值用作 iv 的输入并解密其余部分。
-
@MichaelFehr,对此感到抱歉。我刚刚更新了帖子以添加要解密的密钥和示例数据。我不知道密文是或应该是什么。你能给我举个例子吗?谢谢
标签: node.js encryption coldfusion