In PHP development, many times we need to encrypt and decrypt data, for example, some data need to be saved to a cookie, but not easily accessible to users, then we need to encrypt the data saved to the cookie, and so we need to use them to decrypt.
The encryption process is as follows:
Encrypt the data and write to the cookie $cookie_data = $this-Encrypt ("scutephp.com", $data); $cookie = Array ( ' name ' = ' $data ', ' value ' = = $cookie _data, ' expire ' = = $user _expire, ' domain ' = ' = ', ' path ' = '/', ' prefix ' + ', ' ; $this->input->set_cookie ($cookie); Encrypt 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)); }
Re-decrypt when used:
if (Isset ($_cookie[' data ')) { //Use a COOKIE to assign a value to the SESSION $_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)); }
The use of this reversible cryptographic function is recorded here.