Jump to content


Encrypting Cookie -- need help

  • Please log in to reply
No replies to this topic

#1 werty37

  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 18 June 2006 - 09:50 AM

I am trying to encrypt a cookie and then read its value in the next page by decrypting it
I am not able to decrypt the cookie value and read it...
Any help would be appreciated very much


// securedata_inc.php

class securedata
    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;

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>';
   echo '</pre>';
   echo "<p>Session ID: {$cookie['sid']}</p>";
   echo "<p>Login Time: {$cookie['logintime']}</p>";

I am able to set the encrypted cookie.. but not decrypt the cookie and read the values

Thank you

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users