日期:2011-12-04  浏览次数:20482 次

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

  1. <?php   
  2. class Mcrypt   
  3. {   
  4.     /**  
  5.      * 解密  
  6.      *   
  7.      * @param string $encryptedText 已加密字符串  
  8.      * @param string $key  密钥  
  9.      * @return string  
  10.      */   
  11.     public static function _decrypt($encryptedText,$key = null)   
  12.     {   
  13.         $key = $key === null ? Config::get('secret_key') : $key;   
  14.         $cryptText = base64_decode($encryptedText);   
  15.         $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);   
  16.         $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);   
  17.         $decryptText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key$cryptText, MCRYPT_MODE_ECB, $iv);   
  18.         return trim($decryptText);   
  19.     }   
  20.    
  21.     /**  
  22.      * 加密  
  23.      *  
  24.      * @param string $plainText 未加密字符串   
  25.      * @param string $key        密钥