PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/utility/phpseclib/Crypt/DES.php

https://github.com/cuijinquan/nextwind
PHP | 945 lines | 516 code | 64 blank | 365 comment | 49 complexity | c8d0b14055fa99f5b134b4e273d109ba MD5 | raw file
  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: This library is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU Lesser General Public
  37. * License as published by the Free Software Foundation; either
  38. * version 2.1 of the License, or (at your option) any later version.
  39. *
  40. * This library is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43. * Lesser General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU Lesser General Public
  46. * License along with this library; if not, write to the Free Software
  47. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  48. * MA 02111-1307 USA
  49. *
  50. * @category Crypt
  51. * @package Crypt_DES
  52. * @author Jim Wigginton <terrafrost@php.net>
  53. * @copyright MMVII Jim Wigginton
  54. * @license http://www.gnu.org/licenses/lgpl.txt
  55. * @version $Id: DES.php 21939 2012-12-17 07:13:16Z long.shi $
  56. * @link http://phpseclib.sourceforge.net
  57. */
  58. /**#@+
  59. * @access private
  60. * @see Crypt_DES::_prepareKey()
  61. * @see Crypt_DES::_processBlock()
  62. */
  63. /**
  64. * Contains array_reverse($keys[CRYPT_DES_DECRYPT])
  65. */
  66. define('CRYPT_DES_ENCRYPT', 0);
  67. /**
  68. * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])
  69. */
  70. define('CRYPT_DES_DECRYPT', 1);
  71. /**#@-*/
  72. /**#@+
  73. * @access public
  74. * @see Crypt_DES::encrypt()
  75. * @see Crypt_DES::decrypt()
  76. */
  77. /**
  78. * Encrypt / decrypt using the Counter mode.
  79. *
  80. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  81. *
  82. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  83. */
  84. define('CRYPT_DES_MODE_CTR', -1);
  85. /**
  86. * Encrypt / decrypt using the Electronic Code Book mode.
  87. *
  88. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  89. */
  90. define('CRYPT_DES_MODE_ECB', 1);
  91. /**
  92. * Encrypt / decrypt using the Code Book Chaining mode.
  93. *
  94. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  95. */
  96. define('CRYPT_DES_MODE_CBC', 2);
  97. /**#@-*/
  98. /**#@+
  99. * @access private
  100. * @see Crypt_DES::Crypt_DES()
  101. */
  102. /**
  103. * Toggles the internal implementation
  104. */
  105. define('CRYPT_DES_MODE_INTERNAL', 1);
  106. /**
  107. * Toggles the mcrypt implementation
  108. */
  109. define('CRYPT_DES_MODE_MCRYPT', 2);
  110. /**#@-*/
  111. /**
  112. * Pure-PHP implementation of DES.
  113. *
  114. * @author Jim Wigginton <terrafrost@php.net>
  115. * @version 0.1.0
  116. * @access public
  117. * @package Crypt_DES
  118. */
  119. class Crypt_DES {
  120. /**
  121. * The Key Schedule
  122. *
  123. * @see Crypt_DES::setKey()
  124. * @var Array
  125. * @access private
  126. */
  127. var $keys = "\0\0\0\0\0\0\0\0";
  128. /**
  129. * The Encryption Mode
  130. *
  131. * @see Crypt_DES::Crypt_DES()
  132. * @var Integer
  133. * @access private
  134. */
  135. var $mode;
  136. /**
  137. * Continuous Buffer status
  138. *
  139. * @see Crypt_DES::enableContinuousBuffer()
  140. * @var Boolean
  141. * @access private
  142. */
  143. var $continuousBuffer = false;
  144. /**
  145. * Padding status
  146. *
  147. * @see Crypt_DES::enablePadding()
  148. * @var Boolean
  149. * @access private
  150. */
  151. var $padding = true;
  152. /**
  153. * The Initialization Vector
  154. *
  155. * @see Crypt_DES::setIV()
  156. * @var String
  157. * @access private
  158. */
  159. var $iv = "\0\0\0\0\0\0\0\0";
  160. /**
  161. * A "sliding" Initialization Vector
  162. *
  163. * @see Crypt_DES::enableContinuousBuffer()
  164. * @var String
  165. * @access private
  166. */
  167. var $encryptIV = "\0\0\0\0\0\0\0\0";
  168. /**
  169. * A "sliding" Initialization Vector
  170. *
  171. * @see Crypt_DES::enableContinuousBuffer()
  172. * @var String
  173. * @access private
  174. */
  175. var $decryptIV = "\0\0\0\0\0\0\0\0";
  176. /**
  177. * mcrypt resource for encryption
  178. *
  179. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  180. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  181. *
  182. * @see Crypt_AES::encrypt()
  183. * @var String
  184. * @access private
  185. */
  186. var $enmcrypt;
  187. /**
  188. * mcrypt resource for decryption
  189. *
  190. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  191. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  192. *
  193. * @see Crypt_AES::decrypt()
  194. * @var String
  195. * @access private
  196. */
  197. var $demcrypt;
  198. /**
  199. * Does the (en|de)mcrypt resource need to be (re)initialized?
  200. *
  201. * @see setKey()
  202. * @see setIV()
  203. * @var Boolean
  204. * @access private
  205. */
  206. var $changed = true;
  207. /**
  208. * Default Constructor.
  209. *
  210. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  211. * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
  212. *
  213. * @param optional Integer $mode
  214. * @return Crypt_DES
  215. * @access public
  216. */
  217. function Crypt_DES($mode = CRYPT_MODE_DES_CBC)
  218. {
  219. if ( !defined('CRYPT_DES_MODE') ) {
  220. switch (true) {
  221. case extension_loaded('mcrypt'):
  222. // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),
  223. // but since that can be changed after the object has been created, there doesn't seem to be
  224. // a lot of point...
  225. define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
  226. break;
  227. default:
  228. define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
  229. }
  230. }
  231. switch ( CRYPT_DES_MODE ) {
  232. case CRYPT_DES_MODE_MCRYPT:
  233. switch ($mode) {
  234. case CRYPT_DES_MODE_ECB:
  235. $this->mode = MCRYPT_MODE_ECB;
  236. break;
  237. case CRYPT_DES_MODE_CTR:
  238. $this->mode = 'ctr';
  239. //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;
  240. break;
  241. case CRYPT_DES_MODE_CBC:
  242. default:
  243. $this->mode = MCRYPT_MODE_CBC;
  244. }
  245. break;
  246. default:
  247. switch ($mode) {
  248. case CRYPT_DES_MODE_ECB:
  249. case CRYPT_DES_MODE_CTR:
  250. case CRYPT_DES_MODE_CBC:
  251. $this->mode = $mode;
  252. break;
  253. default:
  254. $this->mode = CRYPT_DES_MODE_CBC;
  255. }
  256. }
  257. }
  258. /**
  259. * Sets the key.
  260. *
  261. * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
  262. * only use the first eight, if $key has more then eight characters in it, and pad $key with the
  263. * null byte if it is less then eight characters long.
  264. *
  265. * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
  266. *
  267. * If the key is not explicitly set, it'll be assumed to be all zero's.
  268. *
  269. * @access public
  270. * @param String $key
  271. */
  272. function setKey($key)
  273. {
  274. $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? substr($key, 0, 8) : $this->_prepareKey($key);
  275. $this->changed = true;
  276. }
  277. /**
  278. * Sets the initialization vector. (optional)
  279. *
  280. * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
  281. * to be all zero's.
  282. *
  283. * @access public
  284. * @param String $iv
  285. */
  286. function setIV($iv)
  287. {
  288. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
  289. $this->changed = true;
  290. }
  291. /**
  292. * Generate CTR XOR encryption key
  293. *
  294. * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
  295. * plaintext / ciphertext in CTR mode.
  296. *
  297. * @see Crypt_DES::decrypt()
  298. * @see Crypt_DES::encrypt()
  299. * @access public
  300. * @param Integer $length
  301. * @param String $iv
  302. */
  303. function _generate_xor($length, &$iv)
  304. {
  305. $xor = '';
  306. $num_blocks = ($length + 7) >> 3;
  307. for ($i = 0; $i < $num_blocks; $i++) {
  308. $xor.= $iv;
  309. for ($j = 4; $j <= 8; $j+=4) {
  310. $temp = substr($iv, -$j, 4);
  311. switch ($temp) {
  312. case "\xFF\xFF\xFF\xFF":
  313. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  314. break;
  315. case "\x7F\xFF\xFF\xFF":
  316. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  317. break 2;
  318. default:
  319. extract(unpack('Ncount', $temp));
  320. $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  321. break 2;
  322. }
  323. }
  324. }
  325. return $xor;
  326. }
  327. /**
  328. * Encrypts a message.
  329. *
  330. * $plaintext will be padded with up to 8 additional bytes. Other DES implementations may or may not pad in the
  331. * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
  332. * URL:
  333. *
  334. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  335. *
  336. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  337. * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
  338. * length.
  339. *
  340. * @see Crypt_DES::decrypt()
  341. * @access public
  342. * @param String $plaintext
  343. */
  344. function encrypt($plaintext)
  345. {
  346. if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {
  347. $plaintext = $this->_pad($plaintext);
  348. }
  349. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  350. if ($this->changed) {
  351. if (!isset($this->enmcrypt)) {
  352. $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  353. }
  354. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  355. $this->changed = false;
  356. }
  357. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  358. if (!$this->continuousBuffer) {
  359. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  360. }
  361. return $ciphertext;
  362. }
  363. if (!is_array($this->keys)) {
  364. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  365. }
  366. $ciphertext = '';
  367. switch ($this->mode) {
  368. case CRYPT_DES_MODE_ECB:
  369. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  370. $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);
  371. }
  372. break;
  373. case CRYPT_DES_MODE_CBC:
  374. $xor = $this->encryptIV;
  375. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  376. $block = substr($plaintext, $i, 8);
  377. $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);
  378. $xor = $block;
  379. $ciphertext.= $block;
  380. }
  381. if ($this->continuousBuffer) {
  382. $this->encryptIV = $xor;
  383. }
  384. break;
  385. case CRYPT_DES_MODE_CTR:
  386. $xor = $this->encryptIV;
  387. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  388. $block = substr($plaintext, $i, 8);
  389. $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  390. $ciphertext.= $block ^ $key;
  391. }
  392. if ($this->continuousBuffer) {
  393. $this->encryptIV = $xor;
  394. }
  395. }
  396. return $ciphertext;
  397. }
  398. /**
  399. * Decrypts a message.
  400. *
  401. * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.
  402. *
  403. * @see Crypt_DES::encrypt()
  404. * @access public
  405. * @param String $ciphertext
  406. */
  407. function decrypt($ciphertext)
  408. {
  409. if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {
  410. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  411. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  412. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
  413. }
  414. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  415. if ($this->changed) {
  416. if (!isset($this->demcrypt)) {
  417. $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  418. }
  419. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  420. $this->changed = false;
  421. }
  422. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  423. if (!$this->continuousBuffer) {
  424. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  425. }
  426. return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;
  427. }
  428. if (!is_array($this->keys)) {
  429. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  430. }
  431. $plaintext = '';
  432. switch ($this->mode) {
  433. case CRYPT_DES_MODE_ECB:
  434. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  435. $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);
  436. }
  437. break;
  438. case CRYPT_DES_MODE_CBC:
  439. $xor = $this->decryptIV;
  440. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  441. $block = substr($ciphertext, $i, 8);
  442. $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;
  443. $xor = $block;
  444. }
  445. if ($this->continuousBuffer) {
  446. $this->decryptIV = $xor;
  447. }
  448. break;
  449. case CRYPT_DES_MODE_CTR:
  450. $xor = $this->decryptIV;
  451. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  452. $block = substr($ciphertext, $i, 8);
  453. $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  454. $plaintext.= $block ^ $key;
  455. }
  456. if ($this->continuousBuffer) {
  457. $this->decryptIV = $xor;
  458. }
  459. }
  460. return $this->mode != CRYPT_DES_MODE_CTR ? $this->_unpad($plaintext) : $plaintext;
  461. }
  462. /**
  463. * Treat consecutive "packets" as if they are a continuous buffer.
  464. *
  465. * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  466. * will yield different outputs:
  467. *
  468. * <code>
  469. * echo $des->encrypt(substr($plaintext, 0, 8));
  470. * echo $des->encrypt(substr($plaintext, 8, 8));
  471. * </code>
  472. * <code>
  473. * echo $des->encrypt($plaintext);
  474. * </code>
  475. *
  476. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  477. * another, as demonstrated with the following:
  478. *
  479. * <code>
  480. * $des->encrypt(substr($plaintext, 0, 8));
  481. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  482. * </code>
  483. * <code>
  484. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  485. * </code>
  486. *
  487. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  488. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  489. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  490. *
  491. * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  492. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  493. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  494. * however, they are also less intuitive and more likely to cause you problems.
  495. *
  496. * @see Crypt_DES::disableContinuousBuffer()
  497. * @access public
  498. */
  499. function enableContinuousBuffer()
  500. {
  501. $this->continuousBuffer = true;
  502. }
  503. /**
  504. * Treat consecutive packets as if they are a discontinuous buffer.
  505. *
  506. * The default behavior.
  507. *
  508. * @see Crypt_DES::enableContinuousBuffer()
  509. * @access public
  510. */
  511. function disableContinuousBuffer()
  512. {
  513. $this->continuousBuffer = false;
  514. $this->encryptIV = $this->iv;
  515. $this->decryptIV = $this->iv;
  516. }
  517. /**
  518. * Pad "packets".
  519. *
  520. * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
  521. * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
  522. *
  523. * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
  524. * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
  525. * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
  526. * transmitted separately)
  527. *
  528. * @see Crypt_DES::disablePadding()
  529. * @access public
  530. */
  531. function enablePadding()
  532. {
  533. $this->padding = true;
  534. }
  535. /**
  536. * Do not pad packets.
  537. *
  538. * @see Crypt_DES::enablePadding()
  539. * @access public
  540. */
  541. function disablePadding()
  542. {
  543. $this->padding = false;
  544. }
  545. /**
  546. * Pads a string
  547. *
  548. * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
  549. * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
  550. *
  551. * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
  552. * and padding will, hence forth, be enabled.
  553. *
  554. * @see Crypt_DES::_unpad()
  555. * @access private
  556. */
  557. function _pad($text)
  558. {
  559. $length = strlen($text);
  560. if (!$this->padding) {
  561. if (($length & 7) == 0) {
  562. return $text;
  563. } else {
  564. user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
  565. $this->padding = true;
  566. }
  567. }
  568. $pad = 8 - ($length & 7);
  569. return str_pad($text, $length + $pad, chr($pad));
  570. }
  571. /**
  572. * Unpads a string
  573. *
  574. * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
  575. * and false will be returned.
  576. *
  577. * @see Crypt_DES::_pad()
  578. * @access private
  579. */
  580. function _unpad($text)
  581. {
  582. if (!$this->padding) {
  583. return $text;
  584. }
  585. $length = ord($text[strlen($text) - 1]);
  586. if (!$length || $length > 8) {
  587. return false;
  588. }
  589. return substr($text, 0, -$length);
  590. }
  591. /**
  592. * Encrypts or decrypts a 64-bit block
  593. *
  594. * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
  595. * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
  596. * idea of what this function does.
  597. *
  598. * @access private
  599. * @param String $block
  600. * @param Integer $mode
  601. * @return String
  602. */
  603. function _processBlock($block, $mode)
  604. {
  605. // s-boxes. in the official DES docs, they're described as being matrices that
  606. // one accesses by using the first and last bits to determine the row and the
  607. // middle four bits to determine the column. in this implementation, they've
  608. // been converted to vectors
  609. static $sbox = array(
  610. array(
  611. 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
  612. 3, 10 ,10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
  613. 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
  614. 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
  615. ),
  616. array(
  617. 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
  618. 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
  619. 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
  620. 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
  621. ),
  622. array(
  623. 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
  624. 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
  625. 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
  626. 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
  627. ),
  628. array(
  629. 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
  630. 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
  631. 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
  632. 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
  633. ),
  634. array(
  635. 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
  636. 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
  637. 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
  638. 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
  639. ),
  640. array(
  641. 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
  642. 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
  643. 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
  644. 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
  645. ),
  646. array(
  647. 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
  648. 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
  649. 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
  650. 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
  651. ),
  652. array(
  653. 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
  654. 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
  655. 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
  656. 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
  657. )
  658. );
  659. $keys = $this->keys;
  660. $temp = unpack('Na/Nb', $block);
  661. $block = array($temp['a'], $temp['b']);
  662. // because php does arithmetic right shifts, if the most significant bits are set, right
  663. // shifting those into the correct position will add 1's - not 0's. this will intefere
  664. // with the | operation unless a second & is done. so we isolate these bits and left shift
  665. // them into place. we then & each block with 0x7FFFFFFF to prevennt 1's from being added
  666. // for any other shifts.
  667. $msb = array(
  668. ($block[0] >> 31) & 1,
  669. ($block[1] >> 31) & 1
  670. );
  671. $block[0] &= 0x7FFFFFFF;
  672. $block[1] &= 0x7FFFFFFF;
  673. // we isolate the appropriate bit in the appropriate integer and shift as appropriate. in
  674. // some cases, there are going to be multiple bits in the same integer that need to be shifted
  675. // in the same way. we combine those into one shift operation.
  676. $block = array(
  677. (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |
  678. (($block[1] & 0x00400001) << 7) | (($block[1] & 0x40000100) >> 2) |
  679. (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |
  680. (($block[0] & 0x00400001) << 3) | (($block[0] & 0x40000100) >> 6) |
  681. (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |
  682. (($block[1] & 0x00100000) << 1) | (($block[1] & 0x10000000) >> 8) |
  683. (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) << 6) |
  684. (($block[0] & 0x00100000) >> 3) | (($block[0] & 0x10000000) >> 12) |
  685. (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) << 4) |
  686. (($block[1] & 0x00040000) >> 5) | (($block[1] & 0x04000000) >> 14) |
  687. (($block[0] & 0x00000004) << 9) | ( $block[0] & 0x00000400 ) |
  688. (($block[0] & 0x00040000) >> 9) | (($block[0] & 0x04000000) >> 18) |
  689. (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |
  690. (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)
  691. ,
  692. (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |
  693. (($block[1] & 0x00800002) << 6) | (($block[0] & 0x00000080) << 20) |
  694. (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) << 2) |
  695. (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) << 9) |
  696. ( $block[1] & 0x00200000 ) | (($block[1] & 0x20000000) >> 9) |
  697. (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) << 5) |
  698. (($block[0] & 0x00200000) >> 4) | (($block[0] & 0x20000000) >> 13) |
  699. (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) << 3) |
  700. (($block[1] & 0x00080000) >> 6) | (($block[1] & 0x08000000) >> 15) |
  701. (($block[0] & 0x00000008) << 8) | (($block[0] & 0x00000800) >> 1) |
  702. (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |
  703. (($block[1] & 0x00000200) >> 3) | (($block[0] & 0x00000200) >> 7) |
  704. (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |
  705. (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |
  706. ($msb[1] << 28) | ($msb[0] << 24)
  707. );
  708. for ($i = 0; $i < 16; $i++) {
  709. // start of "the Feistel (F) function" - see the following URL:
  710. // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
  711. $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)
  712. | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)
  713. | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)
  714. | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)
  715. | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)
  716. | (($sbox[5][(($block[1] & 0x00001F80) >> 7) ^ $keys[$mode][$i][5]]) << 8)
  717. | (($sbox[6][(($block[1] & 0x000001F8) >> 3) ^ $keys[$mode][$i][6]]) << 4)
  718. | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);
  719. $msb = ($temp >> 31) & 1;
  720. $temp &= 0x7FFFFFFF;
  721. $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) << 5)
  722. | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)
  723. | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) << 6)
  724. | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) << 9)
  725. | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)
  726. | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >> 8)
  727. | (($temp & 0x00004000) << 4) | (($temp & 0x00000002) << 16)
  728. | (($temp & 0x00442000) >> 6) | (($temp & 0x40800000) >> 15)
  729. | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)
  730. | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) << 3)
  731. | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >> 7)
  732. | (($temp & 0x00200000) >> 19) | ($msb << 23);
  733. // end of "the Feistel (F) function" - $newBlock is F's output
  734. $temp = $block[1];
  735. $block[1] = $block[0] ^ $newBlock;
  736. $block[0] = $temp;
  737. }
  738. $msb = array(
  739. ($block[0] >> 31) & 1,
  740. ($block[1] >> 31) & 1
  741. );
  742. $block[0] &= 0x7FFFFFFF;
  743. $block[1] &= 0x7FFFFFFF;
  744. $block = array(
  745. (($block[0] & 0x01000004) << 7) | (($block[1] & 0x01000004) << 6) |
  746. (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |
  747. (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |
  748. (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |
  749. (($block[0] & 0x02000008) >> 2) | (($block[1] & 0x02000008) >> 3) |
  750. (($block[0] & 0x00020000) << 4) | (($block[1] & 0x00020000) << 3) |
  751. (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) << 9) |
  752. (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |
  753. (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |
  754. (($block[0] & 0x00040000) >> 5) | (($block[1] & 0x00040000) >> 6) |
  755. (($block[0] & 0x00000400) << 1) | ( $block[1] & 0x00000400 ) |
  756. (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |
  757. (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |
  758. (($block[0] & 0x00000800) >> 8) | (($block[1] & 0x00000800) >> 9)
  759. ,
  760. (($block[0] & 0x10000040) << 3) | (($block[1] & 0x10000040) << 2) |
  761. (($block[0] & 0x00100000) << 9) | (($block[1] & 0x00100000) << 8) |
  762. (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |
  763. (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |
  764. (($block[0] & 0x20000080) >> 6) | (($block[1] & 0x20000080) >> 7) |
  765. ( $block[0] & 0x00200000 ) | (($block[1] & 0x00200000) >> 1) |
  766. (($block[0] & 0x00002000) << 6) | (($block[1] & 0x00002000) << 5) |
  767. (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |
  768. (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |
  769. (($block[0] & 0x00400000) >> 9) | (($block[1] & 0x00400000) >> 10) |
  770. (($block[0] & 0x00004000) >> 3) | (($block[1] & 0x00004000) >> 4) |
  771. (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |
  772. (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |
  773. ($msb[0] << 7) | ($msb[1] << 6)
  774. );
  775. return pack('NN', $block[0], $block[1]);
  776. }
  777. /**
  778. * Creates the key schedule.
  779. *
  780. * @access private
  781. * @param String $key
  782. * @return Array
  783. */
  784. function _prepareKey($key)
  785. {
  786. static $shifts = array( // number of key bits shifted per round
  787. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  788. );
  789. // pad the key and remove extra characters as appropriate.
  790. $key = str_pad(substr($key, 0, 8), 8, chr(0));
  791. $temp = unpack('Na/Nb', $key);
  792. $key = array($temp['a'], $temp['b']);
  793. $msb = array(
  794. ($key[0] >> 31) & 1,
  795. ($key[1] >> 31) & 1
  796. );
  797. $key[0] &= 0x7FFFFFFF;
  798. $key[1] &= 0x7FFFFFFF;
  799. $key = array(
  800. (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |
  801. (($key[1] & 0x00020408) << 8) | (($key[1] & 0x02040800) >> 1) |
  802. (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |
  803. (($key[0] & 0x00020408) << 4) | (($key[0] & 0x02040800) >> 5) |
  804. (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |
  805. (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |
  806. (($key[0] & 0x00000010) >> 1) | (($key[0] & 0x00001000) >> 10) |
  807. (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)
  808. ,
  809. (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |
  810. (($key[1] & 0x00800000) << 2) | (($key[0] & 0x00000080) << 16) |
  811. (($key[0] & 0x00008000) << 7) | (($key[0] & 0x00800000) >> 2) |
  812. (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) << 4) |
  813. (($key[1] & 0x00400000) >> 5) | (($key[1] & 0x40000000) >> 14) |
  814. (($key[0] & 0x00000040) << 9) | ( $key[0] & 0x00004000 ) |
  815. (($key[0] & 0x00400000) >> 9) | (($key[0] & 0x40000000) >> 18) |
  816. (($key[1] & 0x00000020) << 6) | (($key[1] & 0x00002000) >> 3) |
  817. (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |
  818. (($key[0] & 0x00000020) << 2) | (($key[0] & 0x00002000) >> 7) |
  819. (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |
  820. (($key[1] & 0x00000010) >> 1) | (($key[1] & 0x00001000) >> 10) |
  821. (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |
  822. ($msb[1] << 24) | ($msb[0] << 20)
  823. );
  824. $keys = array();
  825. for ($i = 0; $i < 16; $i++) {
  826. $key[0] <<= $shifts[$i];
  827. $temp = ($key[0] & 0xF0000000) >> 28;
  828. $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;
  829. $key[1] <<= $shifts[$i];
  830. $temp = ($key[1] & 0xF0000000) >> 28;
  831. $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;
  832. $temp = array(
  833. (($key[1] & 0x00004000) >> 9) | (($key[1] & 0x00000800) >> 7) |
  834. (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >> 2) |
  835. (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)
  836. ,
  837. (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) << 4) |
  838. (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |
  839. (($key[1] & 0x00000080) >> 6)
  840. ,
  841. ( $key[1] & 0x00000020 ) | (($key[1] & 0x00000200) >> 5) |
  842. (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |
  843. (($key[1] & 0x00000004) >> 1) | (($key[1] & 0x00100000) >> 20)
  844. ,
  845. (($key[1] & 0x00001000) >> 7) | (($key[1] & 0x00200000) >> 17) |
  846. (($key[1] & 0x00000002) << 2) | (($key[1] & 0x00000100) >> 6) |
  847. (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)
  848. ,
  849. (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010 ) |
  850. (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |
  851. (($key[0] & 0x00000200) >> 8) | (($key[0] & 0x00000002) >> 1)
  852. ,
  853. (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |
  854. (($key[0] & 0x00000020) >> 2) | (($key[0] & 0x00000800) >> 9) |
  855. (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >> 8)
  856. ,
  857. (($key[0] & 0x00001000) >> 7) | (($key[0] & 0x00000088) >> 3) |
  858. (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) << 2) |
  859. (($key[0] & 0x00400000) >> 21)
  860. ,
  861. (($key[0] & 0x00000400) >> 5) | (($key[0] & 0x00004000) >> 10) |
  862. (($key[0] & 0x00000040) >> 3) | (($key[0] & 0x00100000) >> 18) |
  863. (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)
  864. );
  865. $keys[] = $temp;
  866. }
  867. $temp = array(
  868. CRYPT_DES_ENCRYPT => $keys,
  869. CRYPT_DES_DECRYPT => array_reverse($keys)
  870. );
  871. return $temp;
  872. }
  873. }
  874. // vim: ts=4:sw=4:et:
  875. // vim6: fdl=1: