pkcs7Encoder.php 4.3 KB

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