Aes.php 675 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace utils;
  3. class Aes
  4. {
  5. /**
  6. * AES ECB 加密
  7. * @param $data string 加密数据
  8. * @param $key string 秘钥
  9. * @return string
  10. */
  11. public static function encrypt($data, $key)
  12. {
  13. $data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  14. return base64_encode($data);
  15. }
  16. /**
  17. * AES ECB 解密
  18. * @param $data string 解密数据
  19. * @param $key string 秘钥
  20. * @return false|string
  21. */
  22. public static function decrypt($data, $key)
  23. {
  24. $encrypted = base64_decode($data);
  25. return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  26. }
  27. }