【发布时间】:2018-06-16 05:24:08
【问题描述】:
我在节点中有以下代码,我正在尝试转换为 ColdFusion:
// a correct implementation of PKCS7. The rijndael js has a PKCS7 padding already implemented
// however, it incorrectly pads expecting the phrase to be multiples of 32 bytes when it should pad based on multiples
// 16 bytes. Also, every javascript implementation of PKCS7 assumes utf-8 char encoding. C# however is unicode or utf-16.
// This means that chars need to be treated in our code as 2 byte chars and not 1 byte chars.
function padBytes(string){
const strArray = [...new Buffer(string, 'ucs2')];
const need = 16 - ((strArray.length) % 16);
for(let i = 0; i < need; i++) {
strArray.push(need);
}
return Buffer.from(strArray);
}
我试图准确地了解这个函数在做什么来转换它。我想我理解它,它将字符串转换为 UTF-16 (UCS2),然后为每个字符添加填充。但是,我不明白为什么 need 变量是它的值,也不明白如何在 CF 中实现这一点。
我也不明白为什么它只是一遍又一遍地将相同的值推送到数组中。对于初学者,在我的示例脚本中,字符串是2018-06-14T15:44:10Z testaccount。字符串数组长度为 64。我不知道如何在 CF 中实现。
我已经尝试过字符编码,转换为二进制和 UTF-16 的东西,只是对 js 函数的理解不够好,无法在 ColdFusion 中复制它。我觉得我在编码方面遗漏了一些东西。
编辑:
选择的答案解决了这个问题,但由于我最终尝试使用输入数据进行加密,更简单的方法是根本不使用此功能,而是执行以下操作:
<cfset stringToEncrypt = charsetDecode(input,"utf-16le") />
<cfset variables.result = EncryptBinary(stringToEncrypt, theKey, theAlgorithm, theIV) />
【问题讨论】:
标签: node.js coldfusion