- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 Node 中有以下代码,我正在尝试转换为 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) />
最佳答案
更新:
我们在 chat 跟进了并证明该值最终与 encrypt() 一起使用.由于 encrypt() 已经(自动)处理填充,因此不需要自定义 padBytes()
函数。但是,它确实需要切换到不太常用的 encryptBinary()维护UTF-16编码的函数。常规的 encrypt() 函数只处理 UTF-8,这会产生完全不同的结果。
// Result with sample key/iv: P22lWwtD8pDrNdQGRb2T/w==
result = encrypt("abc", theKey, theAlgorithm, theEncoding, theIV);
// Result Result with sample key/iv: LJCROj8trkXVq1Q8SQNrbA==
input = charsetDecode("abc", "utf-16le");
result= binaryEncode(encryptBinary(input, theKey, theAlgorithm, theIV), "base64);
it's converting the string to utf-16 (ucs2) and then adding padding to each character. ... I feel I'm missing something with the encoding.
是的,第一部分似乎是将字符串解码为 UTF-16(或 UCS2,即 slightly different)。至于你错过了什么,你不是唯一的。在我找到 this comment 之前我也无法让它工作其中解释了“UTF-16”前置 BOM。要省略 BOM,请根据所需的字节顺序使用“UTF-16BE”或“UTF-16LE”。
why it's only pushing the same value into the array over and over again.
因为这是 PCKS7 padding 的定义.它不是用 null 或 zeroes 之类的东西填充,而是计算需要多少字节填充。然后使用该数字作为填充值。例如,假设一个字符串需要额外的三个字节填充。 PCKS7 附加值 3
- 三次:"string"+ "3"+ "3"+ "3"
。
其余代码在 CF 中类似。不幸的是,charsetDecode() 的结果是不可变的。您必须构建一个单独的数组来保存填充,然后将两者结合起来。
请注意,此示例使用 CF2016 特定语法组合数组,但也可以使用简单的循环来代替
功能:
function padBytes(string text){
var combined = [];
var padding = [];
// decode as utf-16
var decoded = charsetDecode(arguments.text,"utf-16le");
// how many padding bytes are needed?
var need = 16 - (arrayLen(decoded) % 16);
// fill array with any padding bytes
for(var i = 0; i < need; i++) {
padding.append(need);
}
// concatenate the two arrays
// CF2016+ specific syntax. For earlier versions, use a loop
combined = combined.append(decoded, true);
combined = combined.append(padding, true);
return combined;
}
用法:
result = padBytes("2018-06-14T15:44:10Z testaccount");
writeDump(binaryEncode( javacast("byte[]", result), "base64"));
关于node.js - 如何将 padbytes 函数转换为 coldfusion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50866857/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!