Skip to content

Instantly share code, notes, and snippets.

@itgelo
Created January 23, 2017 03:27
Show Gist options
  • Save itgelo/5d79c664c9846f4460349ddac4c9ebcd to your computer and use it in GitHub Desktop.
Save itgelo/5d79c664c9846f4460349ddac4c9ebcd to your computer and use it in GitHub Desktop.
Simple Encrypt/Decrypt PHP
echo encrypt("itgelo");
echo decrypt("B9mFi5BNEhL0kbSdBauFq+L1MEDCmfnoSfUjQ3moAvw=");
function encrypt($string){
$key = "secret-key";
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
$encrypted = base64_encode($iv.mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hash('sha256', $key, true), $string, MCRYPT_MODE_CBC, $iv));
return $encrypted;
}
function decrypt($encrypted){
$key = "secret-key";
$data = base64_decode($encrypted);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, hash('sha256', $key, true), substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)), MCRYPT_MODE_CBC, $iv), "\0");
return $decrypted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment