AesEcb.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * AES.php UTF-8
  4. *
  5. *
  6. * @date : 2021-03-10 17:29
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 9.0
  11. */
  12. namespace huoIdentify\controller;
  13. class AesEcb {
  14. /**
  15. * key
  16. *
  17. * @var string
  18. */
  19. private $key;
  20. /**
  21. * method
  22. *
  23. * @var string
  24. */
  25. private $cipher;
  26. /**
  27. * iv length
  28. *
  29. * @var int
  30. */
  31. private $ivlen;
  32. /**
  33. * AES constructor.
  34. *
  35. * @param $key
  36. * @param string $cipher
  37. */
  38. public function __construct($key, $cipher = 'aes-128-ecb') {
  39. $this->cipher = $cipher;
  40. $this->key = $key;
  41. }
  42. /**
  43. *
  44. * @param $cipherText
  45. *
  46. * @return string|false
  47. */
  48. public function decrypt($cipherText) {
  49. return openssl_decrypt($cipherText, $this->cipher, $this->key, OPENSSL_RAW_DATA);
  50. }
  51. /**
  52. *
  53. * @param string $plainText
  54. *
  55. * @return string
  56. */
  57. public function encrypt($plainText) {
  58. $result = openssl_encrypt($plainText, $this->cipher, $this->key, OPENSSL_RAW_DATA);
  59. return bin2hex($result);
  60. }
  61. }