Encryptor.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\MiniProgram;
  16. use App\Account\Exceptions\MiniProgramLoginException;
  17. use ByteDance\Kernel\Encryptor as BaseEncryptor;
  18. use ByteDance\Kernel\Exceptions\DecryptException;
  19. use ByteDance\Kernel\Support\AES;
  20. /**
  21. * Class Encryptor.
  22. *
  23. * @author mingyoung <mingyoungcheung@gmail.com>
  24. */
  25. class Encryptor extends BaseEncryptor
  26. {
  27. /**
  28. * Decrypt data.
  29. *
  30. * @param string $sessionKey
  31. * @param string $iv
  32. * @param string $encrypted
  33. *
  34. * @return array
  35. */
  36. public function decryptData(string $sessionKey, string $iv, string $encrypted): array
  37. {
  38. $decrypted = AES::decrypt(
  39. base64_decode($encrypted, false), base64_decode($sessionKey, false), base64_decode($iv, false)
  40. );
  41. $decrypted = json_decode($this->pkcs7Unpad($decrypted), true);
  42. if (!$decrypted) {
  43. throw new DecryptException('The given payload is invalid.');
  44. }
  45. return $decrypted;
  46. }
  47. }