Last active
March 15, 2018 16:23
-
-
Save nhalstead/bc18d465d0af91022756b1c0f714b9f6 to your computer and use it in GitHub Desktop.
PHP_MCRYPT_CBC_256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Used to encrypt data for Web Transport. | |
* | |
* Code from the link attached and modified to be better. | |
* @link http://truelogic.org/wordpress/2012/09/14/url-masking-by-encrypting-query-string/ | |
* @param Array Data to Encrypt and Return | |
* @param String Encryption Key used to make the Output | |
*/ | |
function encryptData($data, $keySalt = null){ | |
if($keySalt == null){ return false; } | |
$qryStr = json_encode($data); //making query string | |
$query = base64_encode( | |
mcrypt_encrypt( | |
MCRYPT_RIJNDAEL_256, | |
md5($keySalt), $qryStr, | |
MCRYPT_MODE_CBC, md5(md5($keySalt)) | |
) | |
); | |
return $query; | |
} | |
/** | |
* Used to decrypt data the has been encrypted via encryptData. | |
* | |
* Code from the link attached and modified to be better. | |
* @link http://truelogic.org/wordpress/2012/09/14/url-masking-by-encrypting-query-string/ | |
* @param String The Data that is Encrypted by encryptData | |
* @param String Encryption Key used to make the encrypted data. | |
*/ | |
function decryptData($data, $keySalt){ | |
if($keySalt == null){ return false; } | |
$queryString = rtrim( | |
mcrypt_decrypt( | |
MCRYPT_RIJNDAEL_256, md5($keySalt), | |
base64_decode($data), | |
MCRYPT_MODE_CBC, md5(md5($keySalt)) | |
) | |
, "\0"); | |
return json_decode($queryString, true); | |
} | |
// Test Code | |
$t = encryptData($_SERVER, "KEY_GOES_HERE"); | |
echo "Out Data:<br>"; | |
echo $t; | |
echo "<br><br>In Data:<br>"; | |
print_r( decryptData($t, "KEY_GOES_HERE") ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment