Php 加密和解密cookie

Php 加密和解密cookie,php,cookies,encryption,Php,Cookies,Encryption,我正在使用这个函数: function encryptData($value) { $key = "top 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(MCRY

我正在使用这个函数:

function encryptData($value) { 
   $key = "top 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 $crypttext;
} 

function decryptData($value) { 
   $key = "top secret key"; 
   $crypttext = $value; 
   $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); 
}
login.php

$time = time() + 60*60*24*30*12; //store cookie for one year
setcookie('cookie_name', encryptCookie('username'), $time, '/');
$cookie_value = decryptCookie($_COOKIE['cookie_name']);
cookie已设置并正在运行

问题:

  • 我如何解密
    $\u COOKIE['COOKIE\u name']
    的内容并将其打印出来

我需要在这行中打印它:

so.addVariable("uid", "<? if ($_COOKIE['username']) print $_COOKIE['username']; ?>");
so.addVariable(“uid”,“uid”);

您提供了自己进行加密和解密的代码

为了将加密的用户名保存到cookie,您可以使用以下命令:

$time = time()+60*60*24*30*12; //store cookie for one year
setcookie('username', encryptCookie($username), $time, '/');
要稍后从加密的cookie中检索用户名,请使用以下命令:

echo decryptCookie($_COOKIE['username']);

谢谢,cookie已设置,但不会显示我为空,我是说使用echo decryptCookie($\u cookie['username']);旁注:欧洲央行很弱,不要使用它。至少使用CBC。