php实现的AES加密类定义与用法示例

这篇文章主要介绍了php实现的AES加密类定义与用法,结合完整实例形式分析了基于php的AES加密类实现及使用方法,需要的朋友可以参考下。

本文实例讲述了php实现的AES加密类定义与用法,分享给大家供大家参考,具体如下:

CryptAES.class.php文件:

  1. <?php
  2. class CryptAES
  3. {
  4. protected $cipher = MCRYPT_RIJNDAEL_128;
  5. protected $mode = MCRYPT_MODE_ECB;
  6. protected $pad_method = NULL;
  7. protected $secret_key = '';
  8. protected $iv = '';
  9. public function set_cipher($cipher)
  10. {
  11. $this->cipher = $cipher;
  12. }
  13. public function set_mode($mode)
  14. {
  15. $this->mode = $mode;
  16. }
  17. public function set_iv($iv)
  18. {
  19. $this->iv = $iv;
  20. }
  21. public function set_key($key)
  22. {
  23. $this->secret_key = $key;
  24. }
  25. public function require_pkcs5()
  26. {
  27. $this->pad_method = 'pkcs5';
  28. }
  29. protected function pad_or_unpad($str, $ext)
  30. {
  31. if ( is_null($this->pad_method) )
  32. {
  33. return $str;
  34. }
  35. else
  36. {
  37. $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
  38. if ( is_callable($func_name) )
  39. {
  40. $size = mcrypt_get_block_size($this->cipher, $this->mode);
  41. return call_user_func($func_name, $str, $size);
  42. }
  43. }
  44. return $str;
  45. }
  46. protected function pad($str)
  47. {
  48. return $this->pad_or_unpad($str, '');
  49. }
  50. protected function unpad($str)
  51. {
  52. return $this->pad_or_unpad($str, 'un');
  53. }
  54. public function encrypt($str)
  55. {
  56. $str = $this->pad($str);
  57. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  58. if ( emptyempty($this->iv) )
  59. {
  60. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  61. }
  62. else
  63. {
  64. $iv = $this->iv;
  65. }
  66. mcrypt_generic_init($td, $this->secret_key, $iv);
  67. $cyper_text = mcrypt_generic($td, $str);
  68. //$rt=base64_encode($cyper_text);
  69. $rt = bin2hex($cyper_text);
  70. mcrypt_generic_deinit($td);
  71. mcrypt_module_close($td);
  72. return $rt;
  73. }
  74. public function decrypt($str){
  75. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  76. if ( emptyempty($this->iv) )
  77. {
  78. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  79. }
  80. else
  81. {
  82. $iv = $this->iv;
  83. }
  84. mcrypt_generic_init($td, $this->secret_key, $iv);
  85. $decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
  86. //$decrypted_text = mdecrypt_generic($td, base64_decode($str));
  87. $rt = $decrypted_text;
  88. mcrypt_generic_deinit($td);
  89. mcrypt_module_close($td);
  90. return $this->unpad($rt);
  91. }
  92. public static function hex2bin($hexdata) {
  93. $bindata = '';
  94. $length = strlen($hexdata);
  95. for ($i=0; $i < $length; $i += 2)
  96. {
  97. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  98. }
  99. return $bindata;
  100. }
  101. public static function pkcs5_pad($text, $blocksize)
  102. {
  103. $pad = $blocksize - (strlen($text) % $blocksize);
  104. return $text . str_repeat(chr($pad), $pad);
  105. }
  106. public static function pkcs5_unpad($text)
  107. {
  108. $pad = ord($text{strlen($text) - 1});
  109. if ($pad > strlen($text)) return false;
  110. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
  111. return substr($text, 0, -1 * $pad);
  112. }
  113. }
  114. ?>

用法:

  1. require_once("CryptAES.class.php");
  2. $keyStr = 'ss4fs4skfhksk';
  3. $aes = new CryptAES();
  4. $keyStr = $aes->hex2bin($keyStr);
  5. $aes->set_key($keyStr);
  6. $aes->require_pkcs5();
  7. $d = $aes->encrypt($data);

注:这里需要在php.ini中开启:extension=php_mcrypt.dll