PHPSECLIB 0.3.1 added to the project to support SFTP transfers of lab orders and...
[openemr.git] / library / phpseclib / Crypt / DES.php
blob0d6dd00e5de08405f64c4e546e6212b05845b5e2
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 /**
5 * Pure-PHP implementation of DES.
7 * Uses mcrypt, if available, and an internal implementation, otherwise.
9 * PHP versions 4 and 5
11 * Useful resources are as follows:
13 * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}
14 * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}
15 * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}
17 * Here's a short example of how to use this library:
18 * <code>
19 * <?php
20 * include('Crypt/DES.php');
22 * $des = new Crypt_DES();
24 * $des->setKey('abcdefgh');
26 * $size = 10 * 1024;
27 * $plaintext = '';
28 * for ($i = 0; $i < $size; $i++) {
29 * $plaintext.= 'a';
30 * }
32 * echo $des->decrypt($des->encrypt($plaintext));
33 * ?>
34 * </code>
36 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to deal
38 * in the Software without restriction, including without limitation the rights
39 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40 * copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52 * THE SOFTWARE.
54 * @category Crypt
55 * @package Crypt_DES
56 * @author Jim Wigginton <terrafrost@php.net>
57 * @copyright MMVII Jim Wigginton
58 * @license http://www.opensource.org/licenses/mit-license.html MIT License
59 * @version $Id: DES.php,v 1.12 2010/02/09 06:10:26 terrafrost Exp $
60 * @link http://phpseclib.sourceforge.net
63 /**#@+
64 * @access private
65 * @see Crypt_DES::_prepareKey()
66 * @see Crypt_DES::_processBlock()
68 /**
69 * Contains array_reverse($keys[CRYPT_DES_DECRYPT])
71 define('CRYPT_DES_ENCRYPT', 0);
72 /**
73 * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])
75 define('CRYPT_DES_DECRYPT', 1);
76 /**#@-*/
78 /**#@+
79 * @access public
80 * @see Crypt_DES::encrypt()
81 * @see Crypt_DES::decrypt()
83 /**
84 * Encrypt / decrypt using the Counter mode.
86 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
88 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
90 define('CRYPT_DES_MODE_CTR', -1);
91 /**
92 * Encrypt / decrypt using the Electronic Code Book mode.
94 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
96 define('CRYPT_DES_MODE_ECB', 1);
97 /**
98 * Encrypt / decrypt using the Code Book Chaining mode.
100 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
102 define('CRYPT_DES_MODE_CBC', 2);
104 * Encrypt / decrypt using the Cipher Feedback mode.
106 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
108 define('CRYPT_DES_MODE_CFB', 3);
110 * Encrypt / decrypt using the Cipher Feedback mode.
112 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
114 define('CRYPT_DES_MODE_OFB', 4);
115 /**#@-*/
117 /**#@+
118 * @access private
119 * @see Crypt_DES::Crypt_DES()
122 * Toggles the internal implementation
124 define('CRYPT_DES_MODE_INTERNAL', 1);
126 * Toggles the mcrypt implementation
128 define('CRYPT_DES_MODE_MCRYPT', 2);
129 /**#@-*/
132 * Pure-PHP implementation of DES.
134 * @author Jim Wigginton <terrafrost@php.net>
135 * @version 0.1.0
136 * @access public
137 * @package Crypt_DES
139 class Crypt_DES {
141 * The Key Schedule
143 * @see Crypt_DES::setKey()
144 * @var Array
145 * @access private
147 var $keys = "\0\0\0\0\0\0\0\0";
150 * The Encryption Mode
152 * @see Crypt_DES::Crypt_DES()
153 * @var Integer
154 * @access private
156 var $mode;
159 * Continuous Buffer status
161 * @see Crypt_DES::enableContinuousBuffer()
162 * @var Boolean
163 * @access private
165 var $continuousBuffer = false;
168 * Padding status
170 * @see Crypt_DES::enablePadding()
171 * @var Boolean
172 * @access private
174 var $padding = true;
177 * The Initialization Vector
179 * @see Crypt_DES::setIV()
180 * @var String
181 * @access private
183 var $iv = "\0\0\0\0\0\0\0\0";
186 * A "sliding" Initialization Vector
188 * @see Crypt_DES::enableContinuousBuffer()
189 * @var String
190 * @access private
192 var $encryptIV = "\0\0\0\0\0\0\0\0";
195 * A "sliding" Initialization Vector
197 * @see Crypt_DES::enableContinuousBuffer()
198 * @var String
199 * @access private
201 var $decryptIV = "\0\0\0\0\0\0\0\0";
204 * mcrypt resource for encryption
206 * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
207 * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
209 * @see Crypt_DES::encrypt()
210 * @var String
211 * @access private
213 var $enmcrypt;
216 * mcrypt resource for decryption
218 * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
219 * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
221 * @see Crypt_DES::decrypt()
222 * @var String
223 * @access private
225 var $demcrypt;
228 * Does the enmcrypt resource need to be (re)initialized?
230 * @see Crypt_DES::setKey()
231 * @see Crypt_DES::setIV()
232 * @var Boolean
233 * @access private
235 var $enchanged = true;
238 * Does the demcrypt resource need to be (re)initialized?
240 * @see Crypt_DES::setKey()
241 * @see Crypt_DES::setIV()
242 * @var Boolean
243 * @access private
245 var $dechanged = true;
248 * Is the mode one that is paddable?
250 * @see Crypt_DES::Crypt_DES()
251 * @var Boolean
252 * @access private
254 var $paddable = false;
257 * Encryption buffer for CTR, OFB and CFB modes
259 * @see Crypt_DES::encrypt()
260 * @var String
261 * @access private
263 var $enbuffer = '';
266 * Decryption buffer for CTR, OFB and CFB modes
268 * @see Crypt_DES::decrypt()
269 * @var String
270 * @access private
272 var $debuffer = '';
275 * mcrypt resource for CFB mode
277 * @see Crypt_DES::encrypt()
278 * @see Crypt_DES::decrypt()
279 * @var String
280 * @access private
282 var $ecb;
285 * Default Constructor.
287 * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
288 * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
290 * @param optional Integer $mode
291 * @return Crypt_DES
292 * @access public
294 function Crypt_DES($mode = CRYPT_DES_MODE_CBC)
296 if ( !defined('CRYPT_DES_MODE') ) {
297 switch (true) {
298 case extension_loaded('mcrypt') && in_array('des', mcrypt_list_algorithms()):
299 define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
300 break;
301 default:
302 define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
306 switch ( CRYPT_DES_MODE ) {
307 case CRYPT_DES_MODE_MCRYPT:
308 switch ($mode) {
309 case CRYPT_DES_MODE_ECB:
310 $this->paddable = true;
311 $this->mode = MCRYPT_MODE_ECB;
312 break;
313 case CRYPT_DES_MODE_CTR:
314 $this->mode = 'ctr';
315 //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;
316 break;
317 case CRYPT_DES_MODE_CFB:
318 $this->mode = 'ncfb';
319 break;
320 case CRYPT_DES_MODE_OFB:
321 $this->mode = MCRYPT_MODE_NOFB;
322 break;
323 case CRYPT_DES_MODE_CBC:
324 default:
325 $this->paddable = true;
326 $this->mode = MCRYPT_MODE_CBC;
329 break;
330 default:
331 switch ($mode) {
332 case CRYPT_DES_MODE_ECB:
333 case CRYPT_DES_MODE_CBC:
334 $this->paddable = true;
335 $this->mode = $mode;
336 break;
337 case CRYPT_DES_MODE_CTR:
338 case CRYPT_DES_MODE_CFB:
339 case CRYPT_DES_MODE_OFB:
340 $this->mode = $mode;
341 break;
342 default:
343 $this->paddable = true;
344 $this->mode = CRYPT_DES_MODE_CBC;
350 * Sets the key.
352 * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
353 * only use the first eight, if $key has more then eight characters in it, and pad $key with the
354 * null byte if it is less then eight characters long.
356 * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
358 * If the key is not explicitly set, it'll be assumed to be all zero's.
360 * @access public
361 * @param String $key
363 function setKey($key)
365 $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? str_pad(substr($key, 0, 8), 8, chr(0)) : $this->_prepareKey($key);
366 $this->changed = true;
370 * Sets the password.
372 * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
373 * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}:
374 * $hash, $salt, $count
376 * @param String $password
377 * @param optional String $method
378 * @access public
380 function setPassword($password, $method = 'pbkdf2')
382 $key = '';
384 switch ($method) {
385 default: // 'pbkdf2'
386 list(, , $hash, $salt, $count) = func_get_args();
387 if (!isset($hash)) {
388 $hash = 'sha1';
390 // WPA and WPA use the SSID as the salt
391 if (!isset($salt)) {
392 $salt = 'phpseclib/salt';
394 // RFC2898#section-4.2 uses 1,000 iterations by default
395 // WPA and WPA2 use 4,096.
396 if (!isset($count)) {
397 $count = 1000;
400 if (!class_exists('Crypt_Hash')) {
401 require_once('Crypt/Hash.php');
404 $i = 1;
405 while (strlen($key) < 8) { // $dkLen == 8
406 //$dk.= $this->_pbkdf($password, $salt, $count, $i++);
407 $hmac = new Crypt_Hash();
408 $hmac->setHash($hash);
409 $hmac->setKey($password);
410 $f = $u = $hmac->hash($salt . pack('N', $i++));
411 for ($j = 2; $j <= $count; $j++) {
412 $u = $hmac->hash($u);
413 $f^= $u;
415 $key.= $f;
419 $this->setKey($key);
423 * Sets the initialization vector. (optional)
425 * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
426 * to be all zero's.
428 * @access public
429 * @param String $iv
431 function setIV($iv)
433 $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
434 $this->changed = true;
438 * Generate CTR XOR encryption key
440 * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
441 * plaintext / ciphertext in CTR mode.
443 * @see Crypt_DES::decrypt()
444 * @see Crypt_DES::encrypt()
445 * @access public
446 * @param Integer $length
447 * @param String $iv
449 function _generate_xor($length, &$iv)
451 $xor = '';
452 $num_blocks = ($length + 7) >> 3;
453 for ($i = 0; $i < $num_blocks; $i++) {
454 $xor.= $iv;
455 for ($j = 4; $j <= 8; $j+=4) {
456 $temp = substr($iv, -$j, 4);
457 switch ($temp) {
458 case "\xFF\xFF\xFF\xFF":
459 $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
460 break;
461 case "\x7F\xFF\xFF\xFF":
462 $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
463 break 2;
464 default:
465 extract(unpack('Ncount', $temp));
466 $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
467 break 2;
472 return $xor;
476 * Encrypts a message.
478 * $plaintext will be padded with up to 8 additional bytes. Other DES implementations may or may not pad in the
479 * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
480 * URL:
482 * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
484 * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
485 * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
486 * length.
488 * @see Crypt_DES::decrypt()
489 * @access public
490 * @param String $plaintext
492 function encrypt($plaintext)
494 if ($this->paddable) {
495 $plaintext = $this->_pad($plaintext);
498 if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
499 if ($this->enchanged) {
500 if (!isset($this->enmcrypt)) {
501 $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
503 mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
504 if ($this->mode != 'ncfb') {
505 $this->enchanged = false;
509 if ($this->mode != 'ncfb') {
510 $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
511 } else {
512 if ($this->enchanged) {
513 $this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
514 mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
515 $this->enchanged = false;
518 if (strlen($this->enbuffer)) {
519 $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer));
520 $this->enbuffer.= $ciphertext;
521 if (strlen($this->enbuffer) == 8) {
522 $this->encryptIV = $this->enbuffer;
523 $this->enbuffer = '';
524 mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
526 $plaintext = substr($plaintext, strlen($ciphertext));
527 } else {
528 $ciphertext = '';
531 $last_pos = strlen($plaintext) & 0xFFFFFFF8;
532 $ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : '';
534 if (strlen($plaintext) & 0x7) {
535 if (strlen($ciphertext)) {
536 $this->encryptIV = substr($ciphertext, -8);
538 $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
539 $this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
540 $ciphertext.= $this->enbuffer;
544 if (!$this->continuousBuffer) {
545 mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
548 return $ciphertext;
551 if (!is_array($this->keys)) {
552 $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
555 $buffer = &$this->enbuffer;
556 $continuousBuffer = $this->continuousBuffer;
557 $ciphertext = '';
558 switch ($this->mode) {
559 case CRYPT_DES_MODE_ECB:
560 for ($i = 0; $i < strlen($plaintext); $i+=8) {
561 $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);
563 break;
564 case CRYPT_DES_MODE_CBC:
565 $xor = $this->encryptIV;
566 for ($i = 0; $i < strlen($plaintext); $i+=8) {
567 $block = substr($plaintext, $i, 8);
568 $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);
569 $xor = $block;
570 $ciphertext.= $block;
572 if ($this->continuousBuffer) {
573 $this->encryptIV = $xor;
575 break;
576 case CRYPT_DES_MODE_CTR:
577 $xor = $this->encryptIV;
578 if (strlen($buffer['encrypted'])) {
579 for ($i = 0; $i < strlen($plaintext); $i+=8) {
580 $block = substr($plaintext, $i, 8);
581 $buffer['encrypted'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
582 $key = $this->_string_shift($buffer['encrypted'], 8);
583 $ciphertext.= $block ^ $key;
585 } else {
586 for ($i = 0; $i < strlen($plaintext); $i+=8) {
587 $block = substr($plaintext, $i, 8);
588 $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
589 $ciphertext.= $block ^ $key;
592 if ($this->continuousBuffer) {
593 $this->encryptIV = $xor;
594 if ($start = strlen($plaintext) & 7) {
595 $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
598 break;
599 case CRYPT_DES_MODE_CFB:
600 if (!empty($buffer['xor'])) {
601 $ciphertext = $plaintext ^ $buffer['xor'];
602 $iv = $buffer['encrypted'] . $ciphertext;
603 $start = strlen($ciphertext);
604 $buffer['encrypted'].= $ciphertext;
605 $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
606 } else {
607 $ciphertext = '';
608 $iv = $this->encryptIV;
609 $start = 0;
612 for ($i = $start; $i < strlen($plaintext); $i+=8) {
613 $block = substr($plaintext, $i, 8);
614 $xor = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
615 $iv = $block ^ $xor;
616 if ($continuousBuffer && strlen($iv) != 8) {
617 $buffer = array(
618 'encrypted' => $iv,
619 'xor' => substr($xor, strlen($iv))
622 $ciphertext.= $iv;
625 if ($this->continuousBuffer) {
626 $this->encryptIV = $iv;
628 break;
629 case CRYPT_DES_MODE_OFB:
630 $xor = $this->encryptIV;
631 if (strlen($buffer)) {
632 for ($i = 0; $i < strlen($plaintext); $i+=8) {
633 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
634 $buffer.= $xor;
635 $key = $this->_string_shift($buffer, 8);
636 $ciphertext.= substr($plaintext, $i, 8) ^ $key;
638 } else {
639 for ($i = 0; $i < strlen($plaintext); $i+=8) {
640 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
641 $ciphertext.= substr($plaintext, $i, 8) ^ $xor;
643 $key = $xor;
645 if ($this->continuousBuffer) {
646 $this->encryptIV = $xor;
647 if ($start = strlen($plaintext) & 7) {
648 $buffer = substr($key, $start) . $buffer;
653 return $ciphertext;
657 * Decrypts a message.
659 * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.
661 * @see Crypt_DES::encrypt()
662 * @access public
663 * @param String $ciphertext
665 function decrypt($ciphertext)
667 if ($this->paddable) {
668 // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
669 // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
670 $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
673 if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
674 if ($this->dechanged) {
675 if (!isset($this->demcrypt)) {
676 $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
678 mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
679 if ($this->mode != 'ncfb') {
680 $this->dechanged = false;
684 if ($this->mode != 'ncfb') {
685 $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
686 } else {
687 if ($this->dechanged) {
688 $this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
689 mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
690 $this->dechanged = false;
693 if (strlen($this->debuffer)) {
694 $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer));
696 $this->debuffer.= substr($ciphertext, 0, strlen($plaintext));
697 if (strlen($this->debuffer) == 8) {
698 $this->decryptIV = $this->debuffer;
699 $this->debuffer = '';
700 mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
702 $ciphertext = substr($ciphertext, strlen($plaintext));
703 } else {
704 $plaintext = '';
707 $last_pos = strlen($ciphertext) & 0xFFFFFFF8;
708 $plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : '';
710 if (strlen($ciphertext) & 0x7) {
711 if (strlen($plaintext)) {
712 $this->decryptIV = substr($ciphertext, $last_pos - 8, 8);
714 $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
715 $this->debuffer = substr($ciphertext, $last_pos);
716 $plaintext.= $this->debuffer ^ $this->decryptIV;
719 return $plaintext;
722 if (!$this->continuousBuffer) {
723 mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
726 return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
729 if (!is_array($this->keys)) {
730 $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
733 $buffer = &$this->debuffer;
734 $continuousBuffer = $this->continuousBuffer;
735 $plaintext = '';
736 switch ($this->mode) {
737 case CRYPT_DES_MODE_ECB:
738 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
739 $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);
741 break;
742 case CRYPT_DES_MODE_CBC:
743 $xor = $this->decryptIV;
744 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
745 $block = substr($ciphertext, $i, 8);
746 $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;
747 $xor = $block;
749 if ($this->continuousBuffer) {
750 $this->decryptIV = $xor;
752 break;
753 case CRYPT_DES_MODE_CTR:
754 $xor = $this->decryptIV;
755 if (strlen($buffer['ciphertext'])) {
756 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
757 $block = substr($ciphertext, $i, 8);
758 $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
759 $key = $this->_string_shift($buffer['ciphertext'], 8);
760 $plaintext.= $block ^ $key;
762 } else {
763 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
764 $block = substr($ciphertext, $i, 8);
765 $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
766 $plaintext.= $block ^ $key;
769 if ($this->continuousBuffer) {
770 $this->decryptIV = $xor;
771 if ($start = strlen($ciphertext) % 8) {
772 $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
775 break;
776 case CRYPT_DES_MODE_CFB:
777 if (!empty($buffer['ciphertext'])) {
778 $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer['ciphertext']));
779 $buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext));
780 if (strlen($buffer['ciphertext']) == 8) {
781 $xor = $this->_processBlock($buffer['ciphertext'], CRYPT_DES_ENCRYPT);
782 $buffer['ciphertext'] = '';
784 $start = strlen($plaintext);
785 $block = $this->decryptIV;
786 } else {
787 $plaintext = '';
788 $xor = $this->_processBlock($this->decryptIV, CRYPT_DES_ENCRYPT);
789 $start = 0;
792 for ($i = $start; $i < strlen($ciphertext); $i+=8) {
793 $block = substr($ciphertext, $i, 8);
794 $plaintext.= $block ^ $xor;
795 if ($continuousBuffer && strlen($block) != 8) {
796 $buffer['ciphertext'].= $block;
797 $block = $xor;
798 } else if (strlen($block) == 8) {
799 $xor = $this->_processBlock($block, CRYPT_DES_ENCRYPT);
802 if ($this->continuousBuffer) {
803 $this->decryptIV = $block;
805 break;
806 case CRYPT_DES_MODE_OFB:
807 $xor = $this->decryptIV;
808 if (strlen($buffer)) {
809 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
810 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
811 $buffer.= $xor;
812 $key = $this->_string_shift($buffer, 8);
813 $plaintext.= substr($ciphertext, $i, 8) ^ $key;
815 } else {
816 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
817 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
818 $plaintext.= substr($ciphertext, $i, 8) ^ $xor;
820 $key = $xor;
822 if ($this->continuousBuffer) {
823 $this->decryptIV = $xor;
824 if ($start = strlen($ciphertext) % 8) {
825 $buffer = substr($key, $start) . $buffer;
830 return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
834 * Treat consecutive "packets" as if they are a continuous buffer.
836 * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
837 * will yield different outputs:
839 * <code>
840 * echo $des->encrypt(substr($plaintext, 0, 8));
841 * echo $des->encrypt(substr($plaintext, 8, 8));
842 * </code>
843 * <code>
844 * echo $des->encrypt($plaintext);
845 * </code>
847 * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
848 * another, as demonstrated with the following:
850 * <code>
851 * $des->encrypt(substr($plaintext, 0, 8));
852 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
853 * </code>
854 * <code>
855 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
856 * </code>
858 * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
859 * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
860 * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
862 * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
863 * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
864 * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
865 * however, they are also less intuitive and more likely to cause you problems.
867 * @see Crypt_DES::disableContinuousBuffer()
868 * @access public
870 function enableContinuousBuffer()
872 $this->continuousBuffer = true;
876 * Treat consecutive packets as if they are a discontinuous buffer.
878 * The default behavior.
880 * @see Crypt_DES::enableContinuousBuffer()
881 * @access public
883 function disableContinuousBuffer()
885 $this->continuousBuffer = false;
886 $this->encryptIV = $this->iv;
887 $this->decryptIV = $this->iv;
891 * Pad "packets".
893 * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
894 * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
896 * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
897 * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
898 * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
899 * transmitted separately)
901 * @see Crypt_DES::disablePadding()
902 * @access public
904 function enablePadding()
906 $this->padding = true;
910 * Do not pad packets.
912 * @see Crypt_DES::enablePadding()
913 * @access public
915 function disablePadding()
917 $this->padding = false;
921 * Pads a string
923 * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
924 * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
926 * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
927 * and padding will, hence forth, be enabled.
929 * @see Crypt_DES::_unpad()
930 * @access private
932 function _pad($text)
934 $length = strlen($text);
936 if (!$this->padding) {
937 if (($length & 7) == 0) {
938 return $text;
939 } else {
940 user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
941 $this->padding = true;
945 $pad = 8 - ($length & 7);
946 return str_pad($text, $length + $pad, chr($pad));
950 * Unpads a string
952 * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
953 * and false will be returned.
955 * @see Crypt_DES::_pad()
956 * @access private
958 function _unpad($text)
960 if (!$this->padding) {
961 return $text;
964 $length = ord($text[strlen($text) - 1]);
966 if (!$length || $length > 8) {
967 return false;
970 return substr($text, 0, -$length);
974 * Encrypts or decrypts a 64-bit block
976 * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
977 * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
978 * idea of what this function does.
980 * @access private
981 * @param String $block
982 * @param Integer $mode
983 * @return String
985 function _processBlock($block, $mode)
987 // s-boxes. in the official DES docs, they're described as being matrices that
988 // one accesses by using the first and last bits to determine the row and the
989 // middle four bits to determine the column. in this implementation, they've
990 // been converted to vectors
991 static $sbox = array(
992 array(
993 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
994 3, 10 ,10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
995 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
996 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
998 array(
999 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
1000 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
1001 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
1002 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
1004 array(
1005 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1006 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
1007 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
1008 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
1010 array(
1011 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1012 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
1013 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
1014 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
1016 array(
1017 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
1018 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
1019 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
1020 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
1022 array(
1023 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
1024 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
1025 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
1026 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
1028 array(
1029 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
1030 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1031 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
1032 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
1034 array(
1035 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
1036 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
1037 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
1038 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
1042 $keys = $this->keys;
1044 $temp = unpack('Na/Nb', $block);
1045 $block = array($temp['a'], $temp['b']);
1047 // because php does arithmetic right shifts, if the most significant bits are set, right
1048 // shifting those into the correct position will add 1's - not 0's. this will intefere
1049 // with the | operation unless a second & is done. so we isolate these bits and left shift
1050 // them into place. we then & each block with 0x7FFFFFFF to prevennt 1's from being added
1051 // for any other shifts.
1052 $msb = array(
1053 ($block[0] >> 31) & 1,
1054 ($block[1] >> 31) & 1
1056 $block[0] &= 0x7FFFFFFF;
1057 $block[1] &= 0x7FFFFFFF;
1059 // we isolate the appropriate bit in the appropriate integer and shift as appropriate. in
1060 // some cases, there are going to be multiple bits in the same integer that need to be shifted
1061 // in the same way. we combine those into one shift operation.
1062 $block = array(
1063 (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |
1064 (($block[1] & 0x00400001) << 7) | (($block[1] & 0x40000100) >> 2) |
1065 (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |
1066 (($block[0] & 0x00400001) << 3) | (($block[0] & 0x40000100) >> 6) |
1067 (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |
1068 (($block[1] & 0x00100000) << 1) | (($block[1] & 0x10000000) >> 8) |
1069 (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) << 6) |
1070 (($block[0] & 0x00100000) >> 3) | (($block[0] & 0x10000000) >> 12) |
1071 (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) << 4) |
1072 (($block[1] & 0x00040000) >> 5) | (($block[1] & 0x04000000) >> 14) |
1073 (($block[0] & 0x00000004) << 9) | ( $block[0] & 0x00000400 ) |
1074 (($block[0] & 0x00040000) >> 9) | (($block[0] & 0x04000000) >> 18) |
1075 (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |
1076 (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)
1078 (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |
1079 (($block[1] & 0x00800002) << 6) | (($block[0] & 0x00000080) << 20) |
1080 (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) << 2) |
1081 (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) << 9) |
1082 ( $block[1] & 0x00200000 ) | (($block[1] & 0x20000000) >> 9) |
1083 (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) << 5) |
1084 (($block[0] & 0x00200000) >> 4) | (($block[0] & 0x20000000) >> 13) |
1085 (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) << 3) |
1086 (($block[1] & 0x00080000) >> 6) | (($block[1] & 0x08000000) >> 15) |
1087 (($block[0] & 0x00000008) << 8) | (($block[0] & 0x00000800) >> 1) |
1088 (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |
1089 (($block[1] & 0x00000200) >> 3) | (($block[0] & 0x00000200) >> 7) |
1090 (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |
1091 (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |
1092 ($msb[1] << 28) | ($msb[0] << 24)
1095 for ($i = 0; $i < 16; $i++) {
1096 // start of "the Feistel (F) function" - see the following URL:
1097 // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
1098 $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)
1099 | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)
1100 | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)
1101 | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)
1102 | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)
1103 | (($sbox[5][(($block[1] & 0x00001F80) >> 7) ^ $keys[$mode][$i][5]]) << 8)
1104 | (($sbox[6][(($block[1] & 0x000001F8) >> 3) ^ $keys[$mode][$i][6]]) << 4)
1105 | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);
1107 $msb = ($temp >> 31) & 1;
1108 $temp &= 0x7FFFFFFF;
1109 $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) << 5)
1110 | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)
1111 | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) << 6)
1112 | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) << 9)
1113 | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)
1114 | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >> 8)
1115 | (($temp & 0x00004000) << 4) | (($temp & 0x00000002) << 16)
1116 | (($temp & 0x00442000) >> 6) | (($temp & 0x40800000) >> 15)
1117 | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)
1118 | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) << 3)
1119 | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >> 7)
1120 | (($temp & 0x00200000) >> 19) | ($msb << 23);
1121 // end of "the Feistel (F) function" - $newBlock is F's output
1123 $temp = $block[1];
1124 $block[1] = $block[0] ^ $newBlock;
1125 $block[0] = $temp;
1128 $msb = array(
1129 ($block[0] >> 31) & 1,
1130 ($block[1] >> 31) & 1
1132 $block[0] &= 0x7FFFFFFF;
1133 $block[1] &= 0x7FFFFFFF;
1135 $block = array(
1136 (($block[0] & 0x01000004) << 7) | (($block[1] & 0x01000004) << 6) |
1137 (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |
1138 (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |
1139 (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |
1140 (($block[0] & 0x02000008) >> 2) | (($block[1] & 0x02000008) >> 3) |
1141 (($block[0] & 0x00020000) << 4) | (($block[1] & 0x00020000) << 3) |
1142 (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) << 9) |
1143 (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |
1144 (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |
1145 (($block[0] & 0x00040000) >> 5) | (($block[1] & 0x00040000) >> 6) |
1146 (($block[0] & 0x00000400) << 1) | ( $block[1] & 0x00000400 ) |
1147 (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |
1148 (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |
1149 (($block[0] & 0x00000800) >> 8) | (($block[1] & 0x00000800) >> 9)
1151 (($block[0] & 0x10000040) << 3) | (($block[1] & 0x10000040) << 2) |
1152 (($block[0] & 0x00100000) << 9) | (($block[1] & 0x00100000) << 8) |
1153 (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |
1154 (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |
1155 (($block[0] & 0x20000080) >> 6) | (($block[1] & 0x20000080) >> 7) |
1156 ( $block[0] & 0x00200000 ) | (($block[1] & 0x00200000) >> 1) |
1157 (($block[0] & 0x00002000) << 6) | (($block[1] & 0x00002000) << 5) |
1158 (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |
1159 (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |
1160 (($block[0] & 0x00400000) >> 9) | (($block[1] & 0x00400000) >> 10) |
1161 (($block[0] & 0x00004000) >> 3) | (($block[1] & 0x00004000) >> 4) |
1162 (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |
1163 (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |
1164 ($msb[0] << 7) | ($msb[1] << 6)
1167 return pack('NN', $block[0], $block[1]);
1171 * Creates the key schedule.
1173 * @access private
1174 * @param String $key
1175 * @return Array
1177 function _prepareKey($key)
1179 static $shifts = array( // number of key bits shifted per round
1180 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
1183 // pad the key and remove extra characters as appropriate.
1184 $key = str_pad(substr($key, 0, 8), 8, chr(0));
1186 $temp = unpack('Na/Nb', $key);
1187 $key = array($temp['a'], $temp['b']);
1188 $msb = array(
1189 ($key[0] >> 31) & 1,
1190 ($key[1] >> 31) & 1
1192 $key[0] &= 0x7FFFFFFF;
1193 $key[1] &= 0x7FFFFFFF;
1195 $key = array(
1196 (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |
1197 (($key[1] & 0x00020408) << 8) | (($key[1] & 0x02040800) >> 1) |
1198 (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |
1199 (($key[0] & 0x00020408) << 4) | (($key[0] & 0x02040800) >> 5) |
1200 (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |
1201 (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |
1202 (($key[0] & 0x00000010) >> 1) | (($key[0] & 0x00001000) >> 10) |
1203 (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)
1205 (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |
1206 (($key[1] & 0x00800000) << 2) | (($key[0] & 0x00000080) << 16) |
1207 (($key[0] & 0x00008000) << 7) | (($key[0] & 0x00800000) >> 2) |
1208 (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) << 4) |
1209 (($key[1] & 0x00400000) >> 5) | (($key[1] & 0x40000000) >> 14) |
1210 (($key[0] & 0x00000040) << 9) | ( $key[0] & 0x00004000 ) |
1211 (($key[0] & 0x00400000) >> 9) | (($key[0] & 0x40000000) >> 18) |
1212 (($key[1] & 0x00000020) << 6) | (($key[1] & 0x00002000) >> 3) |
1213 (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |
1214 (($key[0] & 0x00000020) << 2) | (($key[0] & 0x00002000) >> 7) |
1215 (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |
1216 (($key[1] & 0x00000010) >> 1) | (($key[1] & 0x00001000) >> 10) |
1217 (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |
1218 ($msb[1] << 24) | ($msb[0] << 20)
1221 $keys = array();
1222 for ($i = 0; $i < 16; $i++) {
1223 $key[0] <<= $shifts[$i];
1224 $temp = ($key[0] & 0xF0000000) >> 28;
1225 $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;
1227 $key[1] <<= $shifts[$i];
1228 $temp = ($key[1] & 0xF0000000) >> 28;
1229 $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;
1231 $temp = array(
1232 (($key[1] & 0x00004000) >> 9) | (($key[1] & 0x00000800) >> 7) |
1233 (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >> 2) |
1234 (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)
1236 (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) << 4) |
1237 (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |
1238 (($key[1] & 0x00000080) >> 6)
1240 ( $key[1] & 0x00000020 ) | (($key[1] & 0x00000200) >> 5) |
1241 (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |
1242 (($key[1] & 0x00000004) >> 1) | (($key[1] & 0x00100000) >> 20)
1244 (($key[1] & 0x00001000) >> 7) | (($key[1] & 0x00200000) >> 17) |
1245 (($key[1] & 0x00000002) << 2) | (($key[1] & 0x00000100) >> 6) |
1246 (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)
1248 (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010 ) |
1249 (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |
1250 (($key[0] & 0x00000200) >> 8) | (($key[0] & 0x00000002) >> 1)
1252 (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |
1253 (($key[0] & 0x00000020) >> 2) | (($key[0] & 0x00000800) >> 9) |
1254 (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >> 8)
1256 (($key[0] & 0x00001000) >> 7) | (($key[0] & 0x00000088) >> 3) |
1257 (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) << 2) |
1258 (($key[0] & 0x00400000) >> 21)
1260 (($key[0] & 0x00000400) >> 5) | (($key[0] & 0x00004000) >> 10) |
1261 (($key[0] & 0x00000040) >> 3) | (($key[0] & 0x00100000) >> 18) |
1262 (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)
1265 $keys[] = $temp;
1268 $temp = array(
1269 CRYPT_DES_ENCRYPT => $keys,
1270 CRYPT_DES_DECRYPT => array_reverse($keys)
1273 return $temp;
1277 * String Shift
1279 * Inspired by array_shift
1281 * @param String $string
1282 * @param optional Integer $index
1283 * @return String
1284 * @access private
1286 function _string_shift(&$string, $index = 1)
1288 $substr = substr($string, 0, $index);
1289 $string = substr($string, $index);
1290 return $substr;
1294 // vim: ts=4:sw=4:et:
1295 // vim6: fdl=1: