Prpcrypt.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. /**
  14. * PKCS7算法 - 加解密
  15. * Class PKCS7Encoder
  16. */
  17. class PKCS7Encoder
  18. {
  19. public static $blockSize = 32;
  20. /**
  21. * 对需要加密的明文进行填充补位
  22. * @param string $text 需要进行填充补位操作的明文
  23. * @return string 补齐明文字符串
  24. */
  25. function encode($text)
  26. {
  27. $amount_to_pad = PKCS7Encoder::$blockSize - (strlen($text) % PKCS7Encoder::$blockSize);
  28. if ($amount_to_pad == 0) {
  29. $amount_to_pad = PKCS7Encoder::$blockSize;
  30. }
  31. list($pad_chr, $tmp) = [chr($amount_to_pad), ''];
  32. for ($index = 0; $index < $amount_to_pad; $index++) {
  33. $tmp .= $pad_chr;
  34. }
  35. return $text . $tmp;
  36. }
  37. /**
  38. * 对解密后的明文进行补位删除
  39. * @param string $text 解密后的明文
  40. * @return string 删除填充补位后的明文
  41. */
  42. function decode($text)
  43. {
  44. $pad = ord(substr($text, -1));
  45. if ($pad < 1 || $pad > PKCS7Encoder::$blockSize) {
  46. $pad = 0;
  47. }
  48. return substr($text, 0, strlen($text) - $pad);
  49. }
  50. }
  51. /**
  52. * 公众号消息 - 加解密
  53. * Class Prpcrypt
  54. */
  55. class Prpcrypt
  56. {
  57. public $key;
  58. /**
  59. * Prpcrypt constructor.
  60. * @param $key
  61. */
  62. function __construct($key)
  63. {
  64. $this->key = base64_decode("{$key}=");
  65. }
  66. /**
  67. * 对明文进行加密
  68. * @param string $text 需要加密的明文
  69. * @param string $appid 公众号APPID
  70. * @return array
  71. */
  72. public function encrypt($text, $appid)
  73. {
  74. try {
  75. $random = $this->getRandomStr();
  76. $iv = substr($this->key, 0, 16);
  77. $pkcEncoder = new PKCS7Encoder();
  78. $text = $pkcEncoder->encode($random . pack("N", strlen($text)) . $text . $appid);
  79. $encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
  80. return [ErrorCode::$OK, $encrypted];
  81. } catch (Exception $e) {
  82. return [ErrorCode::$EncryptAESError, null];
  83. }
  84. }
  85. /**
  86. * 对密文进行解密
  87. * @param string $encrypted 需要解密的密文
  88. * @return array
  89. */
  90. public function decrypt($encrypted)
  91. {
  92. try {
  93. $iv = substr($this->key, 0, 16);
  94. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
  95. } catch (Exception $e) {
  96. return [ErrorCode::$DecryptAESError, null];
  97. }
  98. try {
  99. $pkcEncoder = new PKCS7Encoder();
  100. $result = $pkcEncoder->decode($decrypted);
  101. if (strlen($result) < 16) {
  102. return [ErrorCode::$DecryptAESError, null];
  103. }
  104. $content = substr($result, 16, strlen($result));
  105. $len_list = unpack("N", substr($content, 0, 4));
  106. $xml_len = $len_list[1];
  107. return [0, substr($content, 4, $xml_len), substr($content, $xml_len + 4)];
  108. } catch (Exception $e) {
  109. return [ErrorCode::$IllegalBuffer, null];
  110. }
  111. }
  112. /**
  113. * 随机生成16位字符串
  114. * @param string $str
  115. * @return string 生成的字符串
  116. */
  117. function getRandomStr($str = "")
  118. {
  119. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  120. $max = strlen($str_pol) - 1;
  121. for ($i = 0; $i < 16; $i++) {
  122. $str .= $str_pol[mt_rand(0, $max)];
  123. }
  124. return $str;
  125. }
  126. }
  127. /**
  128. * 仅用作类内部使用
  129. * 不用于官方API接口的errCode码
  130. * Class ErrorCode
  131. */
  132. class ErrorCode
  133. {
  134. public static $OK = 0;
  135. public static $ParseXmlError = 40002;
  136. public static $IllegalAesKey = 40004;
  137. public static $IllegalBuffer = 40008;
  138. public static $EncryptAESError = 40006;
  139. public static $DecryptAESError = 40007;
  140. public static $EncodeBase64Error = 40009;
  141. public static $DecodeBase64Error = 40010;
  142. public static $GenReturnXmlError = 40011;
  143. public static $ValidateAppidError = 40005;
  144. public static $ComputeSignatureError = 40003;
  145. public static $ValidateSignatureError = 40001;
  146. public static $errCode = [
  147. '0' => '处理成功',
  148. '40001' => '校验签名失败',
  149. '40002' => '解析xml失败',
  150. '40003' => '计算签名失败',
  151. '40004' => '不合法的AESKey',
  152. '40005' => '校验AppID失败',
  153. '40006' => 'AES加密失败',
  154. '40007' => 'AES解密失败',
  155. '40008' => '公众平台发送的xml不合法',
  156. '40009' => 'Base64编码失败',
  157. '40010' => 'Base64解码失败',
  158. '40011' => '公众帐号生成回包xml失败',
  159. ];
  160. /**
  161. * 获取错误消息内容
  162. * @param string $code 错误代码
  163. * @return bool
  164. */
  165. public static function getErrText($code)
  166. {
  167. if (isset(self::$errCode[$code])) {
  168. return self::$errCode[$code];
  169. }
  170. return false;
  171. }
  172. }