I am trying to encrypt cookie using mcrypt rijindael 256. I am able to encrypt the cookie
but im not able to decrypt and retrieve values after the encryption. Here is the code
Code: Select all
<?php
class securedata
{
private
$key,
$iv;
public function __construct()
{
$this->key = 'Four score and twenty years ago';
$this->iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
}
public function encrypt($STR)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);
}
public function decrypt($STR)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, $STR, MCRYPT_MODE_CBC, $this->iv);
}
}
$secure = new securedata;
?>Code: Select all
<?php
require_once "securedata_inc.php";
if (!isset($_COOKIE['EMITES'])) {
$expiry = time()+600; $path = '/';
$cookieset['sid'] = md5(uniqid(rand(), true));
$cookieset['user'] = 'Guest';
$cookieset['logintime'] = time();
$cookieset['ip'] = $_SERVER['REMOTE_ADDR'];
$cookieset['useragent'] = $_SERVER['HTTP_USER_AGENT'];
// Set the cookie
setcookie('EMITES', $secure->encrypt(serialize($cookieset)), $expiry, $path);
echo "<p>Cookie placed: ";
} else {
$cookie = unserialize(stripslashes($secure->decrypt($_COOKIE['EMITES'])));
echo '<p>Unserialized cookie:</p><pre>';
print_r($cookie);
echo '</pre>';
echo "<p>Session ID: {$cookie['sid']}</p>";
echo "<p>Login Time: {$cookie['logintime']}</p>";
}
?>Thanks in advance