AESUtil.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace addons\JoinPay\common\logic\util;
  3. /**
  4. * AES加解密工具类
  5. * Class AESUtil
  6. * @package utils
  7. */
  8. class AESUtil {
  9. const EBC_MODE = "AES-128-ECB";
  10. /**
  11. * AES加密,模式为:AES/ECB/PKCK7Padding
  12. * @param string $data
  13. * @param string $secKey
  14. * @return string
  15. * @throws SDKException
  16. */
  17. public static function encryptECB(string $data, string $secKey){
  18. $encrypted = openssl_encrypt($data, self::EBC_MODE, $secKey, OPENSSL_RAW_DATA);
  19. if($encrypted === false){
  20. throw new SDKException(SDKException::BIZ_ERROR, "aes加密失败");
  21. }
  22. return base64_encode($encrypted);
  23. }
  24. /**
  25. * AES解密,模式为:AES/ECB/PKCK7Padding
  26. * @param string $data
  27. * @param string $secKey
  28. * @return string
  29. * @throws SDKException
  30. */
  31. public static function decryptECB(string $data, string $secKey){
  32. $decrypted = openssl_decrypt(base64_decode($data), self::EBC_MODE, $secKey, OPENSSL_RAW_DATA);
  33. if($decrypted === false){
  34. throw new SDKException(SDKException::BIZ_ERROR, "aes解密失败");
  35. }
  36. return $decrypted;
  37. }
  38. }