This topic is locked

AES128 Decrypt the value in PHP instead of database

10/22/2023 10:26:14 AM
PHPRunner Tips and Tricks
DRCR Dev author

My AES decryption in database works fine normally, but I have a grouped query where decrypting in the database causes issues.
I need to know how to decrypt the value in PHP if I have the encrypted value in a PHP variable.
I tried this but I don't think the iv_length is correct.
function decrypt($encrypted, $key) {
// Assuming the same IV (initialization vector) was used for encryption and is the first 16 bytes of the encrypted data
$iv_length = 16;
$iv = substr($encrypted, 0, $iv_length);
$encrypted_data = substr($encrypted, $iv_length);
$decrypted = openssl_decrypt($encrypted_data, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv); return $decrypted;}