Runshax
Runshax

Reputation: 117

binaryDecode to javascript

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

Upvotes: 2

Views: 264

Answers (2)

Runshax
Runshax

Reputation: 117

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

Upvotes: 1

Yaroslav Gaponov
Yaroslav Gaponov

Reputation: 2099

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

Upvotes: 2

Related Questions