【问题标题】:Decrypt AES/CBC/PKCS5Padding in NodeJS from ColdFusion encrypt从 ColdFusion 加密解密 NodeJS 中的 AES/CBC/PKCS5Padding
【发布时间】: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


【解决方案1】:

ColdFusion encrypt 函数描述为here。可以明确指定 AES/CBC 所需的 16 字节 IV。如果没有给出 IV,它会自动生成并放在密文的前面(s.也是 Michael Fehr 的评论)。 NodeJS中的解密可以如下进行:

const crypto = require('crypto');

const key = Buffer.from('nQw7y6QejwGFh/SNrul20Q==', 'base64');  
const ivCiphertext = Buffer.from('FB391CAAE5CD8FF47C55211ED8636D213C95F233B615D4E56CB7CD6B051D01DF356E1C45ED7AABAB5F9BCBB9EED6355B', 'hex');
const iv = ivCiphertext.slice(0, 16);
const ciphertext = ivCiphertext.slice(16);

var decrypt = (value) => {
    try {
        var decipher = crypto.createDecipheriv('AES-128-CBC', key, iv);
        var value = 
            decipher.update(value, '', 'utf8') +
            decipher.final('utf8');
        return value;
    } catch (err) {
        console.log(err);
    }
}

console.log(decrypt(ciphertext)); // 4388576099656673

结果为@​​987654327@,与对应的ColdFusion脚本一致,可以执行e.g. here, 年代。 例子

<cfscript>
key = 'nQw7y6QejwGFh/SNrul20Q==';
iv = BinaryDecode('FB391CAAE5CD8FF47C55211ED8636D21', 'HEX');
ciphertext = '3C95F233B615D4E56CB7CD6B051D01DF356E1C45ED7AABAB5F9BCBB9EED6355B';
plaintext = decrypt(ciphertext, key, 'AES/CBC/PKCS5Padding', 'HEX', iv);
writeOutput(plaintext);
</cfscript>

请注意,new Buffer() 已弃用。 Buffer.alloc() 的描述可以在 here 找到。

【讨论】:

  • 太棒了!谢谢你的解释
猜你喜欢
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多