PhpSpreadsheet/src/PhpSpreadsheet/Reader/Xls/RC4.php

81 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2016-10-06 11:49:41 +00:00
namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
2015-06-01 20:08:48 +00:00
/**
* Copyright (c) 2006 - 2016 PhpSpreadsheet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PhpSpreadsheet
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
2015-05-12 09:22:06 +00:00
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
2015-06-01 20:08:48 +00:00
class RC4
{
2015-05-12 09:22:06 +00:00
// Context
protected $s = [];
2015-05-17 18:00:00 +00:00
protected $i = 0;
protected $j = 0;
2015-05-12 09:22:06 +00:00
/**
* RC4 stream decryption/encryption constrcutor
2015-05-17 10:11:56 +00:00
*
2015-05-12 09:22:06 +00:00
* @param string $key Encryption key/passphrase
*/
public function __construct($key)
{
$len = strlen($key);
for ($this->i = 0; $this->i < 256; ++$this->i) {
2015-05-12 09:22:06 +00:00
$this->s[$this->i] = $this->i;
}
2015-05-12 09:22:06 +00:00
$this->j = 0;
for ($this->i = 0; $this->i < 256; ++$this->i) {
2015-05-12 09:22:06 +00:00
$this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
$t = $this->s[$this->i];
$this->s[$this->i] = $this->s[$this->j];
$this->s[$this->j] = $t;
}
$this->i = $this->j = 0;
}
2015-05-12 09:22:06 +00:00
/**
* Symmetric decryption/encryption function
2015-05-17 10:11:56 +00:00
*
2015-05-12 09:22:06 +00:00
* @param string $data Data to encrypt/decrypt
2015-05-17 10:11:56 +00:00
*
2015-05-12 09:22:06 +00:00
* @return string
*/
public function RC4($data)
{
$len = strlen($data);
for ($c = 0; $c < $len; ++$c) {
2015-05-12 09:22:06 +00:00
$this->i = ($this->i + 1) % 256;
$this->j = ($this->j + $this->s[$this->i]) % 256;
$t = $this->s[$this->i];
$this->s[$this->i] = $this->s[$this->j];
$this->s[$this->j] = $t;
2015-05-12 09:22:06 +00:00
$t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
2015-05-12 09:22:06 +00:00
$data[$c] = chr(ord($data[$c]) ^ $this->s[$t]);
}
2015-05-12 09:22:06 +00:00
return $data;
}
}