PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/DES.php

https://github.com/sezuan/core
PHP | 1334 lines | 822 code | 76 blank | 436 comment | 109 complexity | ab14022fc00c783661dd0da32aba83a9 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of DES.
  5. *
  6. * Uses mcrypt, if available, and an internal implementation, otherwise.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * Useful resources are as follows:
  11. *
  12. * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}
  13. * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}
  14. * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}
  15. *
  16. * Here's a short example of how to use this library:
  17. * <code>
  18. * <?php
  19. * include('Crypt/DES.php');
  20. *
  21. * $des = new Crypt_DES();
  22. *
  23. * $des->setKey('abcdefgh');
  24. *
  25. * $size = 10 * 1024;
  26. * $plaintext = '';
  27. * for ($i = 0; $i < $size; $i++) {
  28. * $plaintext.= 'a';
  29. * }
  30. *
  31. * echo $des->decrypt($des->encrypt($plaintext));
  32. * ?>
  33. * </code>
  34. *
  35. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  36. * of this software and associated documentation files (the "Software"), to deal
  37. * in the Software without restriction, including without limitation the rights
  38. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  39. * copies of the Software, and to permit persons to whom the Software is
  40. * furnished to do so, subject to the following conditions:
  41. *
  42. * The above copyright notice and this permission notice shall be included in
  43. * all copies or substantial portions of the Software.
  44. *
  45. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  47. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  48. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  49. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  50. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  51. * THE SOFTWARE.
  52. *
  53. * @category Crypt
  54. * @package Crypt_DES
  55. * @author Jim Wigginton <terrafrost@php.net>
  56. * @copyright MMVII Jim Wigginton
  57. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  58. * @version $Id: DES.php,v 1.12 2010/02/09 06:10:26 terrafrost Exp $
  59. * @link http://phpseclib.sourceforge.net
  60. */
  61. /**#@+
  62. * @access private
  63. * @see Crypt_DES::_prepareKey()
  64. * @see Crypt_DES::_processBlock()
  65. */
  66. /**
  67. * Contains array_reverse($keys[CRYPT_DES_DECRYPT])
  68. */
  69. define('CRYPT_DES_ENCRYPT', 0);
  70. /**
  71. * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])
  72. */
  73. define('CRYPT_DES_DECRYPT', 1);
  74. /**#@-*/
  75. /**#@+
  76. * @access public
  77. * @see Crypt_DES::encrypt()
  78. * @see Crypt_DES::decrypt()
  79. */
  80. /**
  81. * Encrypt / decrypt using the Counter mode.
  82. *
  83. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  84. *
  85. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  86. */
  87. define('CRYPT_DES_MODE_CTR', -1);
  88. /**
  89. * Encrypt / decrypt using the Electronic Code Book mode.
  90. *
  91. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  92. */
  93. define('CRYPT_DES_MODE_ECB', 1);
  94. /**
  95. * Encrypt / decrypt using the Code Book Chaining mode.
  96. *
  97. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  98. */
  99. define('CRYPT_DES_MODE_CBC', 2);
  100. /**
  101. * Encrypt / decrypt using the Cipher Feedback mode.
  102. *
  103. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  104. */
  105. define('CRYPT_DES_MODE_CFB', 3);
  106. /**
  107. * Encrypt / decrypt using the Cipher Feedback mode.
  108. *
  109. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  110. */
  111. define('CRYPT_DES_MODE_OFB', 4);
  112. /**#@-*/
  113. /**#@+
  114. * @access private
  115. * @see Crypt_DES::Crypt_DES()
  116. */
  117. /**
  118. * Toggles the internal implementation
  119. */
  120. define('CRYPT_DES_MODE_INTERNAL', 1);
  121. /**
  122. * Toggles the mcrypt implementation
  123. */
  124. define('CRYPT_DES_MODE_MCRYPT', 2);
  125. /**#@-*/
  126. /**
  127. * Pure-PHP implementation of DES.
  128. *
  129. * @author Jim Wigginton <terrafrost@php.net>
  130. * @version 0.1.0
  131. * @access public
  132. * @package Crypt_DES
  133. */
  134. class Crypt_DES {
  135. /**
  136. * The Key Schedule
  137. *
  138. * @see Crypt_DES::setKey()
  139. * @var Array
  140. * @access private
  141. */
  142. var $keys = "\0\0\0\0\0\0\0\0";
  143. /**
  144. * The Encryption Mode
  145. *
  146. * @see Crypt_DES::Crypt_DES()
  147. * @var Integer
  148. * @access private
  149. */
  150. var $mode;
  151. /**
  152. * Continuous Buffer status
  153. *
  154. * @see Crypt_DES::enableContinuousBuffer()
  155. * @var Boolean
  156. * @access private
  157. */
  158. var $continuousBuffer = false;
  159. /**
  160. * Padding status
  161. *
  162. * @see Crypt_DES::enablePadding()
  163. * @var Boolean
  164. * @access private
  165. */
  166. var $padding = true;
  167. /**
  168. * The Initialization Vector
  169. *
  170. * @see Crypt_DES::setIV()
  171. * @var String
  172. * @access private
  173. */
  174. var $iv = "\0\0\0\0\0\0\0\0";
  175. /**
  176. * A "sliding" Initialization Vector
  177. *
  178. * @see Crypt_DES::enableContinuousBuffer()
  179. * @var String
  180. * @access private
  181. */
  182. var $encryptIV = "\0\0\0\0\0\0\0\0";
  183. /**
  184. * A "sliding" Initialization Vector
  185. *
  186. * @see Crypt_DES::enableContinuousBuffer()
  187. * @var String
  188. * @access private
  189. */
  190. var $decryptIV = "\0\0\0\0\0\0\0\0";
  191. /**
  192. * mcrypt resource for encryption
  193. *
  194. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  195. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  196. *
  197. * @see Crypt_DES::encrypt()
  198. * @var String
  199. * @access private
  200. */
  201. var $enmcrypt;
  202. /**
  203. * mcrypt resource for decryption
  204. *
  205. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  206. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  207. *
  208. * @see Crypt_DES::decrypt()
  209. * @var String
  210. * @access private
  211. */
  212. var $demcrypt;
  213. /**
  214. * Does the enmcrypt resource need to be (re)initialized?
  215. *
  216. * @see Crypt_DES::setKey()
  217. * @see Crypt_DES::setIV()
  218. * @var Boolean
  219. * @access private
  220. */
  221. var $enchanged = true;
  222. /**
  223. * Does the demcrypt resource need to be (re)initialized?
  224. *
  225. * @see Crypt_DES::setKey()
  226. * @see Crypt_DES::setIV()
  227. * @var Boolean
  228. * @access private
  229. */
  230. var $dechanged = true;
  231. /**
  232. * Is the mode one that is paddable?
  233. *
  234. * @see Crypt_DES::Crypt_DES()
  235. * @var Boolean
  236. * @access private
  237. */
  238. var $paddable = false;
  239. /**
  240. * Encryption buffer for CTR, OFB and CFB modes
  241. *
  242. * @see Crypt_DES::encrypt()
  243. * @var Array
  244. * @access private
  245. */
  246. var $enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
  247. /**
  248. * Decryption buffer for CTR, OFB and CFB modes
  249. *
  250. * @see Crypt_DES::decrypt()
  251. * @var Array
  252. * @access private
  253. */
  254. var $debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'demcrypt_init' => true);
  255. /**
  256. * mcrypt resource for CFB mode
  257. *
  258. * @see Crypt_DES::encrypt()
  259. * @see Crypt_DES::decrypt()
  260. * @var String
  261. * @access private
  262. */
  263. var $ecb;
  264. /**
  265. * Default Constructor.
  266. *
  267. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  268. * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
  269. *
  270. * @param optional Integer $mode
  271. * @return Crypt_DES
  272. * @access public
  273. */
  274. function Crypt_DES($mode = CRYPT_DES_MODE_CBC)
  275. {
  276. if ( !defined('CRYPT_DES_MODE') ) {
  277. switch (true) {
  278. case extension_loaded('mcrypt') && in_array('des', mcrypt_list_algorithms()):
  279. define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
  280. break;
  281. default:
  282. define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
  283. }
  284. }
  285. switch ( CRYPT_DES_MODE ) {
  286. case CRYPT_DES_MODE_MCRYPT:
  287. switch ($mode) {
  288. case CRYPT_DES_MODE_ECB:
  289. $this->paddable = true;
  290. $this->mode = MCRYPT_MODE_ECB;
  291. break;
  292. case CRYPT_DES_MODE_CTR:
  293. $this->mode = 'ctr';
  294. //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;
  295. break;
  296. case CRYPT_DES_MODE_CFB:
  297. $this->mode = 'ncfb';
  298. $this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
  299. break;
  300. case CRYPT_DES_MODE_OFB:
  301. $this->mode = MCRYPT_MODE_NOFB;
  302. break;
  303. case CRYPT_DES_MODE_CBC:
  304. default:
  305. $this->paddable = true;
  306. $this->mode = MCRYPT_MODE_CBC;
  307. }
  308. $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  309. $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  310. break;
  311. default:
  312. switch ($mode) {
  313. case CRYPT_DES_MODE_ECB:
  314. case CRYPT_DES_MODE_CBC:
  315. $this->paddable = true;
  316. $this->mode = $mode;
  317. break;
  318. case CRYPT_DES_MODE_CTR:
  319. case CRYPT_DES_MODE_CFB:
  320. case CRYPT_DES_MODE_OFB:
  321. $this->mode = $mode;
  322. break;
  323. default:
  324. $this->paddable = true;
  325. $this->mode = CRYPT_DES_MODE_CBC;
  326. }
  327. }
  328. }
  329. /**
  330. * Sets the key.
  331. *
  332. * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
  333. * only use the first eight, if $key has more then eight characters in it, and pad $key with the
  334. * null byte if it is less then eight characters long.
  335. *
  336. * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
  337. *
  338. * If the key is not explicitly set, it'll be assumed to be all zero's.
  339. *
  340. * @access public
  341. * @param String $key
  342. */
  343. function setKey($key)
  344. {
  345. $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? str_pad(substr($key, 0, 8), 8, chr(0)) : $this->_prepareKey($key);
  346. $this->enchanged = true;
  347. $this->dechanged = true;
  348. }
  349. /**
  350. * Sets the password.
  351. *
  352. * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
  353. * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}:
  354. * $hash, $salt, $count
  355. *
  356. * @param String $password
  357. * @param optional String $method
  358. * @access public
  359. */
  360. function setPassword($password, $method = 'pbkdf2')
  361. {
  362. $key = '';
  363. switch ($method) {
  364. default: // 'pbkdf2'
  365. list(, , $hash, $salt, $count) = func_get_args();
  366. if (!isset($hash)) {
  367. $hash = 'sha1';
  368. }
  369. // WPA and WPA2 use the SSID as the salt
  370. if (!isset($salt)) {
  371. $salt = 'phpseclib/salt';
  372. }
  373. // RFC2898#section-4.2 uses 1,000 iterations by default
  374. // WPA and WPA2 use 4,096.
  375. if (!isset($count)) {
  376. $count = 1000;
  377. }
  378. if (!class_exists('Crypt_Hash')) {
  379. require_once('Crypt/Hash.php');
  380. }
  381. $i = 1;
  382. while (strlen($key) < 8) { // $dkLen == 8
  383. //$dk.= $this->_pbkdf($password, $salt, $count, $i++);
  384. $hmac = new Crypt_Hash();
  385. $hmac->setHash($hash);
  386. $hmac->setKey($password);
  387. $f = $u = $hmac->hash($salt . pack('N', $i++));
  388. for ($j = 2; $j <= $count; $j++) {
  389. $u = $hmac->hash($u);
  390. $f^= $u;
  391. }
  392. $key.= $f;
  393. }
  394. }
  395. $this->setKey($key);
  396. }
  397. /**
  398. * Sets the initialization vector. (optional)
  399. *
  400. * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
  401. * to be all zero's.
  402. *
  403. * @access public
  404. * @param String $iv
  405. */
  406. function setIV($iv)
  407. {
  408. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
  409. $this->enchanged = true;
  410. $this->dechanged = true;
  411. }
  412. /**
  413. * Generate CTR XOR encryption key
  414. *
  415. * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
  416. * plaintext / ciphertext in CTR mode.
  417. *
  418. * @see Crypt_DES::decrypt()
  419. * @see Crypt_DES::encrypt()
  420. * @access public
  421. * @param String $iv
  422. */
  423. function _generate_xor(&$iv)
  424. {
  425. $xor = $iv;
  426. for ($j = 4; $j <= 8; $j+=4) {
  427. $temp = substr($iv, -$j, 4);
  428. switch ($temp) {
  429. case "\xFF\xFF\xFF\xFF":
  430. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  431. break;
  432. case "\x7F\xFF\xFF\xFF":
  433. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  434. break 2;
  435. default:
  436. extract(unpack('Ncount', $temp));
  437. $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  438. break 2;
  439. }
  440. }
  441. return $xor;
  442. }
  443. /**
  444. * Encrypts a message.
  445. *
  446. * $plaintext will be padded with up to 8 additional bytes. Other DES implementations may or may not pad in the
  447. * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
  448. * URL:
  449. *
  450. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  451. *
  452. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  453. * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
  454. * length.
  455. *
  456. * @see Crypt_DES::decrypt()
  457. * @access public
  458. * @param String $plaintext
  459. */
  460. function encrypt($plaintext)
  461. {
  462. if ($this->paddable) {
  463. $plaintext = $this->_pad($plaintext);
  464. }
  465. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  466. if ($this->enchanged) {
  467. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  468. if ($this->mode == 'ncfb') {
  469. mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
  470. }
  471. $this->enchanged = false;
  472. }
  473. if ($this->mode != 'ncfb' || !$this->continuousBuffer) {
  474. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  475. } else {
  476. $iv = &$this->encryptIV;
  477. $pos = &$this->enbuffer['pos'];
  478. $len = strlen($plaintext);
  479. $ciphertext = '';
  480. $i = 0;
  481. if ($pos) {
  482. $orig_pos = $pos;
  483. $max = 8 - $pos;
  484. if ($len >= $max) {
  485. $i = $max;
  486. $len-= $max;
  487. $pos = 0;
  488. } else {
  489. $i = $len;
  490. $pos+= $len;
  491. $len = 0;
  492. }
  493. $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
  494. $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
  495. $this->enbuffer['enmcrypt_init'] = true;
  496. }
  497. if ($len >= 8) {
  498. if ($this->enbuffer['enmcrypt_init'] === false || $len > 600) {
  499. if ($this->enbuffer['enmcrypt_init'] === true) {
  500. mcrypt_generic_init($this->enmcrypt, $this->keys, $iv);
  501. $this->enbuffer['enmcrypt_init'] = false;
  502. }
  503. $ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % 8));
  504. $iv = substr($ciphertext, -8);
  505. $len%= 8;
  506. } else {
  507. while ($len >= 8) {
  508. $iv = mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, 8);
  509. $ciphertext.= $iv;
  510. $len-= 8;
  511. $i+= 8;
  512. }
  513. }
  514. }
  515. if ($len) {
  516. $iv = mcrypt_generic($this->ecb, $iv);
  517. $block = $iv ^ substr($plaintext, -$len);
  518. $iv = substr_replace($iv, $block, 0, $len);
  519. $ciphertext.= $block;
  520. $pos = $len;
  521. }
  522. return $ciphertext;
  523. }
  524. if (!$this->continuousBuffer) {
  525. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  526. }
  527. return $ciphertext;
  528. }
  529. if (!is_array($this->keys)) {
  530. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  531. }
  532. $buffer = &$this->enbuffer;
  533. $continuousBuffer = $this->continuousBuffer;
  534. $ciphertext = '';
  535. switch ($this->mode) {
  536. case CRYPT_DES_MODE_ECB:
  537. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  538. $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);
  539. }
  540. break;
  541. case CRYPT_DES_MODE_CBC:
  542. $xor = $this->encryptIV;
  543. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  544. $block = substr($plaintext, $i, 8);
  545. $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);
  546. $xor = $block;
  547. $ciphertext.= $block;
  548. }
  549. if ($this->continuousBuffer) {
  550. $this->encryptIV = $xor;
  551. }
  552. break;
  553. case CRYPT_DES_MODE_CTR:
  554. $xor = $this->encryptIV;
  555. if (strlen($buffer['encrypted'])) {
  556. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  557. $block = substr($plaintext, $i, 8);
  558. $buffer['encrypted'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
  559. $key = $this->_string_shift($buffer['encrypted'], 8);
  560. $ciphertext.= $block ^ $key;
  561. }
  562. } else {
  563. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  564. $block = substr($plaintext, $i, 8);
  565. $key = $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
  566. $ciphertext.= $block ^ $key;
  567. }
  568. }
  569. if ($this->continuousBuffer) {
  570. $this->encryptIV = $xor;
  571. if ($start = strlen($plaintext) & 7) {
  572. $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
  573. }
  574. }
  575. break;
  576. case CRYPT_DES_MODE_CFB:
  577. if ($this->continuousBuffer) {
  578. $iv = &$this->encryptIV;
  579. $pos = &$buffer['pos'];
  580. } else {
  581. $iv = $this->encryptIV;
  582. $pos = 0;
  583. }
  584. $len = strlen($plaintext);
  585. $i = 0;
  586. if ($pos) {
  587. $orig_pos = $pos;
  588. $max = 8 - $pos;
  589. if ($len >= $max) {
  590. $i = $max;
  591. $len-= $max;
  592. $pos = 0;
  593. } else {
  594. $i = $len;
  595. $pos+= $len;
  596. $len = 0;
  597. }
  598. $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
  599. $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
  600. }
  601. while ($len >= 8) {
  602. $iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT) ^ substr($plaintext, $i, 8);
  603. $ciphertext.= $iv;
  604. $len-= 8;
  605. $i+= 8;
  606. }
  607. if ($len) {
  608. $iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
  609. $block = $iv ^ substr($plaintext, $i);
  610. $iv = substr_replace($iv, $block, 0, $len);
  611. $ciphertext.= $block;
  612. $pos = $len;
  613. }
  614. return $ciphertext;
  615. case CRYPT_DES_MODE_OFB:
  616. $xor = $this->encryptIV;
  617. if (strlen($buffer['xor'])) {
  618. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  619. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  620. $buffer['xor'].= $xor;
  621. $key = $this->_string_shift($buffer['xor'], 8);
  622. $ciphertext.= substr($plaintext, $i, 8) ^ $key;
  623. }
  624. } else {
  625. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  626. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  627. $ciphertext.= substr($plaintext, $i, 8) ^ $xor;
  628. }
  629. $key = $xor;
  630. }
  631. if ($this->continuousBuffer) {
  632. $this->encryptIV = $xor;
  633. if ($start = strlen($plaintext) & 7) {
  634. $buffer['xor'] = substr($key, $start) . $buffer['xor'];
  635. }
  636. }
  637. }
  638. return $ciphertext;
  639. }
  640. /**
  641. * Decrypts a message.
  642. *
  643. * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.
  644. *
  645. * @see Crypt_DES::encrypt()
  646. * @access public
  647. * @param String $ciphertext
  648. */
  649. function decrypt($ciphertext)
  650. {
  651. if ($this->paddable) {
  652. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  653. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  654. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
  655. }
  656. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  657. if ($this->dechanged) {
  658. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  659. if ($this->mode == 'ncfb') {
  660. mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
  661. }
  662. $this->dechanged = false;
  663. }
  664. if ($this->mode != 'ncfb' || !$this->continuousBuffer) {
  665. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  666. } else {
  667. $iv = &$this->decryptIV;
  668. $pos = &$this->debuffer['pos'];
  669. $len = strlen($ciphertext);
  670. $plaintext = '';
  671. $i = 0;
  672. if ($pos) {
  673. $orig_pos = $pos;
  674. $max = 8 - $pos;
  675. if ($len >= $max) {
  676. $i = $max;
  677. $len-= $max;
  678. $pos = 0;
  679. } else {
  680. $i = $len;
  681. $pos+= $len;
  682. $len = 0;
  683. }
  684. $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
  685. $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
  686. }
  687. if ($len >= 8) {
  688. $cb = substr($ciphertext, $i, $len - $len % 8);
  689. $plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
  690. $iv = substr($cb, -8);
  691. $len%= 8;
  692. }
  693. if ($len) {
  694. $iv = mcrypt_generic($this->ecb, $iv);
  695. $plaintext.= $iv ^ substr($ciphertext, -$len);
  696. $iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
  697. $pos = $len;
  698. }
  699. return $plaintext;
  700. }
  701. if (!$this->continuousBuffer) {
  702. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  703. }
  704. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  705. }
  706. if (!is_array($this->keys)) {
  707. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  708. }
  709. $buffer = &$this->debuffer;
  710. $continuousBuffer = $this->continuousBuffer;
  711. $plaintext = '';
  712. switch ($this->mode) {
  713. case CRYPT_DES_MODE_ECB:
  714. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  715. $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);
  716. }
  717. break;
  718. case CRYPT_DES_MODE_CBC:
  719. $xor = $this->decryptIV;
  720. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  721. $block = substr($ciphertext, $i, 8);
  722. $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;
  723. $xor = $block;
  724. }
  725. if ($this->continuousBuffer) {
  726. $this->decryptIV = $xor;
  727. }
  728. break;
  729. case CRYPT_DES_MODE_CTR:
  730. $xor = $this->decryptIV;
  731. if (strlen($buffer['ciphertext'])) {
  732. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  733. $block = substr($ciphertext, $i, 8);
  734. $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
  735. $key = $this->_string_shift($buffer['ciphertext'], 8);
  736. $plaintext.= $block ^ $key;
  737. }
  738. } else {
  739. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  740. $block = substr($ciphertext, $i, 8);
  741. $key = $this->_processBlock($this->_generate_xor($xor), CRYPT_DES_ENCRYPT);
  742. $plaintext.= $block ^ $key;
  743. }
  744. }
  745. if ($this->continuousBuffer) {
  746. $this->decryptIV = $xor;
  747. if ($start = strlen($ciphertext) % 8) {
  748. $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
  749. }
  750. }
  751. break;
  752. case CRYPT_DES_MODE_CFB:
  753. if ($this->continuousBuffer) {
  754. $iv = &$this->decryptIV;
  755. $pos = &$buffer['pos'];
  756. } else {
  757. $iv = $this->decryptIV;
  758. $pos = 0;
  759. }
  760. $len = strlen($ciphertext);
  761. $i = 0;
  762. if ($pos) {
  763. $orig_pos = $pos;
  764. $max = 8 - $pos;
  765. if ($len >= $max) {
  766. $i = $max;
  767. $len-= $max;
  768. $pos = 0;
  769. } else {
  770. $i = $len;
  771. $pos+= $len;
  772. $len = 0;
  773. }
  774. $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
  775. $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
  776. }
  777. while ($len >= 8) {
  778. $iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
  779. $cb = substr($ciphertext, $i, 8);
  780. $plaintext.= $iv ^ $cb;
  781. $iv = $cb;
  782. $len-= 8;
  783. $i+= 8;
  784. }
  785. if ($len) {
  786. $iv = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
  787. $plaintext.= $iv ^ substr($ciphertext, $i);
  788. $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
  789. $pos = $len;
  790. }
  791. return $plaintext;
  792. case CRYPT_DES_MODE_OFB:
  793. $xor = $this->decryptIV;
  794. if (strlen($buffer['xor'])) {
  795. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  796. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  797. $buffer['xor'].= $xor;
  798. $key = $this->_string_shift($buffer['xor'], 8);
  799. $plaintext.= substr($ciphertext, $i, 8) ^ $key;
  800. }
  801. } else {
  802. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  803. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  804. $plaintext.= substr($ciphertext, $i, 8) ^ $xor;
  805. }
  806. $key = $xor;
  807. }
  808. if ($this->continuousBuffer) {
  809. $this->decryptIV = $xor;
  810. if ($start = strlen($ciphertext) % 8) {
  811. $buffer['xor'] = substr($key, $start) . $buffer['xor'];
  812. }
  813. }
  814. }
  815. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  816. }
  817. /**
  818. * Treat consecutive "packets" as if they are a continuous buffer.
  819. *
  820. * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  821. * will yield different outputs:
  822. *
  823. * <code>
  824. * echo $des->encrypt(substr($plaintext, 0, 8));
  825. * echo $des->encrypt(substr($plaintext, 8, 8));
  826. * </code>
  827. * <code>
  828. * echo $des->encrypt($plaintext);
  829. * </code>
  830. *
  831. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  832. * another, as demonstrated with the following:
  833. *
  834. * <code>
  835. * $des->encrypt(substr($plaintext, 0, 8));
  836. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  837. * </code>
  838. * <code>
  839. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  840. * </code>
  841. *
  842. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  843. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  844. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  845. *
  846. * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  847. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  848. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  849. * however, they are also less intuitive and more likely to cause you problems.
  850. *
  851. * @see Crypt_DES::disableContinuousBuffer()
  852. * @access public
  853. */
  854. function enableContinuousBuffer()
  855. {
  856. $this->continuousBuffer = true;
  857. }
  858. /**
  859. * Treat consecutive packets as if they are a discontinuous buffer.
  860. *
  861. * The default behavior.
  862. *
  863. * @see Crypt_DES::enableContinuousBuffer()
  864. * @access public
  865. */
  866. function disableContinuousBuffer()
  867. {
  868. $this->continuousBuffer = false;
  869. $this->encryptIV = $this->iv;
  870. $this->decryptIV = $this->iv;
  871. $this->enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
  872. $this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'demcrypt_init' => true);
  873. if (CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT) {
  874. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->iv);
  875. mcrypt_generic_init($this->demcrypt, $this->keys, $this->iv);
  876. }
  877. }
  878. /**
  879. * Pad "packets".
  880. *
  881. * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
  882. * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
  883. *
  884. * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
  885. * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
  886. * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
  887. * transmitted separately)
  888. *
  889. * @see Crypt_DES::disablePadding()
  890. * @access public
  891. */
  892. function enablePadding()
  893. {
  894. $this->padding = true;
  895. }
  896. /**
  897. * Do not pad packets.
  898. *
  899. * @see Crypt_DES::enablePadding()
  900. * @access public
  901. */
  902. function disablePadding()
  903. {
  904. $this->padding = false;
  905. }
  906. /**
  907. * Pads a string
  908. *
  909. * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
  910. * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
  911. *
  912. * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
  913. * and padding will, hence forth, be enabled.
  914. *
  915. * @see Crypt_DES::_unpad()
  916. * @access private
  917. */
  918. function _pad($text)
  919. {
  920. $length = strlen($text);
  921. if (!$this->padding) {
  922. if (($length & 7) == 0) {
  923. return $text;
  924. } else {
  925. user_error("The plaintext's length ($length) is not a multiple of the block size (8)");
  926. $this->padding = true;
  927. }
  928. }
  929. $pad = 8 - ($length & 7);
  930. return str_pad($text, $length + $pad, chr($pad));
  931. }
  932. /**
  933. * Unpads a string
  934. *
  935. * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
  936. * and false will be returned.
  937. *
  938. * @see Crypt_DES::_pad()
  939. * @access private
  940. */
  941. function _unpad($text)
  942. {
  943. if (!$this->padding) {
  944. return $text;
  945. }
  946. $length = ord($text[strlen($text) - 1]);
  947. if (!$length || $length > 8) {
  948. return false;
  949. }
  950. return substr($text, 0, -$length);
  951. }
  952. /**
  953. * Encrypts or decrypts a 64-bit block
  954. *
  955. * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
  956. * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
  957. * idea of what this function does.
  958. *
  959. * @access private
  960. * @param String $block
  961. * @param Integer $mode
  962. * @return String
  963. */
  964. function _processBlock($block, $mode)
  965. {
  966. // s-boxes. in the official DES docs, they're described as being matrices that
  967. // one accesses by using the first and last bits to determine the row and the
  968. // middle four bits to determine the column. in this implementation, they've
  969. // been converted to vectors
  970. static $sbox = array(
  971. array(
  972. 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
  973. 3, 10 ,10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
  974. 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
  975. 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
  976. ),
  977. array(
  978. 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
  979. 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
  980. 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
  981. 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
  982. ),
  983. array(
  984. 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
  985. 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
  986. 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
  987. 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
  988. ),
  989. array(
  990. 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
  991. 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
  992. 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
  993. 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
  994. ),
  995. array(
  996. 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
  997. 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
  998. 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
  999. 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
  1000. ),
  1001. array(
  1002. 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
  1003. 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
  1004. 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
  1005. 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
  1006. ),
  1007. array(
  1008. 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
  1009. 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
  1010. 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
  1011. 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
  1012. ),
  1013. array(
  1014. 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
  1015. 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
  1016. 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
  1017. 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
  1018. )
  1019. );
  1020. $keys = $this->keys;
  1021. $temp = unpack('Na/Nb', $block);
  1022. $block = array($temp['a'], $temp['b']);
  1023. // because php does arithmetic right shifts, if the most significant bits are set, right
  1024. // shifting those into the correct position will add 1's - not 0's. this will intefere
  1025. // with the | operation unless a second & is done. so we isolate these bits and left shift
  1026. // them into place. we then & each block with 0x7FFFFFFF to prevennt 1's from being added
  1027. // for any other shifts.
  1028. $msb = array(
  1029. ($block[0] >> 31) & 1,
  1030. ($block[1] >> 31) & 1
  1031. );
  1032. $block[0] &= 0x7FFFFFFF;
  1033. $block[1] &= 0x7FFFFFFF;
  1034. // we isolate the appropriate bit in the appropriate integer and shift as appropriate. in
  1035. // some cases, there are going to be multiple bits in the same integer that need to be shifted
  1036. // in the same way. we combine those into one shift operation.
  1037. $block = array(
  1038. (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |
  1039. (($block[1] & 0x00400001) << 7) | (($block[1] & 0x40000100) >> 2) |
  1040. (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |
  1041. (($block[0] & 0x00400001) << 3) | (($block[0] & 0x40000100) >> 6) |
  1042. (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |
  1043. (($block[1] & 0x00100000) << 1) | (($block[1] & 0x10000000) >> 8) |
  1044. (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) << 6) |
  1045. (($block[0] & 0x00100000) >> 3) | (($block[0] & 0x10000000) >> 12) |
  1046. (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) << 4) |
  1047. (($block[1] & 0x00040000) >> 5) | (($block[1] & 0x04000000) >> 14) |
  1048. (($block[0] & 0x00000004) << 9) | ( $block[0] & 0x00000400 ) |
  1049. (($block[0] & 0x00040000) >> 9) | (($block[0] & 0x04000000) >> 18) |
  1050. (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |
  1051. (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)
  1052. ,
  1053. (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |
  1054. (($block[1] & 0x00800002) << 6) | (($block[0] & 0x00000080) << 20) |
  1055. (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) << 2) |
  1056. (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) << 9) |
  1057. ( $block[1] & 0x00200000 ) | (($block[1] & 0x20000000) >> 9) |
  1058. (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) << 5) |
  1059. (($block[0] & 0x00200000) >> 4) | (($block[0] & 0x20000000) >> 13) |
  1060. (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) << 3) |
  1061. (($block[1] & 0x00080000) >> 6) | (($block[1] & 0x08000000) >> 15) |
  1062. (($block[0] & 0x00000008) << 8) | (($block[0] & 0x00000800) >> 1) |
  1063. (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |
  1064. (($block[1] & 0x00000200) >> 3) | (($block[0] & 0x00000200) >> 7) |
  1065. (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |
  1066. (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |
  1067. ($msb[1] << 28) | ($msb[0] << 24)
  1068. );
  1069. for ($i = 0; $i < 16; $i++) {
  1070. // start of "the Feistel (F) function" - see the following URL:
  1071. // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
  1072. $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)
  1073. | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)
  1074. | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)
  1075. | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)
  1076. | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)
  1077. | (($sbox[5][(($block[1] & 0x00001F80) >> 7) ^ $keys[$mode][$i][5]]) << 8)
  1078. | (($sbox[6][(($block[1] & 0x000001F8) >> 3) ^ $keys[$mode][$i][6]]) << 4)
  1079. | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);
  1080. $msb = ($temp >> 31) & 1;
  1081. $temp &= 0x7FFFFFFF;
  1082. $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) << 5)
  1083. | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)
  1084. | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) << 6)
  1085. | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) << 9)
  1086. | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)
  1087. | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >> 8)
  1088. | (($temp & 0x00004000) << 4) | (($temp & 0x00000002) << 16)
  1089. | (($temp & 0x00442000) >> 6) | (($temp & 0x40800000) >> 15)
  1090. | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)
  1091. | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) << 3)
  1092. | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >> 7)
  1093. | (($temp & 0x00200000) >> 19) | ($msb << 23);
  1094. // end of "the Feistel (F) function" - $newBlock is F's output
  1095. $temp = $block[1];
  1096. $block[1] = $block[0] ^ $newBlock;
  1097. $block[0] = $temp;
  1098. }
  1099. $msb = array(
  1100. ($block[0] >> 31) & 1,
  1101. ($block[1] >> 31) & 1
  1102. );
  1103. $block[0] &= 0x7FFFFFFF;
  1104. $block[1] &= 0x7FFFFFFF;
  1105. $block = array(
  1106. (($block[0] & 0x01000004) << 7) | (($block[1] & 0x01000004) << 6) |
  1107. (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |
  1108. (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |
  1109. (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |
  1110. (($block[0] & 0x02000008) >> 2) | (($block[1] & 0x02000008) >> 3) |
  1111. (($block[0] & 0x00020000) << 4) | (($block[1] & 0x00020000) << 3) |
  1112. (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) << 9) |
  1113. (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |
  1114. (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |
  1115. (($block[0] & 0x00040000) >> 5) | (($block[1] & 0x00040000) >> 6) |
  1116. (($block[0] & 0x00000400) << 1) | ( $block[1] & 0x00000400 ) |
  1117. (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |
  1118. (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |
  1119. (($block[0] & 0x00000800) >> 8) | (($block[1] & 0x00000800) >> 9)
  1120. ,
  1121. (($block[0] & 0x10000040) << 3) | (($block[1] & 0x10000040) << 2) |
  1122. (($block[0] & 0x00100000) << 9) | (($block[1] & 0x00100000) << 8) |
  1123. (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |
  1124. (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |
  1125. (($block[0] & 0x20000080) >> 6) | (($block[1] & 0x20000080) >> 7) |
  1126. ( $block[0] & 0x00200000 ) | (($block[1] & 0x00200000) >> 1) |
  1127. (($block[0] & 0x00002000) << 6) | (($block[1] & 0x00002000) << 5) |
  1128. (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |
  1129. (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |
  1130. (($block[0] & 0x00400000) >> 9) | (($block[1] & 0x00400000) >> 10) |
  1131. (($block[0] & 0x00004000) >> 3) | (($block[1] & 0x00004000) >> 4) |
  1132. (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |
  1133. (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |
  1134. ($msb[0] << 7) | ($msb[1] << 6)
  1135. );
  1136. return pack('NN', $block[0], $block[1]);
  1137. }
  1138. /**
  1139. * Creates the key schedule.
  1140. *
  1141. * @access private
  1142. * @param String $key
  1143. * @return Array
  1144. */
  1145. function _prepareKey($key)
  1146. {
  1147. static $shifts = array( // number of key bits shifted per round
  1148. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  1149. );
  1150. // pad the key and remove extra characters as appropriate.
  1151. $key = str_pad(substr($key, 0, 8), 8, chr(0));
  1152. $temp = unpack('Na/Nb', $key);
  1153. $key = array($temp['a'], $temp['b']);
  1154. $msb = array(
  1155. ($key[0] >> 31) & 1,
  1156. ($key[1] >> 31) & 1
  1157. );
  1158. $key[0] &= 0x7FFFFFFF;
  1159. $key[1] &= 0x7FFFFFFF;
  1160. $key = array(
  1161. (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |
  1162. (($key[1] & 0x00020408) << 8) | (($key[1] & 0x02040800) >> 1) |
  1163. (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |
  1164. (($key[0] & 0x00020408) << 4) | (($key[0] & 0x02040800) >> 5) |
  1165. (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |
  1166. (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |
  1167. (($key[0] & 0x00000010) >> 1) | (($key[0] & 0x00001000) >> 10) |
  1168. (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)
  1169. ,
  1170. (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |
  1171. (($key[1] & 0x00800000) << 2) | (($key[0] & 0x00000080) << 16) |
  1172. (($key[0] & 0x00008000) << 7) | (($key[0] & 0x00800000) >> 2) |
  1173. (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) << 4) |
  1174. (($key[1] & 0x00400000) >> 5) | (($key[1] & 0x40000000) >> 14) |
  1175. (($key[0] & 0x00000040) << 9) | ( $key[0] & 0x00004000 ) |
  1176. (($key[0] & 0x00400000) >> 9) | (($key[0] & 0x40000000) >> 18) |
  1177. (($key[1] & 0x00000020) << 6) | (($key[1] & 0x00002000) >> 3) |
  1178. (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |
  1179. (($key[0] & 0x00000020) << 2) | (($key[0] & 0x00002000) >> 7) |
  1180. (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |
  1181. (($key[1] & 0x00000010) >> 1) | (($key[1] & 0x00001000) >> 10) |
  1182. (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |
  1183. ($msb[1] << 24) | ($msb[0] << 20)
  1184. );
  1185. $keys = array();
  1186. for ($i = 0; $i < 16; $i++) {
  1187. $key[0] <<= $shifts[$i];
  1188. $temp = ($key[0] & 0xF0000000) >> 28;
  1189. $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;
  1190. $key[1] <<= $shifts[$i];
  1191. $temp = ($key[1] & 0xF0000000) >> 28;
  1192. $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;
  1193. $temp = array(
  1194. (($key[1] & 0x00004000) >> 9) | (($key[1] & 0x00000800) >> 7) |
  1195. (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >> 2) |
  1196. (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)
  1197. ,
  1198. (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) << 4) |
  1199. (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |
  1200. (($key[1] & 0x00000080) >> 6)
  1201. ,
  1202. ( $key[1] & 0x00000020 ) | (($key[1] & 0x00000200) >> 5) |
  1203. (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |
  1204. (($key[1] & 0x00000004) >> 1) | (($key[1] & 0x00100000) >> 20)
  1205. ,
  1206. (($key[1] & 0x00001000) >> 7) | (($key[1] & 0x00200000) >> 17) |
  1207. (($key[1] & 0x00000002) << 2) | (($key[1] & 0x00000100) >> 6) |
  1208. (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)
  1209. ,
  1210. (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010 ) |
  1211. (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |
  1212. (($key[0] & 0x00000200) >> 8) | (($key[0] & 0x00000002) >> 1)
  1213. ,
  1214. (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |
  1215. (($key[0] & 0x00000020) >> 2) | (($key[0] & 0x00000800) >> 9) |
  1216. (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >> 8)
  1217. ,
  1218. (($key[0] & 0x00001000) >> 7) | (($key[0] & 0x00000088) >> 3) |
  1219. (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) << 2) |
  1220. (($key[0] & 0x00400000) >> 21)
  1221. ,
  1222. (($key[0] & 0x00000400) >> 5) | (($key[0] & 0x00004000) >> 10) |
  1223. (($key[0] & 0x00000040) >> 3) | (($key[0] & 0x00100000) >> 18) |
  1224. (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)
  1225. );
  1226. $keys[] = $temp;
  1227. }
  1228. $temp = array(
  1229. CRYPT_DES_ENCRYPT => $keys,
  1230. CRYPT_DES_DECRYPT => array_reverse($keys)
  1231. );
  1232. return $temp;
  1233. }
  1234. /**
  1235. * String Shift
  1236. *
  1237. * Inspired by array_shift
  1238. *
  1239. * @param String $string
  1240. * @param optional Integer $index
  1241. * @return String
  1242. * @access private
  1243. */
  1244. function _string_shift(&$string, $index = 1)
  1245. {
  1246. $substr = substr($string, 0, $index);
  1247. $string = substr($string, $index);
  1248. return $substr;
  1249. }
  1250. }
  1251. // vim: ts=4:sw=4:et:
  1252. // vim6: fdl=1: