Jump to content

Recommended Posts

Hi
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

[code]
<?php

// securedata_inc.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]


[code]
<?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>";
}
?>
[/code]

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

Thank you
Link to comment
https://forums.phpfreaks.com/topic/12291-encrypting-cookie-need-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.