Aucune description

pkcs7Encoder.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 > PKCS7Encoder::$block_size) {
  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 = null;
  55. public $iv = null;
  56. /**
  57. * Prpcrypt constructor.
  58. * @param $k
  59. */
  60. public function __construct($k)
  61. {
  62. $this->key = base64_decode($k . '=');
  63. $this->iv = substr($this->key, 0, 16);
  64. }
  65. /**
  66. * 加密
  67. *
  68. * @param $text
  69. * @param $receiveId
  70. * @return array
  71. */
  72. public function encrypt($text, $receiveId)
  73. {
  74. try {
  75. //拼接
  76. $text = $this->getRandomStr() . pack('N', strlen($text)) . $text . $receiveId;
  77. //添加PKCS#7填充
  78. $pkc_encoder = new PKCS7Encoder;
  79. $text = $pkc_encoder->encode($text);
  80. //加密
  81. if (function_exists('openssl_encrypt')) {
  82. $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  83. } else {
  84. $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($text), MCRYPT_MODE_CBC, $this->iv);
  85. }
  86. return array(ErrorCode::$OK, $encrypted);
  87. } catch (Exception $e) {
  88. print $e;
  89. return array(MyErrorCode::$EncryptAESError, null);
  90. }
  91. }
  92. /**
  93. * 解密
  94. *
  95. * @param $encrypted
  96. * @param $receiveId
  97. * @return array
  98. */
  99. public function decrypt($encrypted, $receiveId)
  100. {
  101. try {
  102. //解密
  103. if (function_exists('openssl_decrypt')) {
  104. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  105. } else {
  106. $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($encrypted), MCRYPT_MODE_CBC, $this->iv);
  107. }
  108. } catch (Exception $e) {
  109. return array(ErrorCode::$DecryptAESError, null);
  110. }
  111. try {
  112. //删除PKCS#7填充
  113. $pkc_encoder = new PKCS7Encoder;
  114. $result = $pkc_encoder->decode($decrypted);
  115. if (strlen($result) < 16) {
  116. return array();
  117. }
  118. //拆分
  119. $content = substr($result, 16, strlen($result));
  120. $len_list = unpack('N', substr($content, 0, 4));
  121. $xml_len = $len_list[1];
  122. $xml_content = substr($content, 4, $xml_len);
  123. $from_receiveId = substr($content, $xml_len + 4);
  124. } catch (Exception $e) {
  125. print $e;
  126. return array(ErrorCode::$IllegalBuffer, null);
  127. }
  128. // if ($from_receiveId != $receiveId) {
  129. // return array(ErrorCode::$ValidateCorpidError, null);
  130. // }
  131. return array(0, $xml_content);
  132. }
  133. /**
  134. * 生成随机字符串
  135. *
  136. * @return string
  137. */
  138. private function getRandomStr()
  139. {
  140. $str = '';
  141. $str_pol = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyl';
  142. $max = strlen($str_pol) - 1;
  143. for ($i = 0; $i < 16; $i++) {
  144. $str .= $str_pol[mt_rand(0, $max)];
  145. }
  146. return $str;
  147. }
  148. }