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

Arash-S's avatar

cryptojs equivalent in php

‍‍‍There is some JavaScript code that works properly

   var response = JSON.parse(json);
    var data = response.data.split('.');

    var saltKey = CryptoJS.enc.Hex.parse(key);
    var passPhrase = CryptoJS.SHA3(referer + '.' + saltKey);
    var iv = CryptoJS.enc.Hex.parse(data[1]);

    var secret = CryptoJS.AES.decrypt(data[0], passPhrase, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8);

I tried to convert this code to its counterpart in php, but the output is not correct, or in other words, it is not the same as the output generated in JavaScript.

        $response = json_decode($json);
        $data = explode('.', $response->data);

        $salt = hex2bin($key);
        $iv = substr(hex2bin($data[1]),0, openssl_cipher_iv_length('AES-256-CBC'));

        $passPhrase = Keccak::hash($referer . "." . $salt, "256");

        $result = openssl_decrypt($data[0], 'AES-256-CBC', $passPhrase, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
        $result = bin2hex($result);
0 likes
0 replies

Please or to participate in this conversation.