当前位置:首页 > PHP教程 > php应用 > 列表

php写的AES加密解密类分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-02-28 16:05:20 浏览: 评论:0 

这篇文章主要介绍了php写的AES加密解密类,实际是为YII框架写的,不在YII框架时只需替换其中的两句代码即可使用,需要的朋友可以参考下,今天写了一个php的AES加密类。适用于Yii的扩展。

如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。

类代码:

  1. <?php 
  2. /** 
  3.  * php AES加解密类 
  4.  * 如果要与java共用,则密钥长度应该为16位长度 
  5.  * 因为java只支持128位加密,所以php也用128位加密,可以与java互转。 
  6.  * 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。 
  7.  * java 要使用AES/CBC/NoPadding标准来加解密 
  8.  *  
  9.  * @author Terry 
  10.  * 
  11.  */ 
  12. class PhpAes 
  13. { 
  14.     /** 
  15.      * This was AES-128 / CBC / NoPadding encrypted. 
  16.      * return base64_encode string 
  17.      * @author Terry 
  18.      * @param string $plaintext 
  19.      * @param string $key 
  20.      */ 
  21.     public static function AesEncrypt($plaintext,$key = null) 
  22.     { 
  23.         $plaintext = trim($plaintext); 
  24.         if ($plaintext == '') return ''; 
  25.         if(!extension_loaded('mcrypt')) 
  26.             throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.')); 
  27.         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); 
  28.         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); 
  29.         $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module)); 
  30.         /* Create the IV and determine the keysize length, use MCRYPT_RAND 
  31.          * on Windows instead */ 
  32.         $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module)); 
  33.         /* Intialize encryption */ 
  34.         mcrypt_generic_init($module, $key, $iv); 
  35.  
  36.         /* Encrypt data */ 
  37.         $encrypted = mcrypt_generic($module, $plaintext); 
  38.  
  39.         /* Terminate encryption handler */ 
  40.         mcrypt_generic_deinit($module); 
  41.         mcrypt_module_close($module); 
  42.         return base64_encode($encrypted); 
  43.     } 
  44.  
  45.     /** 
  46.      * This was AES-128 / CBC / NoPadding decrypted. 
  47.      * @author Terry 
  48.      * @param string $encrypted     base64_encode encrypted string 
  49.      * @param string $key 
  50.      * @throws CException 
  51.      * @return string 
  52.      */ 
  53.     public static function AesDecrypt($encrypted, $key = null) 
  54.     { 
  55.         if ($encrypted == '') return ''; 
  56.         if(!extension_loaded('mcrypt')) 
  57.             throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.')); 
  58.  
  59.         $ciphertext_dec = base64_decode($encrypted); 
  60.         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); 
  61.         $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module)); 
  62.  
  63.         $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module)); 
  64.  
  65.         /* Initialize encryption module for decryption */ 
  66.         mcrypt_generic_init($module, $key, $iv); 
  67.  
  68.         /* Decrypt encrypted string */ 
  69.         $decrypted = mdecrypt_generic($module, $ciphertext_dec); 
  70.  
  71.         /* Terminate decryption handle and close module */ 
  72.         mcrypt_generic_deinit($module); 
  73.         mcrypt_module_close($module); 
  74.         return rtrim($decrypted,"\0"); 
  75.     } 
  76.  
  77.     /** 
  78.      * Returns the length of the given string. 
  79.      * If available uses the multibyte string function mb_strlen. 
  80.      * @param string $string the string being measured for length 
  81.      * @return integer the length of the string 
  82.      */ 
  83.     private static function strlen($string) 
  84.     { 
  85.         return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string); 
  86.     } 
  87.  
  88.     /** 
  89.      * Returns the portion of string specified by the start and length parameters. 
  90.      * If available uses the multibyte string function mb_substr 
  91.      * @param string $string the input string. Must be one character or longer. 
  92.      * @param integer $start the starting position 
  93.      * @param integer $length the desired portion length 
  94.      * @return string the extracted part of string, or FALSE on failure or an empty string. 
  95.      */ 
  96.     private static function substr($string,$start,$length) 
  97.     { 
  98.         return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length); 
  99.     } 
  100. } 

Tags: AES加密解密

分享到: