Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rohansinghrawat's avatar

error in conversion of code in node to php?

I have one piece of code in node.js i.e

const crypto = require("crypto");
const algorithm = "aes-128-cbc";
const input = "CDP|20211118185142004gJaM0i|IDPPAY|PRADEEP|DMT|INR|91.00|911020033563906|211|288|283918|P|NEFT";
const keyStr = "UK9EFHDu4k+aU2tY4g/DKw==";

const keyBytes = Buffer.from(keyStr, "base64");

const iv = keyBytes.slice(0, 16)

const hash = crypto.createHash('sha256');
var checksum = hash.update(input);
checksum = hash.digest("hex")

const encCipher = crypto.createCipheriv(algorithm, keyBytes, iv);

var encrypted = encCipher.update(checksum, "utf-8", "base64");

console.log('Enc base64 Value: ' + encrypted);

encrypted += encCipher.final("base64");

console.log("Enc base64 Value: " + encrypted);
const finalEncVal = encodeURIComponent(encrypted);
console.log("Enc uri encoded Value: " + finalEncVal);


OUTPUT
Enc base64 Value: agPcqiFMjsUTiC9uW/RJOnpToE8XrIfU+a85h6R1x8kLWBtGuzL5rt1F8r3TQmQC/uAukCGE7h982Dym08Lx3g6kA7ev/TEpYg9xvO5Fcls=

now I have to do it in php but I think i am not doing it in correct format

 $input = 'CDP|20211118185142004gJaM0i|IDPPAY|PRADEEP|DMT|INR|91.00|911020033563906|211|288|283918|P|NEFT';
$str = 'UK9EFHDu4k+aU2tY4g/DKw==';

$keyBytes = base64_decode($str);
$iv = substr($keyBytes, 0, 16);

$checksum = hash('sha256', $input);

dd(bin2hex(openssl_encrypt($checksum, 'aes-128-cbc', $keyBytes, OPENSSL_RAW_DATA, $iv)));

OUTPUT
6a03dcaa214c8ec513882f6e5bf4493a7a53a04f17ac87d4f9af3987a475c7c90b581b46bb32f9aedd45f2bdd3426402fee02e902184ee1f7cd83ca6d3c2f1de0ea403b7affd3129620f71bcee45725b

both output are different

so can anyone help me with this or suggest me right path ?

0 likes
0 replies

Please or to participate in this conversation.