source: sourcecode/application/libraries/PHPExcel/Reader/Excel5/RC4.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 2.4 KB
Line 
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/**
29 * PHPExcel_Reader_Excel5_RC4
30 *
31 * @category    PHPExcel
32 * @package             PHPExcel_Reader_Excel5
33 * @copyright   Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
34 */
35class PHPExcel_Reader_Excel5_RC4
36{
37        // Context
38        var $s = array();
39        var $i = 0;
40        var $j = 0;
41
42        /**
43         * RC4 stream decryption/encryption constrcutor
44         *
45         * @param string $key Encryption key/passphrase
46         */
47        public function __construct($key)
48        {
49                $len = strlen($key);
50
51                for ($this->i = 0; $this->i < 256; $this->i++) {
52                        $this->s[$this->i] = $this->i;
53                }
54
55                $this->j = 0;
56                for ($this->i = 0; $this->i < 256; $this->i++) {
57                        $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
58                        $t = $this->s[$this->i];
59                        $this->s[$this->i] = $this->s[$this->j];
60                        $this->s[$this->j] = $t;
61                }
62                $this->i = $this->j = 0;
63        }
64
65        /**
66         * Symmetric decryption/encryption function
67         *
68         * @param string $data Data to encrypt/decrypt
69         *
70         * @return string
71         */
72        public function RC4($data)
73        {
74                $len = strlen($data);
75                for ($c = 0; $c < $len; $c++) {
76                        $this->i = ($this->i + 1) % 256;
77                        $this->j = ($this->j + $this->s[$this->i]) % 256;
78                        $t = $this->s[$this->i];
79                        $this->s[$this->i] = $this->s[$this->j];
80                        $this->s[$this->j] = $t;
81
82                        $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
83
84                        $data[$c] = chr(ord($data[$c]) ^ $this->s[$t]);
85                }
86                return $data;
87        }
88}
Note: See TracBrowser for help on using the repository browser.