Php reversible encryption function code example
// 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 )));
- } // Edit bbs.it-home.org 2013/6/7
// Decrypt
- If (isset ($ _ COOKIE ['data'])
- {
- // Use cookies to assign values to Sessions
- $ _ SESSION ['data'] = decrypt ("nowamagic", $ _ COOKIE ['data']);
- }
// Solves the function
- 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 ));
- }
- ?>
|