H, hope someone can help me, I'm new to this encryption thing, and I found two handy functions off php.net - 1 to encrypt a cookie, and another to decrypt a cookie.
I encrypt the username on one page using this:
encryptCookie($username);
function encryptCookie($value){
if(!$value){return false;}
$key = 'The Line Secret Key';
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return trim(base64_encode($crypttext)); //encode for cookie
}
and then i decrypt it on another page like so:
function decryptCookie($value){
if(!$value){return false;}
$key = 'The Line Secret Key';
$crypttext = base64_decode($value); //decode cookie
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
echo decryptCookie($_COOKIE["myUsrDtls"]);
but the encrypted and decrypted values are not the same!!! Help me please!
A computer always does what you tell it to, but rarely does what you want it to.....
I encrypt the username on one page using this:
encryptCookie($username);
function encryptCookie($value){
if(!$value){return false;}
$key = 'The Line Secret Key';
$text = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return trim(base64_encode($crypttext)); //encode for cookie
}
and then i decrypt it on another page like so:
function decryptCookie($value){
if(!$value){return false;}
$key = 'The Line Secret Key';
$crypttext = base64_decode($value); //decode cookie
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
echo decryptCookie($_COOKIE["myUsrDtls"]);
but the encrypted and decrypted values are not the same!!! Help me please!
A computer always does what you tell it to, but rarely does what you want it to.....