I have some problem when porting a function from PHP to NodeJS. I have tried implement this PHP code with Node JS, but its not working.
This is the code in PHP
<?php
require_once 'vendor/autoload.php';
// function decrypt
function stringDecrypt($key, $string){
$encrypt_method = 'AES-256-CBC';
// hash
$key_hash = hex2bin(hash('sha256', $key));
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hex2bin(hash('sha256', $key)), 0, 16);
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key_hash, OPENSSL_RAW_DATA, $iv);
return $output;
}
?>
This is my code in NodeJs
function decryptResponse(timestamp, string, key) {
var key_hash = hex2bin(crypto.createHash("sha256").update(key).digest('hex'));
var iv = key_hash.substr(0,16);
var decoder = crypto.createDecipheriv('aes-256-cbc', key_hash, iv);
var output = decoder.update(Buffer.from(string).toString('base64'),'base64','utf8') += decoder.final('utf8');
console.log("Decrypt Result : ", output); //Not Showing on Log
}
function hex2bin(hex) {
var bytes = [];
var str;
for(var i=0; i< hex.length-1; i+=2){
bytes.push(parseInt(hex.substr(i, 2), 16));
}
str = String.fromCharCode.apply(String, bytes);
return str;
}
This function is called when I get the response from API and need to send it to the user.
var decompressedResponse = decryptResponse(timestamp, response.data.response, key);
res.send(decompressedResponse);
I need this function to decrypt a response from API so I really need this one working. Thank you for your help.
decompress()function is not used, why do you post it (and why is it applied at all in the NodeJS code)?hex2bin()in the NodeJS code is also not defined.decompress()is not called at all and in the NodeJS code the implementation is missing.