The below can be used for encrypting something that needs to be decrypted again. It can be used for implementing a simple “remember login” also.
The first parameter of mcrypt_encrypt or mcrypt_decrypt is “One of the MCRYPT_ciphername constants, or the name of the algorithm as string.” Details can be found here http://php.net/manual/en/mcrypt.ciphers.php Change it as per suitability.
Details of the fourth parameter in mcrypt_encrypt or mcrypt_decrypt can be found here http://php.net/manual/en/mcrypt.constants.php
Encryption
$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $key = "dkareitz77g5f865q8lt5jvdo0$36iu"; //private key $temp = mcrypt_encrypt(MCRYPT_CAST_256, $key, $email, MCRYPT_MODE_CFB, $iv); //encrypting the email id setcookie("username",base64_encode($temp),time()+60*60*24*30,"/", "domain.com"); //storing the username in Cookie after base64_encoding the above generated hash. base64_encoding not necessary and can be skipped $iv = mcrypt_encrypt(MCRYPT_BLOWFISH,"abcdefgh", $iv, MCRYPT_MODE_ECB); //encrypting the iv also using a different method setcookie("iv1",base64_encode($iv),time()+60*60*24*30,"/", "domain.com"); //storing in cookie.
Decryption
$logEmail = base64_decode($_COOKIE['username']); $iv = base64_decode($_COOKIE['iv1']); $iv = mcrypt_decrypt(MCRYPT_BLOWFISH,"abcdefgh", $iv, MCRYPT_MODE_ECB); //first decrypt the iv $key = "dkareitz77g5f865q8lt5jvdo0$36iu"; //same private key used in encryption $email = mcrypt_decrypt(MCRYPT_CAST_256, $key, $logEmail, MCRYPT_MODE_CFB, $iv); //decrypt the email