gpt4 book ai didi

node.js - 如何将 padbytes 函数转换为 coldfusion

转载 作者:搜寻专家 更新时间:2023-10-31 23:57:26 24 4
gpt4 key购买 nike

我在 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,这会产生完全不同的结果。

Trycf.com Example:

// 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com