Prpcrypt.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace weworkapi\receive;
  3. use weworkapi\receive\ErrorCode;
  4. use weworkapi\receive\PKCS7Encoder;
  5. /**
  6. * Prpcrypt class
  7. *
  8. * 提供接收和推送给公众平台消息的加解密接口.
  9. */
  10. class Prpcrypt
  11. {
  12. public $key = null;
  13. public $iv = null;
  14. /**
  15. * Prpcrypt constructor.
  16. * @param $k
  17. */
  18. public function __construct($k)
  19. {
  20. $this->key = base64_decode($k . '=');
  21. $this->iv = substr($this->key, 0, 16);
  22. }
  23. /**
  24. * 加密
  25. *
  26. * @param $text
  27. * @param $receiveId
  28. * @return array
  29. */
  30. public function encrypt($text, $receiveId)
  31. {
  32. try {
  33. //拼接
  34. $text = $this->getRandomStr() . pack('N', strlen($text)) . $text . $receiveId;
  35. //添加PKCS#7填充
  36. $pkc_encoder = new PKCS7Encoder();
  37. $text = $pkc_encoder->encode($text);
  38. //加密
  39. if (function_exists('openssl_encrypt')) {
  40. $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  41. } else {
  42. $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($text), MCRYPT_MODE_CBC, $this->iv);
  43. }
  44. return array(ErrorCode::$OK, $encrypted);
  45. } catch (Exception $e) {
  46. print $e;
  47. return array(MyErrorCode::$EncryptAESError, null);
  48. }
  49. }
  50. /**
  51. * 解密
  52. *
  53. * @param $encrypted
  54. * @param $receiveId
  55. * @return array
  56. */
  57. public function decrypt($encrypted, $receiveId)
  58. {
  59. try {
  60. //解密
  61. if (function_exists('openssl_decrypt')) {
  62. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
  63. } else {
  64. $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, base64_decode($encrypted), MCRYPT_MODE_CBC, $this->iv);
  65. }
  66. } catch (Exception $e) {
  67. return array(ErrorCode::$DecryptAESError, null);
  68. }
  69. try {
  70. //删除PKCS#7填充
  71. $pkc_encoder = new PKCS7Encoder();
  72. $result = $pkc_encoder->decode($decrypted);
  73. if (strlen($result) < 16) {
  74. return array();
  75. }
  76. //拆分
  77. $content = substr($result, 16, strlen($result));
  78. $len_list = unpack('N', substr($content, 0, 4));
  79. $xml_len = $len_list[1];
  80. $xml_content = substr($content, 4, $xml_len);
  81. $from_receiveId = substr($content, $xml_len + 4);
  82. } catch (Exception $e) {
  83. print $e;
  84. return array(ErrorCode::$IllegalBuffer, null);
  85. }
  86. if ($from_receiveId != $receiveId) {
  87. return array(ErrorCode::$ValidateCorpidError, null);
  88. }
  89. return array(0, $xml_content);
  90. }
  91. /**
  92. * 生成随机字符串
  93. *
  94. * @return string
  95. */
  96. private function getRandomStr()
  97. {
  98. $str = '';
  99. $str_pol = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyl';
  100. $max = strlen($str_pol) - 1;
  101. for ($i = 0; $i < 16; $i++) {
  102. $str .= $str_pol[mt_rand(0, $max)];
  103. }
  104. return $str;
  105. }
  106. }