Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Created October 2, 2019 08:03
Show Gist options
  • Save surferxo3/627045a36af2a05a96e90c5c908b5b50 to your computer and use it in GitHub Desktop.
Save surferxo3/627045a36af2a05a96e90c5c908b5b50 to your computer and use it in GitHub Desktop.
Encrypt/Decrypt using AES-256 openssl raw data, SHA-512 digest seed, and Null Bytes initialization vector
<?php
/*#############################
* Developer: Mohammad Sharaf Ali
* Designation: Sr. SE
* Version: 1.0
*/#############################
// IMPLEMENTATION
function encrypt($requestId, $validationString) {
$digestSeed = hash('SHA512', $requestId, true);
$seedEncArray = substr($digestSeed, 0, 32);
$finalByteArray = openssl_encrypt(
$validationString,
'AES256',
$seedEncArray,
OPENSSL_RAW_DATA,
str_repeat(chr(0), 16)
);
return base64_encode($finalByteArray);
}
function decrypt($xref, $checksum) {
$decPwd = base64_decode($checksum);
$digestSeed = hash('SHA512', $xref, true);
$seedEncArray = substr($digestSeed, 0, 32);
$finalByteArray = openssl_decrypt(
$decPwd,
'AES256',
$seedEncArray,
OPENSSL_RAW_DATA,
str_repeat(chr(0), 16)
);
return $finalByteArray;
}
// USAGE
$requestId = '0000';
$validationString = 'testing123';
$encrypted = encrypt($requestId, $validationString);
echo 'Encrypted: ' . $encrypted;
echo '<br />';
$decrypted = decrypt($requestId, $encrypted);
echo 'Decrypted: ' . $decrypted;
echo '<br /><br />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment