FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

[Resolved] Cookie Encryption

 


thnn
I am trying to two-way encrypt the data in a cookie so that a user cannot change it. However I do not want to use MD5 as it is one way. I also do not want to use rot13 because it is insecure. Does anyone know if MCrypt is installed on the server?

Also does anyone know of anyway I could two-way encrypt the data in a cookie.


Last edited by thnn on Thu Dec 15, 2005 10:59 pm; edited 1 time in total
thnn
OK i worked out i need to use MCrypt. I just though it would share how I did it with everyone.

Code:
<?php

       function cookieencode($input)
       {
       $key = "yourkey";

       $td = mcrypt_module_open('blowfish', '', 'ecb', '');
       $ks = mcrypt_enc_get_key_size($td);
       $key = substr($key, 0, $ks);
       $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
       mcrypt_generic_init($td, $key, $iv);
       
       //do the encrpyting
       $encrypted_data = mcrypt_generic($td, $input);
       //end the system
       mcrypt_generic_deinit($td);
       mcrypt_module_close($td);
       //return the results
       return $encrypted_data;
       }

       function cookiedecode($input)
       {
       $key = "yourkey";

       $td = mcrypt_module_open('blowfish', '', 'ecb', '');
       $ks = mcrypt_enc_get_key_size($td);
       $key = substr($key, 0, $ks);
       $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
       mcrypt_generic_init($td, $key, $iv);
       
       //do the decrpyting
       $decrypt = mdecrypt_generic($td, $input);
       //end the system
       mcrypt_generic_deinit($td);
       mcrypt_module_close($td);
       //return the results
       return $decrypt;
       }
   
?>


Then you can call the different functions depending on what you want to do. Example to encode you would call cookieencode or to decode you would call cookiedecode.
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.