Skip to content

Instantly share code, notes, and snippets.

@mkhalid03
Created September 29, 2021 18:24
Show Gist options
  • Save mkhalid03/73da4ac60ced9e5fb1ee764c9ac82a18 to your computer and use it in GitHub Desktop.
Save mkhalid03/73da4ac60ced9e5fb1ee764c9ac82a18 to your computer and use it in GitHub Desktop.
Decrypt Base64 encoded string using PHP
<?php
/**
* Returns the decrypted text.
*
* @param $dataToDecrypt
* @param $key
*
* @return false|string
*/
function decrypt($dataToDecrypt, $key) {
$cipher_algorithm = 'AES-256-CBC';
$dataToDecrypt = base64_decode($dataToDecrypt);
$key = base64_decode($key);
$ivSize = openssl_cipher_iv_length($cipher_algorithm);
$iv = substr($dataToDecrypt, 0, $ivSize);
$encData = substr($dataToDecrypt, $ivSize);
return openssl_decrypt($encData, $cipher_algorithm, $key, OPENSSL_RAW_DATA, $iv);
}
// Example Code
$dataToDecrypt = "5Wqx0pgeh4ST2LLiw1fPmw6VQhqE4Cif1XtILSqSSZM=";
$key = "8QKcoNHVSIfEL0pdT2OkIOM6DB6XpEkjK8mz+VfHR0I=";
$result = decrypt($dataToDecrypt, $key);
print $result; // Output text wil be: 6\LD\%x@Y6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment