RC4.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader_Excel5
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Reader_Excel5_RC4
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Reader_Excel5
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Reader_Excel5_RC4
  35. {
  36. // Context
  37. var $s = array();
  38. var $i = 0;
  39. var $j = 0;
  40. /**
  41. * RC4 stream decryption/encryption constrcutor
  42. *
  43. * @param string $key Encryption key/passphrase
  44. */
  45. public function __construct($key)
  46. {
  47. $len = strlen($key);
  48. for ($this->i = 0; $this->i < 256; $this->i++) {
  49. $this->s[$this->i] = $this->i;
  50. }
  51. $this->j = 0;
  52. for ($this->i = 0; $this->i < 256; $this->i++) {
  53. $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
  54. $t = $this->s[$this->i];
  55. $this->s[$this->i] = $this->s[$this->j];
  56. $this->s[$this->j] = $t;
  57. }
  58. $this->i = $this->j = 0;
  59. }
  60. /**
  61. * Symmetric decryption/encryption function
  62. *
  63. * @param string $data Data to encrypt/decrypt
  64. *
  65. * @return string
  66. */
  67. public function RC4($data)
  68. {
  69. $len = strlen($data);
  70. for ($c = 0; $c < $len; $c++) {
  71. $this->i = ($this->i + 1) % 256;
  72. $this->j = ($this->j + $this->s[$this->i]) % 256;
  73. $t = $this->s[$this->i];
  74. $this->s[$this->i] = $this->s[$this->j];
  75. $this->s[$this->j] = $t;
  76. $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
  77. $data[$c] = chr(ord($data[$c]) ^ $this->s[$t]);
  78. }
  79. return $data;
  80. }
  81. }