VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • Php Aes加密类程序代码分享

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

AES加密算法 – 算法原理

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

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

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

使用方法,代码如下:

  1. <?php 
  2. require_once('./AES.php'); 
  3. //$aes = new AES(); 
  4. $aes = new AES(true);// 把加密后的字符串按十六进制进行存储 
  5. //$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储 
  6. $key = "this is a 32 byte key";// 密钥 
  7. $keys = $aes->makeKey($key); 
  8. $encode = "123456";// 被加密的字符串 
  9. $ct = $aes->encryptString($encode, $keys); 
  10. echo "encode = ".$ct."<br>";//开源代码phpfensi.com 
  11. $cpt = $aes->decryptString($ct, $keys); 
  12. echo "decode = ".$cpt; 
  13. ?> 
出处: http://www.phpfensi.com/php/20140822/4623.html

相关教程