1.加密解密类
1 class Mcrypt 2 { 3 /** 4 * 解密 5 * 6 * @param string $encryptedText 已加密字符串 7 * @param string $key 密钥 8 * @return string 9 */ 10 public static function _decrypt($encryptedText,$key = null) 11 { 12 $key = $key === null ? Config::get('secret_key') : $key; 13 $cryptText = base64_decode($encryptedText); 14 $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 15 $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); 16 $decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cryptText, MCRYPT_MODE_ECB, $iv); 17 return trim($decryptText); 18 } 19 20 /** 21 * 加密 22 * 23 * @param string $plainText 未加密字符串 24 * @param string $key 密钥 25 */ 26 public static function _encrypt($plainText,$key = null) 27 { 28 $key = $key === null ? Config::get('secret_key') : $key; 29 $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 30 $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); 31 $encryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plainText, MCRYPT_MODE_ECB, $iv); 32 return trim(base64_encode($encryptText)); 33 } 34 }
2.cookie加密解密类
1 <?php 2 class Cookie extends Mcrypt 3 { 4 /** 5 * 删除cookie 6 * 7 * @param array $args 8 * @return boolean 9 */ 10 public static function del($args) 11 { 12 $name = $args['name']; 13 $domain = isset($args['domain']) ? $args['domain'] : null; 14 return isset($_COOKIE[$name]) ? setcookie($name, '', time() - 86400, '/', $domain) : true; 15 } 16 17 /** 18 * 得到指定cookie的值 19 * 20 * @param string $name 21 */ 22 public static function get($name) 23 { 24 return isset($_COOKIE[$name]) ? parent::_decrypt($_COOKIE[$name]) : null; 25 } 26 27 /** 28 * 设置cookie 29 * 30 * @param array $args 31 * @return boolean 32 */ 33 public static function set($args) 34 { 35 $name = $args['name']; 36 $value= parent::_encrypt($args['value']); 37 $expire = isset($args['expire']) ? $args['expire'] : null; 38 $path = isset($args['path']) ? $args['path'] : '/'; 39 $domain = isset($args['domain']) ? $args['domain'] : null; 40 $secure = isset($args['secure']) ? $args['secure'] : 0; 41 return setcookie($name, $value, $expire, $path, $domain, $secure); 42 } 43 }