我有以下Node.js代码,现在我想转换成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) />
2 0 1 8 - 0 6 - 1 4 T.....
数组的长度为76,基本上显示了那些空格。我之前尝试添加每个字母之间的charsetEncode(javacast("byte[]", [0]), "utf-8")
,但即使现在使用utf-16le,字符串的长度也不匹配节点。 - LeeishThe value [B cannot be converted to a number.
的错误。但是,是的,base64的结果与我需要的匹配。请参见我的函数padbytes2。它产生了相同的结果,但我无法在我的服务器上运行binaryencode(javacast(....
。 - Leeish