|
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.
- // Encrypt the data and write it to the cookie
- $ Cookie_data = $ this-> encrypt ("nowamagic", $ data );
- $ Cookie = array (
- 'Name' => '$ data ',
- 'Value' => $ cookie_data,
- 'Expire '=> $ user_expire,
- 'Domain '=> '',
- 'Path' => '/',
- 'Prefix' =>''
- );
- $ This-> input-> set_cookie ($ cookie );
- // Encryption
- Public function encrypt ($ key, $ plain_text ){
- $ Plain_text = trim ($ plain_text );
- $ Iv = substr (md5 ($ key), 0, mcrypt_get_iv_size (MCRYPT_CAST_256, MCRYPT_MODE_CFB ));
- $ C_t = mcrypt_cfb (MCRYPT_CAST_256, $ key, $ plain_text, MCRYPT_ENCRYPT, $ iv );
- Return trim (chop (base64_encode ($ c_t )));
- }
- Decrypt it when using it:
- If (isset ($ _ COOKIE ['data'])
- {
- // Use cookies to assign values to Sessions
- $ _ SESSION ['data'] = decrypt ("nowamagic", $ _ COOKIE ['data']);
- }
- Function decrypt ($ key, $ c_t ){
- $ C_t = trim (chop (base64_decode ($ c_t )));
- $ Iv = substr (md5 ($ key), 0, mcrypt_get_iv_size (MCRYPT_CAST_256, MCRYPT_MODE_CFB ));
- $ P_t = mcrypt_cfb (MCRYPT_CAST_256, $ key, $ c_t, MCRYPT_DECRYPT, $ iv );
- Return trim (chop ($ p_t ));
- }
Here we will record the use of this reversible encryption function.
|