Home  >  Article  >  php教程  >  PHP实现加密解密算法

PHP实现加密解密算法

WBOY
WBOYOriginal
2016-06-21 08:52:511266browse

PHP实现加密解密的算法,如下代码:

  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. }   
  35.  
  36. //调用 
  37. class Cookie extends Mcrypt   
  38. {   
  39.     /**  
  40.      * 删除cookie  
  41.      *   
  42.      * @param array $args  
  43.      * @return boolean  
  44.      */   
  45.     public static function del($args)   
  46.     {   
  47.         $name = $args['name'];   
  48.         $domain = isset($args['domain']) ? $args['domain'] : null;   
  49.         return isset($_COOKIE[$name]) ? setcookie($name'', time() - 86400, '/'$domain) : true;   
  50.     }   
  51.        
  52.     /**  
  53.      * 得到指定cookie的值  
  54.      *   
  55.      * @param string $name  
  56.      */   
  57.     public static function get($name)   
  58.     {   
  59.         return isset($_COOKIE[$name]) ? parent::_decrypt($_COOKIE[$name]) : null;   
  60.     }   
  61.        
  62.     /**  
  63.      * 设置cookie  
  64.      *  
  65.      * @param array $args  
  66.      * @return boolean  
  67.      */   
  68.     public static function set($args)   
  69.     {   
  70.         $name = $args['name'];   
  71.         $value= parent::_encrypt($args['value']);   
  72.         $expire = isset($args['expire']) ? $args['expire'] : null;   
  73.         $path = isset($args['path']) ? $args['path'] : '/';   
  74.         $domain = isset($args['domain']) ? $args['domain'] : null;   
  75.         $secure = isset($args['secure']) ? $args['secure'] : 0;   
  76.         return setcookie($name$value$expire$path$domain$secure);   
  77.     }   
  78. }  



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn