Aes.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace openssl;
  3. class Aes{
  4. /**
  5. * CI_Aes cipher
  6. * @var string
  7. */
  8. protected $cipher = 'aes-128-ecb';
  9. /**
  10. * CI_Aes key
  11. *
  12. * @var string
  13. */
  14. protected $key;
  15. /**
  16. * CI_Aes constructor
  17. * @param string $key Configuration parameter
  18. */
  19. public function __construct($key=null){
  20. $this->key = $key;
  21. }
  22. /**
  23. * Initialize
  24. *
  25. * @param array $params Configuration parameters
  26. * @return CI_Encryption
  27. */
  28. public function initialize($params)
  29. {
  30. if (!empty($params) && is_array($params)) {
  31. foreach ($params as $key => $val) {
  32. $this->$key = $val;
  33. }
  34. }
  35. }
  36. /**
  37. * Encrypt
  38. *
  39. * @param string $data Input data
  40. * @return string
  41. */
  42. public function encrypt($data) {
  43. $endata = openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA);
  44. return bin2hex($endata);
  45. }
  46. /**
  47. * Decrypt
  48. *
  49. * @param string $data Encrypted data
  50. * @return string
  51. */
  52. public function decrypt($data) {
  53. $encrypted = hex2bin($data);
  54. return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA);
  55. }
  56. }