pkcs7Encoder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $pad = 0;
  43. }
  44. return substr($text, 0, (strlen($text) - $pad));
  45. }
  46. }
  47. /**
  48. * Prpcrypt class
  49. *
  50. * 提供接收和推送给公众平台消息的加解密接口.
  51. */
  52. class Prpcrypt
  53. {
  54. public $key;
  55. function __construct($k)
  56. {
  57. $this->key = base64_decode($k . "=");
  58. }
  59. /**
  60. * 对明文进行加密
  61. * @param string $text 需要加密的明文
  62. * @return string 加密后的密文
  63. */
  64. public function encrypt($text, $appid)
  65. {
  66. try {
  67. if(version_compare(PHP_VERSION, '7','>=')) {
  68. //获得16位随机字符串,填充到明文之前
  69. $random = $this->getRandomStr();
  70. $text = $random . pack("N", strlen($text)) . $text . $appid;
  71. $iv = substr($this->key, 0, 16);
  72. $pkc_encoder = new PKCS7Encoder;
  73. $text = $pkc_encoder->encode($text);
  74. $encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
  75. } else {
  76. //获得16位随机字符串,填充到明文之前
  77. $random = $this->getRandomStr();
  78. $text = $random . pack("N", strlen($text)) . $text . $appid;
  79. // 网络字节序
  80. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  81. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  82. $iv = substr($this->key, 0, 16);
  83. //使用自定义的填充方式对明文进行补位填充
  84. $pkc_encoder = new PKCS7Encoder;
  85. $text = $pkc_encoder->encode($text);
  86. mcrypt_generic_init($module, $this->key, $iv);
  87. //加密
  88. $encrypted = mcrypt_generic($module, $text);
  89. mcrypt_generic_deinit($module);
  90. mcrypt_module_close($module);
  91. $encrypted = base64_encode($encrypted);
  92. }
  93. //print($encrypted);
  94. //使用BASE64对加密后的字符串进行编码
  95. return array(ErrorCode::$OK, $encrypted);
  96. } catch (Exception $e) {
  97. //print $e;
  98. return array(ErrorCode::$EncryptAESError, null);
  99. }
  100. }
  101. /**
  102. * 对密文进行解密
  103. * @param string $encrypted 需要解密的密文
  104. * @return string 解密得到的明文
  105. */
  106. public function decrypt($encrypted, $appid)
  107. {
  108. try {
  109. if(version_compare(PHP_VERSION, '7','>=')) {
  110. $iv = substr($this->key, 0, 16);
  111. $decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
  112. } else {
  113. //使用BASE64对需要解密的字符串进行解码
  114. $ciphertext_dec = base64_decode($encrypted);
  115. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  116. $iv = substr($this->key, 0, 16);
  117. mcrypt_generic_init($module, $this->key, $iv);
  118. //解密
  119. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  120. mcrypt_generic_deinit($module);
  121. mcrypt_module_close($module);
  122. }
  123. } catch (Exception $e) {
  124. return array(ErrorCode::$DecryptAESError, null);
  125. }
  126. try {
  127. //去除补位字符
  128. $pkc_encoder = new PKCS7Encoder;
  129. $result = $pkc_encoder->decode($decrypted);
  130. //去除16位随机字符串,网络字节序和AppId
  131. if (strlen($result) < 16)
  132. return "";
  133. $content = substr($result, 16, strlen($result));
  134. $len_list = unpack("N", substr($content, 0, 4));
  135. $xml_len = $len_list[1];
  136. $xml_content = substr($content, 4, $xml_len);
  137. $from_appid = substr($content, $xml_len + 4);
  138. if(version_compare(PHP_VERSION, '7','>=')) {
  139. if (!$appid) {
  140. //如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
  141. $appid = $from_appid;
  142. }
  143. }
  144. } catch (Exception $e) {
  145. //print $e;
  146. return array(ErrorCode::$IllegalBuffer, null);
  147. }
  148. if ($from_appid != $appid)
  149. return array(ErrorCode::$ValidateAppidError, null);
  150. //不注释上边两行,避免传入appid是错误的情况
  151. if(version_compare(PHP_VERSION, '7','>=')) {
  152. //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复
  153. return array(0, $xml_content, $from_appid);
  154. } else {
  155. return array(0, $xml_content);
  156. }
  157. }
  158. /**
  159. * 随机生成16位字符串
  160. * @return string 生成的字符串
  161. */
  162. function getRandomStr()
  163. {
  164. $str = "";
  165. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  166. $max = strlen($str_pol) - 1;
  167. for ($i = 0; $i < 16; $i++) {
  168. $str .= $str_pol[mt_rand(0, $max)];
  169. }
  170. return $str;
  171. }
  172. }
  173. ?>