CryptAes.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Aes加解密类
  4. * @author Len
  5. * @since 2018-01-09
  6. */
  7. namespace crypt;
  8. class CryptAes extends CryptAbstract
  9. {
  10. /**
  11. * 算法模式
  12. */
  13. const CIPHER = 'AES-128-CBC';
  14. public static $cryptName = 'Aes';
  15. public function __construct($config = [])
  16. {
  17. if (!empty($config)) {
  18. $this->_config = $config;
  19. } else {
  20. parent::__construct();
  21. }
  22. }
  23. /**
  24. * AES加密
  25. * @param string $input 明文
  26. * @return string
  27. */
  28. public function encrypt($input)
  29. {
  30. return base64_encode(openssl_encrypt(
  31. $input,
  32. self::CIPHER,
  33. $this->_config['key'],
  34. OPENSSL_RAW_DATA,
  35. $this->_config['iv']
  36. ));
  37. }
  38. /**
  39. * AES解密
  40. * @param string $encrypted 密文
  41. * @return string
  42. */
  43. public function decrypt($encrypted)
  44. {
  45. return openssl_decrypt(
  46. base64_decode($encrypted),
  47. self::CIPHER,
  48. $this->_config['key'],
  49. OPENSSL_RAW_DATA,
  50. $this->_config['iv']
  51. );
  52. }
  53. }