Encryptor.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\MiniProgram;
  11. use EasyWeChat\Kernel\Encryptor as BaseEncryptor;
  12. use EasyWeChat\Kernel\Exceptions\DecryptException;
  13. use EasyWeChat\Kernel\Support\AES;
  14. /**
  15. * Class Encryptor.
  16. *
  17. * @author mingyoung <mingyoungcheung@gmail.com>
  18. */
  19. class Encryptor extends BaseEncryptor
  20. {
  21. /**
  22. * Decrypt data.
  23. *
  24. * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
  25. */
  26. public function decryptData(string $sessionKey, string $iv, string $encrypted): array
  27. {
  28. $decrypted = AES::decrypt(
  29. base64_decode($encrypted, false),
  30. base64_decode($sessionKey, false),
  31. base64_decode($iv, false)
  32. );
  33. $decrypted = json_decode($decrypted, true);
  34. if (!$decrypted) {
  35. throw new DecryptException('The given payload is invalid.');
  36. }
  37. return $decrypted;
  38. }
  39. }