Rev 846 | Rev 1042 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 846 | Rev 874 | ||
---|---|---|---|
Line 24... | Line 24... | ||
24 | * |
24 | * |
25 | * echo $rc2->decrypt($rc2->encrypt($plaintext)); |
25 | * echo $rc2->decrypt($rc2->encrypt($plaintext)); |
26 | * ?> |
26 | * ?> |
27 | * </code> |
27 | * </code> |
28 | * |
28 | * |
- | 29 | * @category Crypt |
|
- | 30 | * @package RC2 |
|
29 | * @author Patrick Monnerat <pm@datasphere.ch> |
31 | * @author Patrick Monnerat <pm@datasphere.ch> |
30 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
32 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
31 | * @link http://phpseclib.sourceforge.net |
33 | * @link http://phpseclib.sourceforge.net |
32 | */ |
34 | */ |
33 | 35 | ||
Line 37... | Line 39... | ||
37 | use phpseclib3\Exception\BadModeException; |
39 | use phpseclib3\Exception\BadModeException; |
38 | 40 | ||
39 | /** |
41 | /** |
40 | * Pure-PHP implementation of RC2. |
42 | * Pure-PHP implementation of RC2. |
41 | * |
43 | * |
- | 44 | * @package RC2 |
|
- | 45 | * @access public |
|
42 | */ |
46 | */ |
43 | class RC2 extends BlockCipher |
47 | class RC2 extends BlockCipher |
44 | { |
48 | { |
45 | /** |
49 | /** |
46 | * Block Length of the cipher |
50 | * Block Length of the cipher |
47 | * |
51 | * |
48 | * @see \phpseclib3\Crypt\Common\SymmetricKey::block_size |
52 | * @see \phpseclib3\Crypt\Common\SymmetricKey::block_size |
49 | * @var int |
53 | * @var int |
- | 54 | * @access private |
|
50 | */ |
55 | */ |
51 | protected $block_size = 8; |
56 | protected $block_size = 8; |
52 | 57 | ||
53 | /** |
58 | /** |
54 | * The Key |
59 | * The Key |
55 | * |
60 | * |
56 | * @see \phpseclib3\Crypt\Common\SymmetricKey::key |
61 | * @see \phpseclib3\Crypt\Common\SymmetricKey::key |
57 | * @see self::setKey() |
62 | * @see self::setKey() |
58 | * @var string |
63 | * @var string |
- | 64 | * @access private |
|
59 | */ |
65 | */ |
60 | protected $key; |
66 | protected $key; |
61 | 67 | ||
62 | /** |
68 | /** |
63 | * The Original (unpadded) Key |
69 | * The Original (unpadded) Key |
Line 65... | Line 71... | ||
65 | * @see \phpseclib3\Crypt\Common\SymmetricKey::key |
71 | * @see \phpseclib3\Crypt\Common\SymmetricKey::key |
66 | * @see self::setKey() |
72 | * @see self::setKey() |
67 | * @see self::encrypt() |
73 | * @see self::encrypt() |
68 | * @see self::decrypt() |
74 | * @see self::decrypt() |
69 | * @var string |
75 | * @var string |
- | 76 | * @access private |
|
70 | */ |
77 | */ |
71 | private $orig_key; |
78 | private $orig_key; |
72 | 79 | ||
73 | /** |
80 | /** |
74 | * Don't truncate / null pad key |
81 | * Don't truncate / null pad key |
75 | * |
82 | * |
76 | * @see \phpseclib3\Crypt\Common\SymmetricKey::clearBuffers() |
83 | * @see \phpseclib3\Crypt\Common\SymmetricKey::clearBuffers() |
77 | * @var bool |
84 | * @var bool |
- | 85 | * @access private |
|
78 | */ |
86 | */ |
79 | private $skip_key_adjustment = true; |
87 | private $skip_key_adjustment = true; |
80 | 88 | ||
81 | /** |
89 | /** |
82 | * Key Length (in bytes) |
90 | * Key Length (in bytes) |
83 | * |
91 | * |
84 | * @see \phpseclib3\Crypt\RC2::setKeyLength() |
92 | * @see \phpseclib3\Crypt\RC2::setKeyLength() |
85 | * @var int |
93 | * @var int |
- | 94 | * @access private |
|
86 | */ |
95 | */ |
87 | protected $key_length = 16; // = 128 bits |
96 | protected $key_length = 16; // = 128 bits |
88 | 97 | ||
89 | /** |
98 | /** |
90 | * The mcrypt specific name of the cipher |
99 | * The mcrypt specific name of the cipher |
91 | * |
100 | * |
92 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt |
101 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt |
93 | * @var string |
102 | * @var string |
- | 103 | * @access private |
|
94 | */ |
104 | */ |
95 | protected $cipher_name_mcrypt = 'rc2'; |
105 | protected $cipher_name_mcrypt = 'rc2'; |
96 | 106 | ||
97 | /** |
107 | /** |
98 | * Optimizing value while CFB-encrypting |
108 | * Optimizing value while CFB-encrypting |
99 | * |
109 | * |
100 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len |
110 | * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len |
101 | * @var int |
111 | * @var int |
- | 112 | * @access private |
|
102 | */ |
113 | */ |
103 | protected $cfb_init_len = 500; |
114 | protected $cfb_init_len = 500; |
104 | 115 | ||
105 | /** |
116 | /** |
106 | * The key length in bits. |
117 | * The key length in bits. |
Line 110... | Line 121... | ||
110 | * {@internal Changing this value after setting the key has no effect.} |
121 | * {@internal Changing this value after setting the key has no effect.} |
111 | * |
122 | * |
112 | * @see self::setKeyLength() |
123 | * @see self::setKeyLength() |
113 | * @see self::setKey() |
124 | * @see self::setKey() |
114 | * @var int |
125 | * @var int |
- | 126 | * @access private |
|
115 | */ |
127 | */ |
116 | private $default_key_length = 1024; |
128 | private $default_key_length = 1024; |
117 | 129 | ||
118 | /** |
130 | /** |
119 | * The key length in bits. |
131 | * The key length in bits. |
Line 121... | Line 133... | ||
121 | * {@internal Should be in range [1..1024].} |
133 | * {@internal Should be in range [1..1024].} |
122 | * |
134 | * |
123 | * @see self::isValidEnine() |
135 | * @see self::isValidEnine() |
124 | * @see self::setKey() |
136 | * @see self::setKey() |
125 | * @var int |
137 | * @var int |
- | 138 | * @access private |
|
126 | */ |
139 | */ |
127 | private $current_key_length; |
140 | private $current_key_length; |
128 | 141 | ||
129 | /** |
142 | /** |
130 | * The Key Schedule |
143 | * The Key Schedule |
131 | * |
144 | * |
132 | * @see self::setupKey() |
145 | * @see self::setupKey() |
133 | * @var array |
146 | * @var array |
- | 147 | * @access private |
|
134 | */ |
148 | */ |
135 | private $keys; |
149 | private $keys; |
136 | 150 | ||
137 | /** |
151 | /** |
138 | * Key expansion randomization table. |
152 | * Key expansion randomization table. |
139 | * Twice the same 256-value sequence to save a modulus in key expansion. |
153 | * Twice the same 256-value sequence to save a modulus in key expansion. |
140 | * |
154 | * |
141 | * @see self::setKey() |
155 | * @see self::setKey() |
142 | * @var array |
156 | * @var array |
- | 157 | * @access private |
|
143 | */ |
158 | */ |
144 | private static $pitable = [ |
159 | private static $pitable = [ |
145 | 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, |
160 | 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED, |
146 | 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, |
161 | 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D, |
147 | 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, |
162 | 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E, |
Line 211... | Line 226... | ||
211 | /** |
226 | /** |
212 | * Inverse key expansion randomization table. |
227 | * Inverse key expansion randomization table. |
213 | * |
228 | * |
214 | * @see self::setKey() |
229 | * @see self::setKey() |
215 | * @var array |
230 | * @var array |
- | 231 | * @access private |
|
216 | */ |
232 | */ |
217 | private static $invpitable = [ |
233 | private static $invpitable = [ |
218 | 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66, |
234 | 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66, |
219 | 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4, |
235 | 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4, |
220 | 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20, |
236 | 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20, |
Line 251... | Line 267... | ||
251 | 267 | ||
252 | /** |
268 | /** |
253 | * Default Constructor. |
269 | * Default Constructor. |
254 | * |
270 | * |
255 | * @param string $mode |
271 | * @param string $mode |
- | 272 | * @access public |
|
256 | * @throws \InvalidArgumentException if an invalid / unsupported mode is provided |
273 | * @throws \InvalidArgumentException if an invalid / unsupported mode is provided |
257 | */ |
274 | */ |
258 | public function __construct($mode) |
275 | public function __construct($mode) |
259 | { |
276 | { |
260 | parent::__construct($mode); |
277 | parent::__construct($mode); |
Line 269... | Line 286... | ||
269 | * |
286 | * |
270 | * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine() |
287 | * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine() |
271 | * |
288 | * |
272 | * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct() |
289 | * @see \phpseclib3\Crypt\Common\SymmetricKey::__construct() |
273 | * @param int $engine |
290 | * @param int $engine |
- | 291 | * @access protected |
|
274 | * @return bool |
292 | * @return bool |
275 | */ |
293 | */ |
276 | protected function isValidEngineHelper($engine) |
294 | protected function isValidEngineHelper($engine) |
277 | { |
295 | { |
278 | switch ($engine) { |
296 | switch ($engine) { |
Line 292... | Line 310... | ||
292 | * |
310 | * |
293 | * Valid key lengths are 8 to 1024. |
311 | * Valid key lengths are 8 to 1024. |
294 | * Calling this function after setting the key has no effect until the next |
312 | * Calling this function after setting the key has no effect until the next |
295 | * \phpseclib3\Crypt\RC2::setKey() call. |
313 | * \phpseclib3\Crypt\RC2::setKey() call. |
296 | * |
314 | * |
- | 315 | * @access public |
|
297 | * @param int $length in bits |
316 | * @param int $length in bits |
298 | * @throws \LengthException if the key length isn't supported |
317 | * @throws \LengthException if the key length isn't supported |
299 | */ |
318 | */ |
300 | public function setKeyLength($length) |
319 | public function setKeyLength($length) |
301 | { |
320 | { |
Line 308... | Line 327... | ||
308 | } |
327 | } |
309 | 328 | ||
310 | /** |
329 | /** |
311 | * Returns the current key length |
330 | * Returns the current key length |
312 | * |
331 | * |
- | 332 | * @access public |
|
313 | * @return int |
333 | * @return int |
314 | */ |
334 | */ |
315 | public function getKeyLength() |
335 | public function getKeyLength() |
316 | { |
336 | { |
317 | return $this->current_key_length; |
337 | return $this->current_key_length; |
Line 324... | Line 344... | ||
324 | * strlen($key) <= 128), however, we only use the first 128 bytes if $key |
344 | * strlen($key) <= 128), however, we only use the first 128 bytes if $key |
325 | * has more then 128 bytes in it, and set $key to a single null byte if |
345 | * has more then 128 bytes in it, and set $key to a single null byte if |
326 | * it is empty. |
346 | * it is empty. |
327 | * |
347 | * |
328 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey() |
348 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey() |
- | 349 | * @access public |
|
329 | * @param string $key |
350 | * @param string $key |
330 | * @param int|boolean $t1 optional Effective key length in bits. |
351 | * @param int|boolean $t1 optional Effective key length in bits. |
331 | * @throws \LengthException if the key length isn't supported |
352 | * @throws \LengthException if the key length isn't supported |
332 | */ |
353 | */ |
333 | public function setKey($key, $t1 = false) |
354 | public function setKey($key, $t1 = false) |
Line 385... | Line 406... | ||
385 | * Encrypts a message. |
406 | * Encrypts a message. |
386 | * |
407 | * |
387 | * Mostly a wrapper for \phpseclib3\Crypt\Common\SymmetricKey::encrypt, with some additional OpenSSL handling code |
408 | * Mostly a wrapper for \phpseclib3\Crypt\Common\SymmetricKey::encrypt, with some additional OpenSSL handling code |
388 | * |
409 | * |
389 | * @see self::decrypt() |
410 | * @see self::decrypt() |
- | 411 | * @access public |
|
390 | * @param string $plaintext |
412 | * @param string $plaintext |
391 | * @return string $ciphertext |
413 | * @return string $ciphertext |
392 | */ |
414 | */ |
393 | public function encrypt($plaintext) |
415 | public function encrypt($plaintext) |
394 | { |
416 | { |
Line 407... | Line 429... | ||
407 | * Decrypts a message. |
429 | * Decrypts a message. |
408 | * |
430 | * |
409 | * Mostly a wrapper for \phpseclib3\Crypt\Common\SymmetricKey::decrypt, with some additional OpenSSL handling code |
431 | * Mostly a wrapper for \phpseclib3\Crypt\Common\SymmetricKey::decrypt, with some additional OpenSSL handling code |
410 | * |
432 | * |
411 | * @see self::encrypt() |
433 | * @see self::encrypt() |
- | 434 | * @access public |
|
412 | * @param string $ciphertext |
435 | * @param string $ciphertext |
413 | * @return string $plaintext |
436 | * @return string $plaintext |
414 | */ |
437 | */ |
415 | public function decrypt($ciphertext) |
438 | public function decrypt($ciphertext) |
416 | { |
439 | { |
Line 428... | Line 451... | ||
428 | /** |
451 | /** |
429 | * Encrypts a block |
452 | * Encrypts a block |
430 | * |
453 | * |
431 | * @see \phpseclib3\Crypt\Common\SymmetricKey::encryptBlock() |
454 | * @see \phpseclib3\Crypt\Common\SymmetricKey::encryptBlock() |
432 | * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt() |
455 | * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt() |
- | 456 | * @access private |
|
433 | * @param string $in |
457 | * @param string $in |
434 | * @return string |
458 | * @return string |
435 | */ |
459 | */ |
436 | protected function encryptBlock($in) |
460 | protected function encryptBlock($in) |
437 | { |
461 | { |
Line 472... | Line 496... | ||
472 | /** |
496 | /** |
473 | * Decrypts a block |
497 | * Decrypts a block |
474 | * |
498 | * |
475 | * @see \phpseclib3\Crypt\Common\SymmetricKey::decryptBlock() |
499 | * @see \phpseclib3\Crypt\Common\SymmetricKey::decryptBlock() |
476 | * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt() |
500 | * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt() |
- | 501 | * @access private |
|
477 | * @param string $in |
502 | * @param string $in |
478 | * @return string |
503 | * @return string |
479 | */ |
504 | */ |
480 | protected function decryptBlock($in) |
505 | protected function decryptBlock($in) |
481 | { |
506 | { |
Line 515... | Line 540... | ||
515 | 540 | ||
516 | /** |
541 | /** |
517 | * Creates the key schedule |
542 | * Creates the key schedule |
518 | * |
543 | * |
519 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey() |
544 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey() |
- | 545 | * @access private |
|
520 | */ |
546 | */ |
521 | protected function setupKey() |
547 | protected function setupKey() |
522 | { |
548 | { |
523 | if (!isset($this->key)) { |
549 | if (!isset($this->key)) { |
524 | $this->setKey(''); |
550 | $this->setKey(''); |
Line 535... | Line 561... | ||
535 | 561 | ||
536 | /** |
562 | /** |
537 | * Setup the performance-optimized function for de/encrypt() |
563 | * Setup the performance-optimized function for de/encrypt() |
538 | * |
564 | * |
539 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt() |
565 | * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt() |
- | 566 | * @access private |
|
540 | */ |
567 | */ |
541 | protected function setupInlineCrypt() |
568 | protected function setupInlineCrypt() |
542 | { |
569 | { |
543 | // Init code for both, encrypt and decrypt. |
570 | // Init code for both, encrypt and decrypt. |
544 | $init_crypt = '$keys = $this->keys;'; |
571 | $init_crypt = '$keys = $this->keys;'; |