DES.php 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Common\Library;
  3. class DES
  4. {
  5. private $key;
  6. private $encryptor;
  7. public function __construct($key)
  8. {
  9. $this->key = $this->complement($key);
  10. $this->encryptor = new Encryptor($key);
  11. }
  12. public function complement($key)
  13. {
  14. if (strlen($key) < 8) {
  15. $len = strlen($key) % 8;
  16. if ($len == 0) {
  17. return $key;
  18. } else {
  19. $j = 8 - $len;
  20. for ($i = 0; $i < $j; $i++) {
  21. $key .= "\0";
  22. }
  23. return $key;
  24. }
  25. } else {
  26. return $key;
  27. }
  28. }
  29. /**
  30. * @param $encrypt
  31. * @return string
  32. */
  33. public function encrypt($encrypt)
  34. {
  35. return $this->encryptor->encrypt($encrypt);
  36. }
  37. /**
  38. * @param $decrypt
  39. * @return string
  40. */
  41. public function decrypt($decrypt)
  42. {
  43. return $this->encryptor->decrypt($decrypt);
  44. }
  45. }