DecryptAes.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WePayV3\Contracts;
  14. use WeChat\Exceptions\InvalidArgumentException;
  15. use WeChat\Exceptions\InvalidDecryptException;
  16. /**
  17. * Aes 解密工具类
  18. * Class DecryptAes
  19. * @package WePayV3\Contracts
  20. */
  21. class DecryptAes
  22. {
  23. private $aesKey;
  24. const KEY_LENGTH_BYTE = 32;
  25. const AUTH_TAG_LENGTH_BYTE = 16;
  26. /**
  27. * Constructor
  28. * @param string $aesKey
  29. */
  30. public function __construct($aesKey)
  31. {
  32. if (strlen($aesKey) != self::KEY_LENGTH_BYTE) {
  33. throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
  34. }
  35. $this->aesKey = $aesKey;
  36. }
  37. /**
  38. * Decrypt AEAD_AES_256_GCM ciphertext
  39. * @param string $associatedData AES GCM additional authentication data
  40. * @param string $nonceStr AES GCM nonce
  41. * @param string $ciphertext AES GCM cipher text
  42. * @return string|bool Decrypted string on success or FALSE on failure
  43. * @throws InvalidDecryptException
  44. */
  45. public function decryptToString($associatedData, $nonceStr, $ciphertext)
  46. {
  47. $ciphertext = \base64_decode($ciphertext);
  48. if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {
  49. return false;
  50. }
  51. try {
  52. // ext-sodium (default installed on >= PHP 7.2)
  53. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') && \sodium_crypto_aead_aes256gcm_is_available()) {
  54. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);
  55. }
  56. // ext-libsodium (need install libsodium-php 1.x via pecl)
  57. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') && \Sodium\crypto_aead_aes256gcm_is_available()) {
  58. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $this->aesKey);
  59. }
  60. // openssl (PHP >= 7.1 support AEAD)
  61. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  62. $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
  63. $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);
  64. return \openssl_decrypt($ctext, 'aes-256-gcm', $this->aesKey, \OPENSSL_RAW_DATA, $nonceStr, $authTag, $associatedData);
  65. }
  66. } catch (\Exception $exception) {
  67. throw new InvalidDecryptException($exception->getMessage(), $exception->getCode());
  68. } catch (\SodiumException $exception) {
  69. throw new InvalidDecryptException($exception->getMessage(), $exception->getCode());
  70. }
  71. throw new InvalidDecryptException('AEAD_AES_256_GCM 需要 PHP 7.1 以上或者安装 libsodium-php');
  72. }
  73. }