pkcs7Encoder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. include_once "errorCode.php";
  3. include_once "sha1.php";
  4. /**
  5. * PKCS7Encoder class
  6. *
  7. * 提供基于PKCS7算法的加解密接口.
  8. */
  9. class PKCS7Encoder
  10. {
  11. public static $block_size = 32;
  12. /**
  13. * 对需要加密的明文进行填充补位
  14. * @param $text 需要进行填充补位操作的明文
  15. * @return 补齐明文字符串
  16. */
  17. function encode($text)
  18. {
  19. $block_size = PKCS7Encoder::$block_size;
  20. $text_length = strlen($text);
  21. //计算需要填充的位数
  22. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  23. if ($amount_to_pad == 0) {
  24. $amount_to_pad = PKCS7Encoder::block_size;
  25. }
  26. //获得补位所用的字符
  27. $pad_chr = chr($amount_to_pad);
  28. $tmp = "";
  29. for ($index = 0; $index < $amount_to_pad; $index++) {
  30. $tmp .= $pad_chr;
  31. }
  32. return $text . $tmp;
  33. }
  34. /**
  35. * 对解密后的明文进行补位删除
  36. * @param decrypted 解密后的明文
  37. * @return 删除填充补位后的明文
  38. */
  39. function decode($text)
  40. {
  41. $pad = ord(substr($text, -1));
  42. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  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 = null;
  56. public $iv = null;
  57. /**
  58. * Prpcrypt constructor.
  59. * @param $k
  60. */
  61. public function __construct($k)
  62. {
  63. $this->key = base64_decode($k . '=');
  64. $this->iv = substr($this->key, 0, 16);
  65. }
  66. /**
  67. * 加密
  68. *
  69. * @param $text
  70. * @param $receiveId
  71. * @return array
  72. */
  73. public function encrypt($text, $receiveId)
  74. {
  75. try {
  76. //拼接
  77. $text = $this->getRandomStr() . pack('N', strlen($text)) . $text . $receiveId;
  78. //添加PKCS#7填充
  79. $pkc_encoder = new PKCS7Encoder;
  80. $text = $pkc_encoder->encode($text);
  81. //加密
  82. $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  83. return [ErrorCode::$OK, $encrypted];
  84. } catch (Exception $e) {
  85. print $e;
  86. return [MyErrorCode::$EncryptAESError, null];
  87. }
  88. }
  89. /**
  90. * 解密
  91. *
  92. * @param $encrypted
  93. * @param $receiveId
  94. * @return array
  95. */
  96. public function decrypt($encrypted, $receiveId)
  97. {
  98. try {
  99. //解密
  100. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  101. } catch (Exception $e) {
  102. return [ErrorCode::$DecryptAESError, null];
  103. }
  104. try {
  105. //删除PKCS#7填充
  106. $pkc_encoder = new PKCS7Encoder;
  107. $result = $pkc_encoder->decode($decrypted);
  108. if (strlen($result) < 16) {
  109. return [];
  110. }
  111. //拆分
  112. $content = substr($result, 16, strlen($result));
  113. $len_list = unpack('N', substr($content, 0, 4));
  114. $json_len = $len_list[1];
  115. $json_content = substr($content, 4, $json_len);
  116. $from_receiveId = substr($content, $json_len + 4);
  117. } catch (Exception $e) {
  118. print $e;
  119. return [ErrorCode::$IllegalBuffer, null];
  120. }
  121. if ($from_receiveId != $receiveId) {
  122. return [ErrorCode::$ValidateCorpidError, null];
  123. }
  124. return [0, $json_content];
  125. }
  126. /**
  127. * 生成随机字符串
  128. *
  129. * @return string
  130. */
  131. private function getRandomStr()
  132. {
  133. $str = '';
  134. $str_pol = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyl';
  135. $max = strlen($str_pol) - 1;
  136. for ($i = 0; $i < 16; $i++) {
  137. $str .= $str_pol[mt_rand(0, $max)];
  138. }
  139. return $str;
  140. }
  141. }