Hello house,
I used Mysql aes encryption to encrypt a column, and i can also decrypt the column in a SELECT query to return the actual text value.
However my problem is i want to be able to decrypt the encrypted data using php since i have the keys and the cipher and iv values, but is not working
$encryptionKey = 'myenckey';
$newiv = bin2hex(random_bytes(8)); //gives a 16 digits
$con->prepare("UPDATE subscriptions SET marginbalance = TO_BASE64(CONCAT('$newiv', AES_ENCRYPT(COALESCE(AES_DECRYPT(SUBSTRING(FROM_BASE64(marginbalance),17), '$encryptionKey',SUBSTRING(FROM_BASE64(marginbalance),1,16)),0) + '$amount', '$encryptionKey','$newiv'))),
To decrypt and get the value in msql i did this
// SELECT - decrypt
$encryptionKey = 'myenckey';
$search = $olanzarmanagercon->prepare("SELECT AES_DECRYPT(SUBSTRING(FROM_BASE64(marginbalance),17), '$encryptionKey', SUBSTRING(FROM_BASE64(marginbalance),1,16)) AS marginbalancenew FROM subscriptions WHERE userid = '12'");
$search->execute();
$result = $search->fetch(PDO::FETCH_ASSOC);
This gives my the value but i am trying to create a php function that will decrypt the encrypted data without using msql to do that, i have this php code yet it returns empty
```
function show($data) {
$key = 'myenckey';
$cipher = 'aes-256-cbc';
// Decode from Base64
$decodedData = base64_decode($data);
// Extract IV and encrypted data
$iv = substr($decodedData, 0, 16);
$encrypted = substr($decodedData, 16);
// Decrypt the data
$decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv);
return $decrypted;
}
Now this returns empty
echo show('OTA4OGU4YjY5NGUyMzc4ZFZYPqyoBNCzIwokVddSP7k=');
Please what do you think i should do to have it decrypt in php