Aes.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Aes.php UTF-8
  4. * AES 对称加密解密
  5. *
  6. * @date : 2017/7/17 22:01
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : guxiannong <gxn@huosdk.com>
  10. * @version : HUOSDK 7.2
  11. */
  12. namespace huolib\tool;
  13. class Aes {
  14. /**
  15. * 与doDecrypt 加密
  16. *
  17. * @param string $str
  18. * @param array $option
  19. *
  20. * @return string
  21. */
  22. public static function doEncrypt($str = '', $option = array()) {
  23. $aes_type = isset($option['aes_type']) ? $option['aes_type'] : 'aes-128-cbc';
  24. $key = isset($option['key']) ? $option['key'] : '1Q2W3E4R5T6Y7U8I9O0P';
  25. $iv = isset($option['iv']) ? $option['iv'] : '9876543219638521';
  26. $aes_option = isset($option['aes_option']) ? $option['aes_option'] : OPENSSL_RAW_DATA;
  27. return bin2hex(openssl_encrypt($str, $aes_type, $key, $aes_option, $iv));
  28. }
  29. /**
  30. * 与doEncrypt 配套的解密
  31. *
  32. * @param string $str
  33. * @param array $option
  34. *
  35. * @return string
  36. */
  37. public static function doDecrypt($str = '', $option = array()) {
  38. $aes_type = isset($option['aes_type']) ? $option['aes_type'] : 'aes-128-cbc';
  39. $key = isset($option['key']) ? $option['key'] : '1Q2W3E4R5T6Y7U8I9O0P';
  40. $iv = isset($option['iv']) ? $option['iv'] : '9876543219638521';
  41. $aes_option = isset($option['aes_option']) ? $option['aes_option'] : OPENSSL_RAW_DATA;
  42. return openssl_decrypt(pack('H*', $str), $aes_type, $key, $aes_option, $iv);
  43. }
  44. }