How to convert an AES decryption code to php?

Asked

Viewed 19 times

0

I need to decrypt an encrypted variable in php via Javascript in the Cloudflare Workers, how could I convert this broken code into php for JS in order to decrypt correctly?

Code:

define("SHARED_SECRET", "sup3rs3cr3t!!");

function decrypt($ivHashCiphertext, $password) {
    $method = "AES-256-CBC";
    $iv = substr($ivHashCiphertext, 0, 16);
    $hash = substr($ivHashCiphertext, 16, 32);
    $ciphertext = substr($ivHashCiphertext, 48);
    $key = hash('sha256', $password, true);

    if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;

    return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
}
$decrypted = decrypt(hex2bin($signature), SHARED_SECRET);
$json_payload = json_decode($decrypted, true);

How to get the above result in JS compatible with Cloudflare Workers?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.