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. /* Encrypt data */
  34. $encrypted = mcrypt_generic($module, $plaintext);
  35. /* Terminate encryption handler */
  36. mcrypt_generic_deinit($module);
  37. mcrypt_module_close($module);
  38. return base64_encode(trim($encrypted));
  39. }
  40. /**
  41. * This was AES-128 / CBC / ZeroBytePadding decrypted.
  42. * @author Terry
  43. * @param string $encrypted base64_encode encrypted string
  44. * @param string $key
  45. * @throws CException
  46. * @return string
  47. */
  48. public static function AesDecrypt($encrypted, $key = null)
  49. {
  50. if ($encrypted == '') return '';
  51. if(!extension_loaded('mcrypt'))
  52. throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
  53. $ciphertext_dec = base64_decode($encrypted);
  54. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  55. $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
  56. $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
  57. /* Initialize encryption module for decryption */
  58. mcrypt_generic_init($module, $key, $iv);
  59. /* Decrypt encrypted string */
  60. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  61. /* Terminate decryption handle and close module */
  62. mcrypt_generic_deinit($module);
  63. mcrypt_module_close($module);
  64. return self::UnPKCS5Padding($decrypted);
  65. }
  66. private static function strlen($string)
  67. {
  68. return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
  69. }
  70. private static function substr($string,$start,$length)
  71. {
  72. return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
  73. }
  74. private static function PKCS5Padding ($text, $blocksize) {
  75. $pad = $blocksize - (self::strlen($text) % $blocksize);
  76. return $text . str_repeat(chr($pad), $pad);
  77. }
  78. private static function UnPKCS5Padding($text)
  79. {
  80. $pad = ord($text{self::strlen($text)-1});
  81. if ($pad > self::strlen($text)) return false;
  82. if (strspn($text, chr($pad), self::strlen($text) - $pad) != $pad) return false;
  83. return substr($text, 0, -1 * $pad);
  84. }
  85. }
  86. ?>

使用方法,代码如下:

  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. ?>