werty37 Posted June 18, 2006 Share Posted June 18, 2006 HiI am trying to encrypt a cookie and then read its value in the next page by decrypting itI am not able to decrypt the cookie value and read it...Any help would be appreciated very much[code]<?php// securedata_inc.phpclass 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][code]<?phprequire_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>";}?>[/code] I am able to set the encrypted cookie.. but not decrypt the cookie and read the valuesThank you Quote Link to comment https://forums.phpfreaks.com/topic/12291-encrypting-cookie-need-help/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.