好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Php Aes加密类程序代码分享 - php类库

Php Aes加密类程序代码分享

今天没事与了一个Php Aes加密类程序,适用于Yii的扩展如果不用在Yii框架中,把代码中Yii::app()->params[\'encryptKey\'] 换成你对应的默认key就可以了.

AES加密算法 – 算法原理

AES 算法基于排列和置换运算,排列是对数据重新进行安排,置换是将一个数据单元替换为另一个,AES 使用几种不同的方法来执行排列和置换运算.

AES 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据,与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据,通过分组密码返回的加密数据的位数与输入数据相同,迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据,代码如下: 

<?php  /**   * php AES加解密类   * 因为java只支持128位加密,所以php也用128位加密,可以与java互转。   * 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。   *   * @author Terry   *   */   class  PhpAes  {  /**   * This was AES-128 / CBC / ZeroBytePadding encrypted.   * return base64_encode string   * @author Terry   * @param string $plaintext   * @param string $key   */   public   static   function  AesEncrypt( $plaintext , $key  = null)  {  if  ( $plaintext  ==  '' )  return   '' ;  if (! extension_loaded ( 'mcrypt' ))  throw   new  CException(Yii::t( 'yii' , 'AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.' ));  $size  = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);  $plaintext  = self::PKCS5Padding( $plaintext ,  $size );  $module  = mcrypt_module_open(MCRYPT_RIJNDAEL_128,  '' , MCRYPT_MODE_CBC,  '' );  $key =self:: substr ( $key ===null ? Yii::app()->params[ 'encryptKey' ] :  $key , 0, mcrypt_enc_get_key_size( $module ));  /* Create the IV and determine the keysize length, use MCRYPT_RAND   * on Windows instead */   $iv  =  substr (md5( $key ),0,mcrypt_enc_get_iv_size( $module ));  /* Intialize encryption */   mcrypt_generic_init( $module ,  $key ,  $iv );    /* Encrypt data */   $encrypted  = mcrypt_generic( $module ,  $plaintext );    /* Terminate encryption handler */   mcrypt_generic_deinit( $module );  mcrypt_module_close( $module );  return   base64_encode (trim( $encrypted ));  }    /**   * This was AES-128 / CBC / ZeroBytePadding decrypted.   * @author Terry   * @param string $encrypted base64_encode encrypted string   * @param string $key   * @throws CException   * @return string   */   public   static   function  AesDecrypt( $encrypted ,  $key  = null)  {  if  ( $encrypted  ==  '' )  return   '' ;  if (! extension_loaded ( 'mcrypt' ))  throw   new  CException(Yii::t( 'yii' , 'AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.' ));    $ciphertext_dec  =  base64_decode ( $encrypted );  $module  = mcrypt_module_open(MCRYPT_RIJNDAEL_128,  '' , MCRYPT_MODE_CBC,  '' );  $key =self:: substr ( $key ===null ? Yii::app()->params[ 'encryptKey' ] :  $key , 0, mcrypt_enc_get_key_size( $module ));    $iv  =  substr (md5( $key ),0,mcrypt_enc_get_iv_size( $module ));    /* Initialize encryption module for decryption */   mcrypt_generic_init( $module ,  $key ,  $iv );    /* Decrypt encrypted string */   $decrypted  = mdecrypt_generic( $module ,  $ciphertext_dec );    /* Terminate decryption handle and close module */   mcrypt_generic_deinit( $module );  mcrypt_module_close( $module );  return  self::UnPKCS5Padding( $decrypted );  }      private   static   function   strlen ( $string )  {  return   extension_loaded ( 'mbstring' ) ? mb_strlen( $string , '8bit' ) :  strlen ( $string );  }    private   static   function   substr ( $string , $start , $length )  {  return   extension_loaded ( 'mbstring' ) ? mb_substr( $string , $start , $length , '8bit' ) :  substr ( $string , $start , $length );  }    private   static   function  PKCS5Padding ( $text ,  $blocksize ) {  $pad  =  $blocksize  - (self:: strlen ( $text ) %  $blocksize );  return   $text  .  str_repeat ( chr ( $pad ),  $pad );  }    private   static   function  UnPKCS5Padding( $text )  {  $pad  = ord( $text {self:: strlen ( $text )-1});  if  ( $pad  > self:: strlen ( $text ))  return  false;  if  ( strspn ( $text ,  chr ( $pad ), self:: strlen ( $text ) -  $pad ) !=  $pad )  return  false;  return   substr ( $text , 0, -1 *  $pad );  }  }  ?> 

使用方法,代码如下:

<?php  require_once ( './AES.php' );  //$aes = new AES();   $aes  =  new  AES(true); // 把加密后的字符串按十六进制进行存储   //$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储   $key  =  "this is a 32 byte key" ; // 密钥   $keys  =  $aes ->makeKey( $key );  $encode  =  "123456" ; // 被加密的字符串   $ct  =  $aes ->encryptString( $encode ,  $keys );  echo   "encode = " . $ct . "<br>" ; //开源代码phpfensi测试数据   $cpt  =  $aes ->decryptString( $ct ,  $keys );  echo   "decode = " . $cpt ;  ?> 

查看更多关于Php Aes加密类程序代码分享 - php类库的详细内容...

  阅读:44次