| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Reader\Xls; |
| 4 |
|
| 5 |
class RC4 |
| 6 |
{ |
| 7 |
// Context |
| 8 |
protected $s = []; |
| 9 |
|
| 10 |
protected $i = 0; |
| 11 |
|
| 12 |
protected $j = 0; |
| 13 |
|
| 14 |
/** |
| 15 |
* RC4 stream decryption/encryption constrcutor. |
| 16 |
* |
| 17 |
* @param string $key Encryption key/passphrase |
| 18 |
*/ |
| 19 |
public function __construct($key) |
| 20 |
{ |
| 21 |
$len = strlen($key); |
| 22 |
|
| 23 |
for ($this->i = 0; $this->i < 256; ++$this->i) { |
| 24 |
$this->s[$this->i] = $this->i; |
| 25 |
} |
| 26 |
|
| 27 |
$this->j = 0; |
| 28 |
for ($this->i = 0; $this->i < 256; ++$this->i) { |
| 29 |
$this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256; |
| 30 |
$t = $this->s[$this->i]; |
| 31 |
$this->s[$this->i] = $this->s[$this->j]; |
| 32 |
$this->s[$this->j] = $t; |
| 33 |
} |
| 34 |
$this->i = $this->j = 0; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Symmetric decryption/encryption function. |
| 39 |
* |
| 40 |
* @param string $data Data to encrypt/decrypt |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function RC4($data) |
| 45 |
{ |
| 46 |
$len = strlen($data); |
| 47 |
for ($c = 0; $c < $len; ++$c) { |
| 48 |
$this->i = ($this->i + 1) % 256; |
| 49 |
$this->j = ($this->j + $this->s[$this->i]) % 256; |
| 50 |
$t = $this->s[$this->i]; |
| 51 |
$this->s[$this->i] = $this->s[$this->j]; |
| 52 |
$this->s[$this->j] = $t; |
| 53 |
|
| 54 |
$t = ($this->s[$this->i] + $this->s[$this->j]) % 256; |
| 55 |
|
| 56 |
$data[$c] = chr(ord($data[$c]) ^ $this->s[$t]); |
| 57 |
} |
| 58 |
|
| 59 |
return $data; |
| 60 |
} |
| 61 |
} |
| 62 |
|