【问题标题】:binaryDecode to javascript二进制解码到 javascript
【发布时间】:2020-05-02 08:49:50
【问题描述】:

我正在尝试生成用于解码已使用 ColdFusion 加密的密码的密钥。当我在binaryDecode(string, "hex") 中传递参数时,我得到了不同的结果。如何将其翻译成 JavaScript?

在 ColdFusion 中:

binaryDecode("CA993CED42F374C9291FC2484495CD9334E8C822", "hex")   
output is: -54-10360-1966-13116-554131-627268-107-51-10952-24-5634 (binary)  
after that the output will be looped and store in array variable 
then binaryEncode(javaCast("byte[]", arrayVariable), "Base64") 
the result is generatedKey



in Node js:
i didnt get the same output after the binaryDecode
43413939334345443432463337344339323931464332343834343935434439333334453843383232
I tried using `buffer.from()` but it just split to `43 41 39` etc. 

我尝试了很多东西,但我无法得到 -54-10360-1966-13116-554131-627268-107-51-10952-24-5634 的结果

【问题讨论】:

  • 您确定您查看的是正确的 ColdFusion 代码吗?您说您正在尝试“解码”密码,但 binaryDecode() 函数不用于加密。它用于将字符串数据转换回二进制数据。
  • 是的 binaryDecode 的返回将用作解码加密的密钥,但我只是意识到 CA993CED42F374C9291FC2484495CD9334E8C822 需要转换为十六进制字节,然后我需要 BASE64 编码,我注意到来自cryptii.com/pipes/base64-to-hex ,我只是不明白 binaryDecode 做了什么

标签: javascript node.js coldfusion


【解决方案1】:

这不是冷融合二进制解码。更有可能一些输出系统只是以这种格式打印。换句话说,这只是将char 转换为sbyte。 但如果你想试试这段代码

function convert(h) {
    h = h.split('');
    const r = [];
    for(let i=0; i<h.length; i+=2) {
        r.push(parseInt(h[i]+h[i+1], 16));
    }
    return r.map(e => e > 127 ? -(256-e): e).join('')
}


console.log(convert("CA993CED42F374C9291FC2484495CD9334E8C822"));

【讨论】:

  • 谢谢,但我想我意识到,我的问题出在哪里,我只需要更改 CA993CED42F374C9291FC2484495CD9334E8C822 字节十六进制和 base64 编码,我会更新我的解决方案
【解决方案2】:

我找到了我要找的东西

var btoa = require('btoa');

const  a= 'CA993CED42F374C9291FC2484495CD9334E8C822';
const f = Buffer.from(a, 'hex');
console.log(f);

const base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(f)));

console.log(base64String);

谢谢你的帮助

【讨论】:

    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多