In many cases, we need to encrypt and decrypt data. For example, some data needs to be stored in cookies, but it cannot be easily obtained by users, in this case, we need to encrypt the data and save it to the cookie, and decrypt it when we need to use them.
The encryption process is as follows: 01 // encrypt the data and write it to the cookie
02 $ cookie_data = $ this-> encrypt ("nowamagic", $ data );
03
04 $ cookie = array (
05 'name' => '$ data ',
06 'value' => $ cookie_data,
07 'expire '=> $ user_expire,
08 'domain '=> '',
09 'path' => '/',
10 'prefix' =>''
11 );
12 $ this-> input-> set_cookie ($ cookie );
13
14 // Encryption
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 )));
20}
Decryption again when using: 01 if (isset ($ _ cookie ['data'])
02 {
03 // use cookies to assign values to sessions
04 $ _ session ['data'] = decrypt ("nowamagic", $ _ cookie ['data']);
05}
06
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 ));
12}
Here we will record the use of this reversible encryption function.