很多时候我们需要对数据进行加密解密,比如有些数据需要保存到cookie中,但又不能被用户轻易得到这些数据,这时我们就需要加密这些数据保存到cookie中,等我们需要使用它们的时候再解密。
加密的过程如下:
02 |
$cookie_data = $this -> encrypt("nowamagic", $data);
|
06 |
'value' => $cookie_data,
|
07 |
'expire' => $user_expire,
|
12 |
$this->input->set_cookie($cookie);
|
15 |
public function encrypt($key, $plain_text) {
|
16 |
$plain_text = trim($plain_text);
|
17 |
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
|
18 |
$c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT,$iv);
|
19 |
return trim(chop(base64_encode($c_t)));
|
使用的时候再解密:
01 |
if( isset($_COOKIE['data']) )
|
04 |
$_SESSION['data'] = decrypt("nowamagic", $_COOKIE['data']);
|
07 |
function decrypt($key, $c_t) {
|
08 |
$c_t = trim(chop(base64_decode($c_t)));
|
09 |
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
|
10 |
$p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT,$iv);
|
11 |
return trim(chop($p_t));
|
这里记录下这个可逆的加密函数的使用。
相关文章:
-
2021-08-17
-
2022-12-23
-
2022-01-13
-
2021-09-06
-
2021-12-19
-
2021-10-18