Prpcrypt.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WeChat\Prpcrypt;
  14. /**
  15. * 公众号消息 - 加解密
  16. * Class Prpcrypt
  17. */
  18. class Prpcrypt
  19. {
  20. public $key;
  21. /**
  22. * Prpcrypt constructor.
  23. * @param $key
  24. */
  25. function __construct($key)
  26. {
  27. $this->key = base64_decode("{$key}=");
  28. }
  29. /**
  30. * 对明文进行加密
  31. * @param string $text 需要加密的明文
  32. * @param string $appid 公众号APPID
  33. * @return array
  34. */
  35. public function encrypt($text, $appid)
  36. {
  37. try {
  38. $random = $this->getRandomStr();
  39. $iv = substr($this->key, 0, 16);
  40. $pkcEncoder = new PKCS7Encoder();
  41. $text = $pkcEncoder->encode($random . pack("N", strlen($text)) . $text . $appid);
  42. $encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
  43. return [ErrorCode::$OK, $encrypted];
  44. } catch (\Exception $e) {
  45. return [ErrorCode::$EncryptAESError, null];
  46. }
  47. }
  48. /**
  49. * 对密文进行解密
  50. * @param string $encrypted 需要解密的密文
  51. * @return array
  52. */
  53. public function decrypt($encrypted)
  54. {
  55. try {
  56. $iv = substr($this->key, 0, 16);
  57. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
  58. } catch (\Exception $e) {
  59. return [ErrorCode::$DecryptAESError, null];
  60. }
  61. try {
  62. $pkcEncoder = new PKCS7Encoder();
  63. $result = $pkcEncoder->decode($decrypted);
  64. if (strlen($result) < 16) {
  65. return [ErrorCode::$DecryptAESError, null];
  66. }
  67. $content = substr($result, 16, strlen($result));
  68. $len_list = unpack("N", substr($content, 0, 4));
  69. $xml_len = $len_list[1];
  70. return [0, substr($content, 4, $xml_len), substr($content, $xml_len + 4)];
  71. } catch (\Exception $e) {
  72. return [ErrorCode::$IllegalBuffer, null];
  73. }
  74. }
  75. /**
  76. * 随机生成16位字符串
  77. * @param string $str
  78. * @return string 生成的字符串
  79. */
  80. function getRandomStr($str = "")
  81. {
  82. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  83. $max = strlen($str_pol) - 1;
  84. for ($i = 0; $i < 16; $i++) {
  85. $str .= $str_pol[mt_rand(0, $max)];
  86. }
  87. return $str;
  88. }
  89. }