You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.0 KiB

7 years ago
  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. //获得16位随机字符串,填充到明文之前
  68. $random = $this->getRandomStr();
  69. $text = $random . pack("N", strlen($text)) . $text . $appid;
  70. // 网络字节序
  71. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  72. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  73. $iv = substr($this->key, 0, 16);
  74. //使用自定义的填充方式对明文进行补位填充
  75. $pkc_encoder = new PKCS7Encoder;
  76. $text = $pkc_encoder->encode($text);
  77. mcrypt_generic_init($module, $this->key, $iv);
  78. //加密
  79. $encrypted = mcrypt_generic($module, $text);
  80. mcrypt_generic_deinit($module);
  81. mcrypt_module_close($module);
  82. //print(base64_encode($encrypted));
  83. //使用BASE64对加密后的字符串进行编码
  84. return array(ErrorCode::$OK, base64_encode($encrypted));
  85. } catch (Exception $e) {
  86. //print $e;
  87. return array(ErrorCode::$EncryptAESError, null);
  88. }
  89. }
  90. /**
  91. * 对密文进行解密
  92. * @param string $encrypted 需要解密的密文
  93. * @return string 解密得到的明文
  94. */
  95. public function decrypt($encrypted, $appid)
  96. {
  97. try {
  98. //使用BASE64对需要解密的字符串进行解码
  99. $ciphertext_dec = base64_decode($encrypted);
  100. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  101. $iv = substr($this->key, 0, 16);
  102. mcrypt_generic_init($module, $this->key, $iv);
  103. //解密
  104. $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  105. mcrypt_generic_deinit($module);
  106. mcrypt_module_close($module);
  107. } catch (Exception $e) {
  108. return array(ErrorCode::$DecryptAESError, null);
  109. }
  110. try {
  111. //去除补位字符
  112. $pkc_encoder = new PKCS7Encoder;
  113. $result = $pkc_encoder->decode($decrypted);
  114. //去除16位随机字符串,网络字节序和AppId
  115. if (strlen($result) < 16)
  116. return "";
  117. $content = substr($result, 16, strlen($result));
  118. $len_list = unpack("N", substr($content, 0, 4));
  119. $xml_len = $len_list[1];
  120. $xml_content = substr($content, 4, $xml_len);
  121. $from_appid = substr($content, $xml_len + 4);
  122. } catch (Exception $e) {
  123. //print $e;
  124. return array(ErrorCode::$IllegalBuffer, null);
  125. }
  126. if ($from_appid != $appid)
  127. return array(ErrorCode::$ValidateAppidError, null);
  128. return array(0, $xml_content);
  129. }
  130. /**
  131. * 随机生成16位字符串
  132. * @return string 生成的字符串
  133. */
  134. function getRandomStr()
  135. {
  136. $str = "";
  137. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  138. $max = strlen($str_pol) - 1;
  139. for ($i = 0; $i < 16; $i++) {
  140. $str .= $str_pol[mt_rand(0, $max)];
  141. }
  142. return $str;
  143. }
  144. }
  145. ?>