ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RC4.php
Go to the documentation of this file.
1<?php
2
45namespace phpseclib\Crypt;
46
48
56class RC4 extends Base
57{
62 const ENCRYPT = 0;
63 const DECRYPT = 1;
76 var $block_size = 0;
77
85 var $key_length = 128; // = 1024 bits
86
94 var $cipher_name_mcrypt = 'arcfour';
95
103 var $use_inline_crypt = false; // currently not available
104
112 var $key = "\0";
113
122
132 function __construct()
133 {
134 parent::__construct(Base::MODE_STREAM);
135 }
136
148 {
149 switch ($engine) {
151 switch (strlen($this->key)) {
152 case 5:
153 $this->cipher_name_openssl = 'rc4-40';
154 break;
155 case 8:
156 $this->cipher_name_openssl = 'rc4-64';
157 break;
158 case 16:
159 $this->cipher_name_openssl = 'rc4';
160 break;
161 default:
162 return false;
163 }
164 }
165
166 return parent::isValidEngine($engine);
167 }
168
188 function setIV($iv)
189 {
190 }
191
200 function setKeyLength($length)
201 {
202 if ($length < 8) {
203 $this->key_length = 1;
204 } elseif ($length > 2048) {
205 $this->key_length = 248;
206 } else {
207 $this->key_length = $length >> 3;
208 }
209
210 parent::setKeyLength($length);
211 }
212
222 function encrypt($plaintext)
223 {
224 if ($this->engine != Base::ENGINE_INTERNAL) {
225 return parent::encrypt($plaintext);
226 }
227 return $this->_crypt($plaintext, self::ENCRYPT);
228 }
229
242 function decrypt($ciphertext)
243 {
244 if ($this->engine != Base::ENGINE_INTERNAL) {
245 return parent::decrypt($ciphertext);
246 }
247 return $this->_crypt($ciphertext, self::DECRYPT);
248 }
249
257 {
258 // RC4 does not utilize this method
259 }
260
268 {
269 // RC4 does not utilize this method
270 }
271
278 function _setupKey()
279 {
281 $keyLength = strlen($key);
282 $keyStream = range(0, 255);
283 $j = 0;
284 for ($i = 0; $i < 256; $i++) {
285 $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
286 $temp = $keyStream[$i];
287 $keyStream[$i] = $keyStream[$j];
288 $keyStream[$j] = $temp;
289 }
290
291 $this->stream = array();
292 $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array(
293 0, // index $i
294 0, // index $j
295 $keyStream
296 );
297 }
298
309 function _crypt($text, $mode)
310 {
311 if ($this->changed) {
312 $this->_setup();
313 $this->changed = false;
314 }
315
316 $stream = &$this->stream[$mode];
317 if ($this->continuousBuffer) {
318 $i = &$stream[0];
319 $j = &$stream[1];
320 $keyStream = &$stream[2];
321 } else {
322 $i = $stream[0];
323 $j = $stream[1];
324 $keyStream = $stream[2];
325 }
326
327 $len = strlen($text);
328 for ($k = 0; $k < $len; ++$k) {
329 $i = ($i + 1) & 255;
330 $ksi = $keyStream[$i];
331 $j = ($j + $ksi) & 255;
332 $ksj = $keyStream[$j];
333
334 $keyStream[$i] = $ksj;
335 $keyStream[$j] = $ksi;
336 $text[$k] = $text[$k] ^ chr($keyStream[($ksj + $ksi) & 255]);
337 }
338
339 return $text;
340 }
341}
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
An exception for terminatinating execution or to throw for unit testing.
const ENGINE_OPENSSL
Base value for the mcrypt implementation $engine switch.
Definition: Base.php:117
const ENGINE_INTERNAL
#+ @access private
Definition: Base.php:109
_setup()
Setup the self::ENGINE_INTERNAL $engine.
Definition: Base.php:1743
const MODE_STREAM
Encrypt / decrypt using streaming mode.
Definition: Base.php:90
isValidEngine($engine)
Test for engine validity.
Definition: RC4.php:147
_decryptBlock($in)
Decrypts a block.
Definition: RC4.php:267
_crypt($text, $mode)
Encrypts or decrypts a message.
Definition: RC4.php:309
__construct()
Default Constructor.
Definition: RC4.php:132
_setupKey()
Setup the key (expansion)
Definition: RC4.php:278
encrypt($plaintext)
Encrypts a message.
Definition: RC4.php:222
decrypt($ciphertext)
Decrypts a message.
Definition: RC4.php:242
setKeyLength($length)
Sets the key length.
Definition: RC4.php:200
const DECRYPT
Definition: RC4.php:63
_encryptBlock($in)
Encrypts a block.
Definition: RC4.php:256
const ENCRYPT
#+ @access private
Definition: RC4.php:62
setIV($iv)
Dummy function.
Definition: RC4.php:188
$i
Definition: disco.tpl.php:19
Base Class for all \phpseclib\Crypt* cipher classes.
Pure-PHP implementation of RC4.
Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic...
Definition: AES.php:50
$text
Definition: errorreport.php:18