2

I'm trying to generate a key for decoding a password that has been encrypted using ColdFusion. Im getting different result when im passing param in binaryDecode(string, "hex"). How do I translate this into JavaScript?

in 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. 

I've tried so many things but I'm unable to get that -54-10360-1966-13116-554131-627268-107-51-10952-24-5634 result

Runshax
  • 117
  • 2
  • 13
  • Does this answer your question? [How to convert hex string into a bytes array, and a bytes array in the hex string?](https://stackoverflow.com/questions/14603205/how-to-convert-hex-string-into-a-bytes-array-and-a-bytes-array-in-the-hex-strin) – O. Jones Jan 15 '20 at 15:27
  • 1
    Are you sure you are looking at the correct ColdFusion code? You say that you are trying to "decode" a password but the `binaryDecode()` function is not used for encryption. It is used to convert string data back into binary data. – Miguel-F Jan 15 '20 at 16:15
  • yes the return of binaryDecode will be used as a key for decoding encryption, but i just realize the CA993CED42F374C9291FC2484495CD9334E8C822 needs to be converted to hexadecimal bytes, then i need to BASE64 encoding, i notice from https://cryptii.com/pipes/base64-to-hex, i just didnt understand what that binaryDecode did – Runshax Jan 15 '20 at 16:25

2 Answers2

2

This is not coldfusion binaryDecode. More likely some output system just print in this format. Other words this is just convert char to sbyte. But If you want try this code

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"));
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
  • thanks, but i think i realize, where my problem is, i just need to change the CA993CED42F374C9291FC2484495CD9334E8C822 byte hex, and base64 encoding, i will update my solution – Runshax Jan 15 '20 at 16:27
1

i found what i was looking for

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);

thanks anyways for the help

Runshax
  • 117
  • 2
  • 13