Prpcrypt.php 3.2 KB

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