PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt / RC4.php
backup / src / JetBackup / 3rdparty / phpseclib3 / Crypt Last commit date
Common 9 months ago DH 9 months ago DSA 9 months ago EC 9 months ago RSA 9 months ago .htaccess 9 months ago AES.php 9 months ago Blowfish.php 9 months ago ChaCha20.php 9 months ago DES.php 9 months ago DH.php 9 months ago DSA.php 9 months ago EC.php 9 months ago Hash.php 9 months ago PublicKeyLoader.php 9 months ago RC2.php 9 months ago RC4.php 9 months ago RSA.php 9 months ago Random.php 9 months ago Rijndael.php 9 months ago Salsa20.php 9 months ago TripleDES.php 9 months ago Twofish.php 9 months ago index.html 9 months ago web.config 9 months ago
RC4.php
265 lines
1 <?php
2
3 /**
4 * Pure-PHP implementation of RC4.
5 *
6 * Uses OpenSSL, if available/possible, and an internal implementation, otherwise
7 *
8 * PHP version 5
9 *
10 * Useful resources are as follows:
11 *
12 * - {@link http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt ARCFOUR Algorithm}
13 * - {@link http://en.wikipedia.org/wiki/RC4 - Wikipedia: RC4}
14 *
15 * RC4 is also known as ARCFOUR or ARC4. The reason is elaborated upon at Wikipedia. This class is named RC4 and not
16 * ARCFOUR or ARC4 because RC4 is how it is referred to in the SSH1 specification.
17 *
18 * Here's a short example of how to use this library:
19 * <code>
20 * <?php
21 * include 'vendor/autoload.php';
22 *
23 * $rc4 = new \phpseclib3\Crypt\RC4();
24 *
25 * $rc4->setKey('abcdefgh');
26 *
27 * $size = 10 * 1024;
28 * $plaintext = '';
29 * for ($i = 0; $i < $size; $i++) {
30 * $plaintext.= 'a';
31 * }
32 *
33 * echo $rc4->decrypt($rc4->encrypt($plaintext));
34 * ?>
35 * </code>
36 *
37 * @author Jim Wigginton <terrafrost@php.net>
38 * @copyright 2007 Jim Wigginton
39 * @license http://www.opensource.org/licenses/mit-license.html MIT License
40 * @link http://phpseclib.sourceforge.net
41 */
42
43 declare(strict_types=1);
44
45 namespace phpseclib3\Crypt;
46
47 use phpseclib3\Crypt\Common\StreamCipher;
48 use phpseclib3\Exception\LengthException;
49
50 /**
51 * Pure-PHP implementation of RC4.
52 *
53 * @author Jim Wigginton <terrafrost@php.net>
54 */
55 class RC4 extends StreamCipher
56 {
57 /**
58 * @see \phpseclib3\Crypt\RC4::_crypt()
59 */
60 public const ENCRYPT = 0;
61
62 /**
63 * @see \phpseclib3\Crypt\RC4::_crypt()
64 */
65 public const DECRYPT = 1;
66
67 /**
68 * Key Length (in bytes)
69 *
70 * @see \phpseclib3\Crypt\RC4::setKeyLength()
71 * @var int
72 */
73 protected $key_length = 128; // = 1024 bits
74
75 /**
76 * The Key
77 *
78 * @see self::setKey()
79 * @var string
80 */
81 protected $key;
82
83 /**
84 * The Key Stream for decryption and encryption
85 *
86 * @see self::setKey()
87 * @var array
88 */
89 private $stream;
90
91 /**
92 * Test for engine validity
93 *
94 * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
95 *
96 * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct()
97 */
98 protected function isValidEngineHelper(int $engine): bool
99 {
100 if ($engine == self::ENGINE_OPENSSL) {
101 if ($this->continuousBuffer) {
102 return false;
103 }
104 // quoting https://www.openssl.org/news/openssl-3.0-notes.html, OpenSSL 3.0.1
105 // "Moved all variations of the EVP ciphers CAST5, BF, IDEA, SEED, RC2, RC4, RC5, and DES to the legacy provider"
106 // in theory openssl_get_cipher_methods() should catch this but, on GitHub Actions, at least, it does not
107 if (defined('OPENSSL_VERSION_TEXT') && version_compare(preg_replace('#OpenSSL (\d+\.\d+\.\d+) .*#', '$1', OPENSSL_VERSION_TEXT), '3.0.1', '>=')) {
108 return false;
109 }
110 $this->cipher_name_openssl = 'rc4-40';
111 }
112
113 return parent::isValidEngineHelper($engine);
114 }
115
116 /**
117 * Sets the key length
118 *
119 * Keys can be between 1 and 256 bytes long.
120 *
121 * @throws LengthException if the key length is invalid
122 */
123 public function setKeyLength(int $length): void
124 {
125 if ($length < 8 || $length > 2048) {
126 throw new LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 256 bytes are supported');
127 }
128
129 $this->key_length = $length >> 3;
130
131 parent::setKeyLength($length);
132 }
133
134 /**
135 * Sets the key length
136 *
137 * Keys can be between 1 and 256 bytes long.
138 */
139 public function setKey(string $key): void
140 {
141 $length = strlen($key);
142 if ($length < 1 || $length > 256) {
143 throw new LengthException('Key size of ' . $length . ' bytes is not supported by RC4. Keys must be between 1 and 256 bytes long');
144 }
145
146 parent::setKey($key);
147 }
148
149 /**
150 * Encrypts a message.
151 *
152 * @return string $ciphertext
153 * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
154 * @see self::crypt()
155 */
156 public function encrypt(string $plaintext): string
157 {
158 if ($this->engine != self::ENGINE_INTERNAL) {
159 return parent::encrypt($plaintext);
160 }
161 return $this->crypt($plaintext, self::ENCRYPT);
162 }
163
164 /**
165 * Decrypts a message.
166 *
167 * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)).
168 * At least if the continuous buffer is disabled.
169 *
170 * @return string $plaintext
171 * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
172 * @see self::crypt()
173 */
174 public function decrypt(string $ciphertext): string
175 {
176 if ($this->engine != self::ENGINE_INTERNAL) {
177 return parent::decrypt($ciphertext);
178 }
179 return $this->crypt($ciphertext, self::DECRYPT);
180 }
181
182 /**
183 * Encrypts a block
184 */
185 protected function encryptBlock(string $in): string
186 {
187 // RC4 does not utilize this method
188 return '';
189 }
190
191 /**
192 * Decrypts a block
193 */
194 protected function decryptBlock(string $in): string
195 {
196 // RC4 does not utilize this method
197 return '';
198 }
199
200 /**
201 * Setup the key (expansion)
202 *
203 * @see \phpseclib3\Crypt\Common\SymmetricKey::_setupKey()
204 */
205 protected function setupKey(): void
206 {
207 $key = $this->key;
208 $keyLength = strlen($key);
209 $keyStream = range(0, 255);
210 $j = 0;
211 for ($i = 0; $i < 256; $i++) {
212 $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
213 $temp = $keyStream[$i];
214 $keyStream[$i] = $keyStream[$j];
215 $keyStream[$j] = $temp;
216 }
217
218 $this->stream = [];
219 $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = [
220 0, // index $i
221 0, // index $j
222 $keyStream,
223 ];
224 }
225
226 /**
227 * Encrypts or decrypts a message.
228 *
229 * @return string $text
230 * @see self::decrypt()
231 * @see self::encrypt()
232 */
233 private function crypt(string $text, int $mode): string
234 {
235 if ($this->changed) {
236 $this->setup();
237 }
238
239 $stream = &$this->stream[$mode];
240 if ($this->continuousBuffer) {
241 $i = &$stream[0];
242 $j = &$stream[1];
243 $keyStream = &$stream[2];
244 } else {
245 $i = $stream[0];
246 $j = $stream[1];
247 $keyStream = $stream[2];
248 }
249
250 $len = strlen($text);
251 for ($k = 0; $k < $len; ++$k) {
252 $i = ($i + 1) & 255;
253 $ksi = $keyStream[$i];
254 $j = ($j + $ksi) & 255;
255 $ksj = $keyStream[$j];
256
257 $keyStream[$i] = $ksj;
258 $keyStream[$j] = $ksi;
259 $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
260 }
261
262 return $text;
263 }
264 }
265