AES.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. class AES{
  3. /**
  4. * This was AES-128 / CBC / PKCS5Padding
  5. * return base64_encode string
  6. * @author Terry
  7. * @param string $plaintext
  8. * @param string $key
  9. * @return string
  10. */
  11. public static function AesEncrypt($plaintext,$key = null)
  12. {
  13. $plaintext = trim($plaintext);
  14. if ($plaintext == '') return '';
  15. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  16. //PKCS5Padding
  17. $padding = $size - strlen($plaintext) % $size;
  18. // 添加Padding
  19. $plaintext .= str_repeat(chr($padding), $padding);
  20. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  21. $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
  22. $iv = str_repeat("\0", $size); //java里面的16个空数组对应的是\0.
  23. /* Intialize encryption */
  24. mcrypt_generic_init($module, $key, $iv);
  25. /* Encrypt data */
  26. $encrypted = mcrypt_generic($module, $plaintext);
  27. /* Terminate encryption handler */
  28. mcrypt_generic_deinit($module);
  29. mcrypt_module_close($module);
  30. return base64_encode($encrypted);
  31. }
  32. /**
  33. * Returns the length of the given string.
  34. * If available uses the multibyte string function mb_strlen.
  35. * @param string $string the string being measured for length
  36. * @return integer the length of the string
  37. */
  38. private static function strlen($string)
  39. {
  40. return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
  41. }
  42. /**
  43. * Returns the portion of string specified by the start and length parameters.
  44. * If available uses the multibyte string function mb_substr
  45. * @param string $string the input string. Must be one character or longer.
  46. * @param integer $start the starting position
  47. * @param integer $length the desired portion length
  48. * @return string the extracted part of string, or FALSE on failure or an empty string.
  49. */
  50. private static function substr($string,$start,$length)
  51. {
  52. return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
  53. }
  54. /**
  55. * This was AES-128 / CBC / PKCS5Padding
  56. * @author Terry
  57. * @param string $encrypted base64_encode encrypted string
  58. * @param string $key
  59. * @throws CException
  60. * @return string
  61. */
  62. public static function AesDecrypt($encrypted, $key = null)
  63. {
  64. if ($encrypted == '') return '';
  65. $ciphertext_dec = base64_decode($encrypted);
  66. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  67. $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
  68. $iv = str_repeat("\0", 16); //解密的初始化向量要和加密时一样。
  69. /* Initialize encryption module for decryption */
  70. mcrypt_generic_init($module, $key, $iv);
  71. /* Decrypt encrypted string */
  72. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  73. /* Terminate decryption handle and close module */
  74. mcrypt_generic_deinit($module);
  75. mcrypt_module_close($module);
  76. $a = rtrim($decrypted,"\0");
  77. return rtrim($decrypted,"\0");
  78. }
  79. }
  80. //echo AES::AesEncrypt("haha","mkeymkeymkeymkey");
  81. ?>