RC4.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Reader_Excel5_RC4
  8. {
  9. // Context
  10. protected $s = array();
  11. protected $i = 0;
  12. protected $j = 0;
  13. /**
  14. * RC4 stream decryption/encryption constrcutor
  15. *
  16. * @param string $key Encryption key/passphrase
  17. */
  18. public function __construct($key)
  19. {
  20. $len = strlen($key);
  21. for ($this->i = 0; $this->i < 256; $this->i++) {
  22. $this->s[$this->i] = $this->i;
  23. }
  24. $this->j = 0;
  25. for ($this->i = 0; $this->i < 256; $this->i++) {
  26. $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
  27. $t = $this->s[$this->i];
  28. $this->s[$this->i] = $this->s[$this->j];
  29. $this->s[$this->j] = $t;
  30. }
  31. $this->i = $this->j = 0;
  32. }
  33. /**
  34. * Symmetric decryption/encryption function
  35. *
  36. * @param string $data Data to encrypt/decrypt
  37. *
  38. * @return string
  39. */
  40. public function RC4($data)
  41. {
  42. $len = strlen($data);
  43. for ($c = 0; $c < $len; $c++) {
  44. $this->i = ($this->i + 1) % 256;
  45. $this->j = ($this->j + $this->s[$this->i]) % 256;
  46. $t = $this->s[$this->i];
  47. $this->s[$this->i] = $this->s[$this->j];
  48. $this->s[$this->j] = $t;
  49. $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
  50. $data[$c] = chr(ord($data[$c]) ^ $this->s[$t]);
  51. }
  52. return $data;
  53. }
  54. }