Skip to content

Instantly share code, notes, and snippets.

@tracend
Created May 10, 2011 05:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracend/963967 to your computer and use it in GitHub Desktop.
Save tracend/963967 to your computer and use it in GitHub Desktop.
PHP: Encryption / decryption of passwords
<?php
function encryptPassword($value){
if(!$value){return false;}
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SECRET_KEY, $text, MCRYPT_MODE_ECB, $iv);
return trim(base64_encode($crypttext)); //encode for cookie
}
function decryptPassword($value){
if(!$value){return false;}
$crypttext = base64_decode($value); //decode cookie
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SECRET_KEY, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment