You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.1 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. include_once "errorCode.php";
  3. /**
  4. * PKCS7Encoder class
  5. *
  6. * 提供基于PKCS7算法的加解密接口.
  7. */
  8. class PKCS7Encoder
  9. {
  10. public static $block_size = 32;
  11. /**
  12. * 对需要加密的明文进行填充补位
  13. * @param $text 需要进行填充补位操作的明文
  14. * @return 补齐明文字符串
  15. */
  16. function encode($text)
  17. {
  18. $block_size = PKCS7Encoder::$block_size;
  19. $text_length = strlen($text);
  20. //计算需要填充的位数
  21. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  22. if ($amount_to_pad == 0) {
  23. $amount_to_pad = PKCS7Encoder::block_size;
  24. }
  25. //获得补位所用的字符
  26. $pad_chr = chr($amount_to_pad);
  27. $tmp = "";
  28. for ($index = 0; $index < $amount_to_pad; $index++) {
  29. $tmp .= $pad_chr;
  30. }
  31. return $text . $tmp;
  32. }
  33. /**
  34. * 对解密后的明文进行补位删除
  35. * @param decrypted 解密后的明文
  36. * @return 删除填充补位后的明文
  37. */
  38. function decode($text)
  39. {
  40. $pad = ord(substr($text, -1));
  41. if ($pad < 1 || $pad > 32)
  42. {
  43. $pad = 0;
  44. }
  45. return substr($text, 0, (strlen($text) - $pad));
  46. }
  47. }
  48. /**
  49. * Prpcrypt class
  50. *
  51. * 提供接收和推送给公众平台消息的加解密接口.
  52. */
  53. class Prpcrypt
  54. {
  55. public $key;
  56. function __construct($k)
  57. {
  58. $this->key = base64_decode($k . "=");
  59. }
  60. /**
  61. * 对明文进行加密
  62. * @param string $text 需要加密的明文
  63. * @return string 加密后的密文
  64. */
  65. public function encrypt($text, $appid)
  66. {
  67. try {
  68. //获得16位随机字符串,填充到明文之前
  69. $random = $this->getRandomStr();
  70. $text = $random . pack("N", strlen($text)) . $text . $appid;
  71. // 网络字节序
  72. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  73. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  74. $iv = substr($this->key, 0, 16);
  75. //使用自定义的填充方式对明文进行补位填充
  76. $pkc_encoder = new PKCS7Encoder;
  77. $text = $pkc_encoder->encode($text);
  78. mcrypt_generic_init($module, $this->key, $iv);
  79. //加密
  80. $encrypted = mcrypt_generic($module, $text);
  81. mcrypt_generic_deinit($module);
  82. mcrypt_module_close($module);
  83. //print(base64_encode($encrypted));
  84. //使用BASE64对加密后的字符串进行编码
  85. return array(ErrorCode::$OK, base64_encode($encrypted));
  86. } catch (Exception $e) {
  87. //print $e;
  88. return array(ErrorCode::$EncryptAESError, null);
  89. }
  90. }
  91. /**
  92. * 对密文进行解密
  93. * @param string $encrypted 需要解密的密文
  94. * @return string 解密得到的明文
  95. */
  96. public function decrypt($encrypted, $appid)
  97. {
  98. try {
  99. //使用BASE64对需要解密的字符串进行解码
  100. $ciphertext_dec = base64_decode($encrypted);
  101. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  102. $iv = substr($this->key, 0, 16);
  103. mcrypt_generic_init($module, $this->key, $iv);
  104. //解密
  105. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  106. mcrypt_generic_deinit($module);
  107. mcrypt_module_close($module);
  108. } catch (Exception $e) {
  109. return array(ErrorCode::$DecryptAESError, null);
  110. }
  111. try {
  112. //去除补位字符
  113. $pkc_encoder = new PKCS7Encoder;
  114. $result = $pkc_encoder->decode($decrypted);
  115. //去除16位随机字符串,网络字节序和AppId
  116. if (strlen($result) < 16)
  117. return "";
  118. $content = substr($result, 16, strlen($result));
  119. $len_list = unpack("N", substr($content, 0, 4));
  120. $xml_len = $len_list[1];
  121. $xml_content = substr($content, 4, $xml_len);
  122. $from_appid = substr($content, $xml_len + 4);
  123. } catch (Exception $e) {
  124. //print $e;
  125. return array(ErrorCode::$IllegalBuffer, null);
  126. }
  127. if ($from_appid != $appid)
  128. return array(ErrorCode::$ValidateAppidError, null);
  129. return array(0, $xml_content);
  130. }
  131. /**
  132. * 随机生成16位字符串
  133. * @return string 生成的字符串
  134. */
  135. function getRandomStr()
  136. {
  137. $str = "";
  138. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  139. $max = strlen($str_pol) - 1;
  140. for ($i = 0; $i < 16; $i++)
  141. {
  142. $str .= $str_pol[mt_rand(0, $max)];
  143. }
  144. return $str;
  145. }
  146. }
  147. ?>