Skip to content

Instantly share code, notes, and snippets.

@albohlabs
Created March 6, 2012 22:14
Show Gist options
  • Save albohlabs/1989352 to your computer and use it in GitHub Desktop.
Save albohlabs/1989352 to your computer and use it in GitHub Desktop.
PHP: password en- & decryption
# http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt
$key = 'password to (en/de)crypt';
$string = ' string to be encrypted '; // note the spaces
# To Encrypt:
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
# To Decrypt:
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment