DES.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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 String
  244. * @access private
  245. */
  246. var $enbuffer = '';
  247. /**
  248. * Decryption buffer for CTR, OFB and CFB modes
  249. *
  250. * @see Crypt_DES::decrypt()
  251. * @var String
  252. * @access private
  253. */
  254. var $debuffer = '';
  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_MODE_DES_CBC)
  275. {
  276. if ( !defined('CRYPT_DES_MODE') ) {
  277. switch (true) {
  278. case extension_loaded('mcrypt'):
  279. // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),
  280. // but since that can be changed after the object has been created, there doesn't seem to be
  281. // a lot of point...
  282. define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
  283. break;
  284. default:
  285. define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
  286. }
  287. }
  288. switch ( CRYPT_DES_MODE ) {
  289. case CRYPT_DES_MODE_MCRYPT:
  290. switch ($mode) {
  291. case CRYPT_DES_MODE_ECB:
  292. $this->paddable = true;
  293. $this->mode = MCRYPT_MODE_ECB;
  294. break;
  295. case CRYPT_DES_MODE_CTR:
  296. $this->mode = 'ctr';
  297. //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;
  298. break;
  299. case CRYPT_DES_MODE_CFB:
  300. $this->mode = 'ncfb';
  301. break;
  302. case CRYPT_DES_MODE_OFB:
  303. $this->mode = MCRYPT_MODE_NOFB;
  304. break;
  305. case CRYPT_DES_MODE_CBC:
  306. default:
  307. $this->paddable = true;
  308. $this->mode = MCRYPT_MODE_CBC;
  309. }
  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->changed = true;
  347. }
  348. /**
  349. * Sets the initialization vector. (optional)
  350. *
  351. * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
  352. * to be all zero's.
  353. *
  354. * @access public
  355. * @param String $iv
  356. */
  357. function setIV($iv)
  358. {
  359. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
  360. $this->changed = true;
  361. }
  362. /**
  363. * Generate CTR XOR encryption key
  364. *
  365. * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
  366. * plaintext / ciphertext in CTR mode.
  367. *
  368. * @see Crypt_DES::decrypt()
  369. * @see Crypt_DES::encrypt()
  370. * @access public
  371. * @param Integer $length
  372. * @param String $iv
  373. */
  374. function _generate_xor($length, &$iv)
  375. {
  376. $xor = '';
  377. $num_blocks = ($length + 7) >> 3;
  378. for ($i = 0; $i < $num_blocks; $i++) {
  379. $xor.= $iv;
  380. for ($j = 4; $j <= 8; $j+=4) {
  381. $temp = substr($iv, -$j, 4);
  382. switch ($temp) {
  383. case "\xFF\xFF\xFF\xFF":
  384. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  385. break;
  386. case "\x7F\xFF\xFF\xFF":
  387. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  388. break 2;
  389. default:
  390. extract(unpack('Ncount', $temp));
  391. $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  392. break 2;
  393. }
  394. }
  395. }
  396. return $xor;
  397. }
  398. /**
  399. * Encrypts a message.
  400. *
  401. * $plaintext will be padded with up to 8 additional bytes. Other DES implementations may or may not pad in the
  402. * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
  403. * URL:
  404. *
  405. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  406. *
  407. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  408. * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
  409. * length.
  410. *
  411. * @see Crypt_DES::decrypt()
  412. * @access public
  413. * @param String $plaintext
  414. */
  415. function encrypt($plaintext)
  416. {
  417. if ($this->paddable) {
  418. $plaintext = $this->_pad($plaintext);
  419. }
  420. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  421. if ($this->enchanged) {
  422. if (!isset($this->enmcrypt)) {
  423. $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  424. }
  425. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  426. if ($this->mode != 'ncfb') {
  427. $this->enchanged = false;
  428. }
  429. }
  430. if ($this->mode != 'ncfb') {
  431. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  432. } else {
  433. if ($this->enchanged) {
  434. $this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
  435. mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
  436. $this->enchanged = false;
  437. }
  438. if (strlen($this->enbuffer)) {
  439. $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer));
  440. $this->enbuffer.= $ciphertext;
  441. if (strlen($this->enbuffer) == 8) {
  442. $this->encryptIV = $this->enbuffer;
  443. $this->enbuffer = '';
  444. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  445. }
  446. $plaintext = substr($plaintext, strlen($ciphertext));
  447. } else {
  448. $ciphertext = '';
  449. }
  450. $last_pos = strlen($plaintext) & 0xFFFFFFF8;
  451. $ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : '';
  452. if (strlen($plaintext) & 0x7) {
  453. if (strlen($ciphertext)) {
  454. $this->encryptIV = substr($ciphertext, -8);
  455. }
  456. $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
  457. $this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
  458. $ciphertext.= $this->enbuffer;
  459. }
  460. }
  461. if (!$this->continuousBuffer) {
  462. mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);
  463. }
  464. return $ciphertext;
  465. }
  466. if (!is_array($this->keys)) {
  467. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  468. }
  469. $buffer = &$this->enbuffer;
  470. $continuousBuffer = $this->continuousBuffer;
  471. $ciphertext = '';
  472. switch ($this->mode) {
  473. case CRYPT_DES_MODE_ECB:
  474. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  475. $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);
  476. }
  477. break;
  478. case CRYPT_DES_MODE_CBC:
  479. $xor = $this->encryptIV;
  480. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  481. $block = substr($plaintext, $i, 8);
  482. $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);
  483. $xor = $block;
  484. $ciphertext.= $block;
  485. }
  486. if ($this->continuousBuffer) {
  487. $this->encryptIV = $xor;
  488. }
  489. break;
  490. case CRYPT_DES_MODE_CTR:
  491. $xor = $this->encryptIV;
  492. if (strlen($buffer)) {
  493. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  494. $block = substr($plaintext, $i, 8);
  495. $buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  496. $key = $this->_string_shift($buffer, 8);
  497. $ciphertext.= $block ^ $key;
  498. }
  499. } else {
  500. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  501. $block = substr($plaintext, $i, 8);
  502. $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  503. $ciphertext.= $block ^ $key;
  504. }
  505. }
  506. if ($this->continuousBuffer) {
  507. $this->encryptIV = $xor;
  508. if ($start = strlen($plaintext) & 7) {
  509. $buffer = substr($key, $start) . $buffer;
  510. }
  511. }
  512. break;
  513. case CRYPT_DES_MODE_CFB:
  514. if (!empty($buffer['xor'])) {
  515. $ciphertext = $plaintext ^ $buffer['xor'];
  516. $iv = $buffer['encrypted'] . $ciphertext;
  517. $start = strlen($ciphertext);
  518. $buffer['encrypted'].= $ciphertext;
  519. $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
  520. } else {
  521. $ciphertext = '';
  522. $iv = $this->encryptIV;
  523. $start = 0;
  524. }
  525. for ($i = $start; $i < strlen($plaintext); $i+=8) {
  526. $block = substr($plaintext, $i, 8);
  527. $xor = $this->_processBlock($iv, CRYPT_DES_ENCRYPT);
  528. $iv = $block ^ $xor;
  529. if ($continuousBuffer && strlen($iv) != 8) {
  530. $buffer = array(
  531. 'encrypted' => $iv,
  532. 'xor' => substr($xor, strlen($iv))
  533. );
  534. }
  535. $ciphertext.= $iv;
  536. }
  537. if ($this->continuousBuffer) {
  538. $this->encryptIV = $iv;
  539. }
  540. break;
  541. case CRYPT_DES_MODE_OFB:
  542. $xor = $this->encryptIV;
  543. if (strlen($buffer)) {
  544. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  545. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  546. $buffer.= $xor;
  547. $key = $this->_string_shift($buffer, 8);
  548. $ciphertext.= substr($plaintext, $i, 8) ^ $key;
  549. }
  550. } else {
  551. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  552. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  553. $ciphertext.= substr($plaintext, $i, 8) ^ $xor;
  554. }
  555. $key = $xor;
  556. }
  557. if ($this->continuousBuffer) {
  558. $this->encryptIV = $xor;
  559. if ($start = strlen($plaintext) & 7) {
  560. $buffer = substr($key, $start) . $buffer;
  561. }
  562. }
  563. }
  564. return $ciphertext;
  565. }
  566. /**
  567. * Decrypts a message.
  568. *
  569. * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.
  570. *
  571. * @see Crypt_DES::encrypt()
  572. * @access public
  573. * @param String $ciphertext
  574. */
  575. function decrypt($ciphertext)
  576. {
  577. if ($this->paddable) {
  578. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  579. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  580. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
  581. }
  582. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  583. if ($this->dechanged) {
  584. if (!isset($this->demcrypt)) {
  585. $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');
  586. }
  587. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  588. if ($this->mode != 'ncfb') {
  589. $this->dechanged = false;
  590. }
  591. }
  592. if ($this->mode != 'ncfb') {
  593. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  594. } else {
  595. if ($this->dechanged) {
  596. $this->ecb = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
  597. mcrypt_generic_init($this->ecb, $this->keys, "\0\0\0\0\0\0\0\0");
  598. $this->dechanged = false;
  599. }
  600. if (strlen($this->debuffer)) {
  601. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer));
  602. $this->debuffer.= substr($ciphertext, 0, strlen($plaintext));
  603. if (strlen($this->debuffer) == 8) {
  604. $this->decryptIV = $this->debuffer;
  605. $this->debuffer = '';
  606. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  607. }
  608. $ciphertext = substr($ciphertext, strlen($plaintext));
  609. } else {
  610. $plaintext = '';
  611. }
  612. $last_pos = strlen($ciphertext) & 0xFFFFFFF8;
  613. $plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : '';
  614. if (strlen($ciphertext) & 0x7) {
  615. if (strlen($plaintext)) {
  616. $this->decryptIV = substr($ciphertext, $last_pos - 8, 8);
  617. }
  618. $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
  619. $this->debuffer = substr($ciphertext, $last_pos);
  620. $plaintext.= $this->debuffer ^ $this->decryptIV;
  621. }
  622. return $plaintext;
  623. }
  624. if (!$this->continuousBuffer) {
  625. mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);
  626. }
  627. return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;
  628. }
  629. if (!is_array($this->keys)) {
  630. $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");
  631. }
  632. $buffer = &$this->debuffer;
  633. $continuousBuffer = $this->continuousBuffer;
  634. $plaintext = '';
  635. switch ($this->mode) {
  636. case CRYPT_DES_MODE_ECB:
  637. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  638. $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);
  639. }
  640. break;
  641. case CRYPT_DES_MODE_CBC:
  642. $xor = $this->decryptIV;
  643. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  644. $block = substr($ciphertext, $i, 8);
  645. $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;
  646. $xor = $block;
  647. }
  648. if ($this->continuousBuffer) {
  649. $this->decryptIV = $xor;
  650. }
  651. break;
  652. case CRYPT_DES_MODE_CTR:
  653. $xor = $this->decryptIV;
  654. if (strlen($buffer)) {
  655. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  656. $block = substr($ciphertext, $i, 8);
  657. $buffer.= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  658. $key = $this->_string_shift($buffer, 8);
  659. $plaintext.= $block ^ $key;
  660. }
  661. } else {
  662. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  663. $block = substr($ciphertext, $i, 8);
  664. $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);
  665. $plaintext.= $block ^ $key;
  666. }
  667. }
  668. if ($this->continuousBuffer) {
  669. $this->decryptIV = $xor;
  670. if ($start = strlen($ciphertext) % 8) {
  671. $buffer = substr($key, $start) . $buffer;
  672. }
  673. }
  674. break;
  675. case CRYPT_DES_MODE_CFB:
  676. if (!empty($buffer['ciphertext'])) {
  677. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer['ciphertext']));
  678. $buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext));
  679. if (strlen($buffer['ciphertext']) == 8) {
  680. $xor = $this->_processBlock($buffer['ciphertext'], CRYPT_DES_ENCRYPT);
  681. $buffer['ciphertext'] = '';
  682. }
  683. $start = strlen($plaintext);
  684. $block = $this->decryptIV;
  685. } else {
  686. $plaintext = '';
  687. $xor = $this->_processBlock($this->decryptIV, CRYPT_DES_ENCRYPT);
  688. $start = 0;
  689. }
  690. for ($i = $start; $i < strlen($ciphertext); $i+=8) {
  691. $block = substr($ciphertext, $i, 8);
  692. $plaintext.= $block ^ $xor;
  693. if ($continuousBuffer && strlen($block) != 8) {
  694. $buffer['ciphertext'].= $block;
  695. $block = $xor;
  696. } else if (strlen($block) == 8) {
  697. $xor = $this->_processBlock($block, CRYPT_DES_ENCRYPT);
  698. }
  699. }
  700. if ($this->continuousBuffer) {
  701. $this->decryptIV = $block;
  702. }
  703. break;
  704. case CRYPT_DES_MODE_OFB:
  705. $xor = $this->decryptIV;
  706. if (strlen($buffer)) {
  707. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  708. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  709. $buffer.= $xor;
  710. $key = $this->_string_shift($buffer, 8);
  711. $plaintext.= substr($ciphertext, $i, 8) ^ $key;
  712. }
  713. } else {
  714. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  715. $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT);
  716. $plaintext.= substr($ciphertext, $i, 8) ^ $xor;
  717. }
  718. $key = $xor;
  719. }
  720. if ($this->continuousBuffer) {
  721. $this->decryptIV = $xor;
  722. if ($start = strlen($ciphertext) % 8) {
  723. $buffer = substr($key, $start) . $buffer;
  724. }
  725. }
  726. }
  727. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  728. }
  729. /**
  730. * Treat consecutive "packets" as if they are a continuous buffer.
  731. *
  732. * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  733. * will yield different outputs:
  734. *
  735. * <code>
  736. * echo $des->encrypt(substr($plaintext, 0, 8));
  737. * echo $des->encrypt(substr($plaintext, 8, 8));
  738. * </code>
  739. * <code>
  740. * echo $des->encrypt($plaintext);
  741. * </code>
  742. *
  743. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  744. * another, as demonstrated with the following:
  745. *
  746. * <code>
  747. * $des->encrypt(substr($plaintext, 0, 8));
  748. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  749. * </code>
  750. * <code>
  751. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  752. * </code>
  753. *
  754. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  755. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  756. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  757. *
  758. * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  759. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  760. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  761. * however, they are also less intuitive and more likely to cause you problems.
  762. *
  763. * @see Crypt_DES::disableContinuousBuffer()
  764. * @access public
  765. */
  766. function enableContinuousBuffer()
  767. {
  768. $this->continuousBuffer = true;
  769. }
  770. /**
  771. * Treat consecutive packets as if they are a discontinuous buffer.
  772. *
  773. * The default behavior.
  774. *
  775. * @see Crypt_DES::enableContinuousBuffer()
  776. * @access public
  777. */
  778. function disableContinuousBuffer()
  779. {
  780. $this->continuousBuffer = false;
  781. $this->encryptIV = $this->iv;
  782. $this->decryptIV = $this->iv;
  783. }
  784. /**
  785. * Pad "packets".
  786. *
  787. * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
  788. * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
  789. *
  790. * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
  791. * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
  792. * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
  793. * transmitted separately)
  794. *
  795. * @see Crypt_DES::disablePadding()
  796. * @access public
  797. */
  798. function enablePadding()
  799. {
  800. $this->padding = true;
  801. }
  802. /**
  803. * Do not pad packets.
  804. *
  805. * @see Crypt_DES::enablePadding()
  806. * @access public
  807. */
  808. function disablePadding()
  809. {
  810. $this->padding = false;
  811. }
  812. /**
  813. * Pads a string
  814. *
  815. * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
  816. * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
  817. *
  818. * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
  819. * and padding will, hence forth, be enabled.
  820. *
  821. * @see Crypt_DES::_unpad()
  822. * @access private
  823. */
  824. function _pad($text)
  825. {
  826. $length = strlen($text);
  827. if (!$this->padding) {
  828. if (($length & 7) == 0) {
  829. return $text;
  830. } else {
  831. user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
  832. $this->padding = true;
  833. }
  834. }
  835. $pad = 8 - ($length & 7);
  836. return str_pad($text, $length + $pad, chr($pad));
  837. }
  838. /**
  839. * Unpads a string
  840. *
  841. * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
  842. * and false will be returned.
  843. *
  844. * @see Crypt_DES::_pad()
  845. * @access private
  846. */
  847. function _unpad($text)
  848. {
  849. if (!$this->padding) {
  850. return $text;
  851. }
  852. $length = ord($text[strlen($text) - 1]);
  853. if (!$length || $length > 8) {
  854. return false;
  855. }
  856. return substr($text, 0, -$length);
  857. }
  858. /**
  859. * Encrypts or decrypts a 64-bit block
  860. *
  861. * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
  862. * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
  863. * idea of what this function does.
  864. *
  865. * @access private
  866. * @param String $block
  867. * @param Integer $mode
  868. * @return String
  869. */
  870. function _processBlock($block, $mode)
  871. {
  872. // s-boxes. in the official DES docs, they're described as being matrices that
  873. // one accesses by using the first and last bits to determine the row and the
  874. // middle four bits to determine the column. in this implementation, they've
  875. // been converted to vectors
  876. static $sbox = array(
  877. array(
  878. 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
  879. 3, 10 ,10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
  880. 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
  881. 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
  882. ),
  883. array(
  884. 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
  885. 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
  886. 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
  887. 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
  888. ),
  889. array(
  890. 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
  891. 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
  892. 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
  893. 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
  894. ),
  895. array(
  896. 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
  897. 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
  898. 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
  899. 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
  900. ),
  901. array(
  902. 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
  903. 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
  904. 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
  905. 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
  906. ),
  907. array(
  908. 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
  909. 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
  910. 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
  911. 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
  912. ),
  913. array(
  914. 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
  915. 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
  916. 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
  917. 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
  918. ),
  919. array(
  920. 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
  921. 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
  922. 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
  923. 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
  924. )
  925. );
  926. $keys = $this->keys;
  927. $temp = unpack('Na/Nb', $block);
  928. $block = array($temp['a'], $temp['b']);
  929. // because php does arithmetic right shifts, if the most significant bits are set, right
  930. // shifting those into the correct position will add 1's - not 0's. this will intefere
  931. // with the | operation unless a second & is done. so we isolate these bits and left shift
  932. // them into place. we then & each block with 0x7FFFFFFF to prevennt 1's from being added
  933. // for any other shifts.
  934. $msb = array(
  935. ($block[0] >> 31) & 1,
  936. ($block[1] >> 31) & 1
  937. );
  938. $block[0] &= 0x7FFFFFFF;
  939. $block[1] &= 0x7FFFFFFF;
  940. // we isolate the appropriate bit in the appropriate integer and shift as appropriate. in
  941. // some cases, there are going to be multiple bits in the same integer that need to be shifted
  942. // in the same way. we combine those into one shift operation.
  943. $block = array(
  944. (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |
  945. (($block[1] & 0x00400001) << 7) | (($block[1] & 0x40000100) >> 2) |
  946. (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |
  947. (($block[0] & 0x00400001) << 3) | (($block[0] & 0x40000100) >> 6) |
  948. (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |
  949. (($block[1] & 0x00100000) << 1) | (($block[1] & 0x10000000) >> 8) |
  950. (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) << 6) |
  951. (($block[0] & 0x00100000) >> 3) | (($block[0] & 0x10000000) >> 12) |
  952. (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) << 4) |
  953. (($block[1] & 0x00040000) >> 5) | (($block[1] & 0x04000000) >> 14) |
  954. (($block[0] & 0x00000004) << 9) | ( $block[0] & 0x00000400 ) |
  955. (($block[0] & 0x00040000) >> 9) | (($block[0] & 0x04000000) >> 18) |
  956. (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |
  957. (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)
  958. ,
  959. (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |
  960. (($block[1] & 0x00800002) << 6) | (($block[0] & 0x00000080) << 20) |
  961. (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) << 2) |
  962. (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) << 9) |
  963. ( $block[1] & 0x00200000 ) | (($block[1] & 0x20000000) >> 9) |
  964. (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) << 5) |
  965. (($block[0] & 0x00200000) >> 4) | (($block[0] & 0x20000000) >> 13) |
  966. (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) << 3) |
  967. (($block[1] & 0x00080000) >> 6) | (($block[1] & 0x08000000) >> 15) |
  968. (($block[0] & 0x00000008) << 8) | (($block[0] & 0x00000800) >> 1) |
  969. (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |
  970. (($block[1] & 0x00000200) >> 3) | (($block[0] & 0x00000200) >> 7) |
  971. (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |
  972. (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |
  973. ($msb[1] << 28) | ($msb[0] << 24)
  974. );
  975. for ($i = 0; $i < 16; $i++) {
  976. // start of "the Feistel (F) function" - see the following URL:
  977. // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
  978. $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)
  979. | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)
  980. | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)
  981. | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)
  982. | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)
  983. | (($sbox[5][(($block[1] & 0x00001F80) >> 7) ^ $keys[$mode][$i][5]]) << 8)
  984. | (($sbox[6][(($block[1] & 0x000001F8) >> 3) ^ $keys[$mode][$i][6]]) << 4)
  985. | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);
  986. $msb = ($temp >> 31) & 1;
  987. $temp &= 0x7FFFFFFF;
  988. $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) << 5)
  989. | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)
  990. | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) << 6)
  991. | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) << 9)
  992. | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)
  993. | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >> 8)
  994. | (($temp & 0x00004000) << 4) | (($temp & 0x00000002) << 16)
  995. | (($temp & 0x00442000) >> 6) | (($temp & 0x40800000) >> 15)
  996. | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)
  997. | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) << 3)
  998. | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >> 7)
  999. | (($temp & 0x00200000) >> 19) | ($msb << 23);
  1000. // end of "the Feistel (F) function" - $newBlock is F's output
  1001. $temp = $block[1];
  1002. $block[1] = $block[0] ^ $newBlock;
  1003. $block[0] = $temp;
  1004. }
  1005. $msb = array(
  1006. ($block[0] >> 31) & 1,
  1007. ($block[1] >> 31) & 1
  1008. );
  1009. $block[0] &= 0x7FFFFFFF;
  1010. $block[1] &= 0x7FFFFFFF;
  1011. $block = array(
  1012. (($block[0] & 0x01000004) << 7) | (($block[1] & 0x01000004) << 6) |
  1013. (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |
  1014. (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |
  1015. (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |
  1016. (($block[0] & 0x02000008) >> 2) | (($block[1] & 0x02000008) >> 3) |
  1017. (($block[0] & 0x00020000) << 4) | (($block[1] & 0x00020000) << 3) |
  1018. (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) << 9) |
  1019. (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |
  1020. (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |
  1021. (($block[0] & 0x00040000) >> 5) | (($block[1] & 0x00040000) >> 6) |
  1022. (($block[0] & 0x00000400) << 1) | ( $block[1] & 0x00000400 ) |
  1023. (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |
  1024. (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |
  1025. (($block[0] & 0x00000800) >> 8) | (($block[1] & 0x00000800) >> 9)
  1026. ,
  1027. (($block[0] & 0x10000040) << 3) | (($block[1] & 0x10000040) << 2) |
  1028. (($block[0] & 0x00100000) << 9) | (($block[1] & 0x00100000) << 8) |
  1029. (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |
  1030. (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |
  1031. (($block[0] & 0x20000080) >> 6) | (($block[1] & 0x20000080) >> 7) |
  1032. ( $block[0] & 0x00200000 ) | (($block[1] & 0x00200000) >> 1) |
  1033. (($block[0] & 0x00002000) << 6) | (($block[1] & 0x00002000) << 5) |
  1034. (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |
  1035. (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |
  1036. (($block[0] & 0x00400000) >> 9) | (($block[1] & 0x00400000) >> 10) |
  1037. (($block[0] & 0x00004000) >> 3) | (($block[1] & 0x00004000) >> 4) |
  1038. (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |
  1039. (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |
  1040. ($msb[0] << 7) | ($msb[1] << 6)
  1041. );
  1042. return pack('NN', $block[0], $block[1]);
  1043. }
  1044. /**
  1045. * Creates the key schedule.
  1046. *
  1047. * @access private
  1048. * @param String $key
  1049. * @return Array
  1050. */
  1051. function _prepareKey($key)
  1052. {
  1053. static $shifts = array( // number of key bits shifted per round
  1054. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  1055. );
  1056. // pad the key and remove extra characters as appropriate.
  1057. $key = str_pad(substr($key, 0, 8), 8, chr(0));
  1058. $temp = unpack('Na/Nb', $key);
  1059. $key = array($temp['a'], $temp['b']);
  1060. $msb = array(
  1061. ($key[0] >> 31) & 1,
  1062. ($key[1] >> 31) & 1
  1063. );
  1064. $key[0] &= 0x7FFFFFFF;
  1065. $key[1] &= 0x7FFFFFFF;
  1066. $key = array(
  1067. (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |
  1068. (($key[1] & 0x00020408) << 8) | (($key[1] & 0x02040800) >> 1) |
  1069. (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |
  1070. (($key[0] & 0x00020408) << 4) | (($key[0] & 0x02040800) >> 5) |
  1071. (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |
  1072. (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |
  1073. (($key[0] & 0x00000010) >> 1) | (($key[0] & 0x00001000) >> 10) |
  1074. (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)
  1075. ,
  1076. (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |
  1077. (($key[1] & 0x00800000) << 2) | (($key[0] & 0x00000080) << 16) |
  1078. (($key[0] & 0x00008000) << 7) | (($key[0] & 0x00800000) >> 2) |
  1079. (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) << 4) |
  1080. (($key[1] & 0x00400000) >> 5) | (($key[1] & 0x40000000) >> 14) |
  1081. (($key[0] & 0x00000040) << 9) | ( $key[0] & 0x00004000 ) |
  1082. (($key[0] & 0x00400000) >> 9) | (($key[0] & 0x40000000) >> 18) |
  1083. (($key[1] & 0x00000020) << 6) | (($key[1] & 0x00002000) >> 3) |
  1084. (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |
  1085. (($key[0] & 0x00000020) << 2) | (($key[0] & 0x00002000) >> 7) |
  1086. (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |
  1087. (($key[1] & 0x00000010) >> 1) | (($key[1] & 0x00001000) >> 10) |
  1088. (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |
  1089. ($msb[1] << 24) | ($msb[0] << 20)
  1090. );
  1091. $keys = array();
  1092. for ($i = 0; $i < 16; $i++) {
  1093. $key[0] <<= $shifts[$i];
  1094. $temp = ($key[0] & 0xF0000000) >> 28;
  1095. $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;
  1096. $key[1] <<= $shifts[$i];
  1097. $temp = ($key[1] & 0xF0000000) >> 28;
  1098. $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;
  1099. $temp = array(
  1100. (($key[1] & 0x00004000) >> 9) | (($key[1] & 0x00000800) >> 7) |
  1101. (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >> 2) |
  1102. (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)
  1103. ,
  1104. (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) << 4) |
  1105. (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |
  1106. (($key[1] & 0x00000080) >> 6)
  1107. ,
  1108. ( $key[1] & 0x00000020 ) | (($key[1] & 0x00000200) >> 5) |
  1109. (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |
  1110. (($key[1] & 0x00000004) >> 1) | (($key[1] & 0x00100000) >> 20)
  1111. ,
  1112. (($key[1] & 0x00001000) >> 7) | (($key[1] & 0x00200000) >> 17) |
  1113. (($key[1] & 0x00000002) << 2) | (($key[1] & 0x00000100) >> 6) |
  1114. (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)
  1115. ,
  1116. (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010 ) |
  1117. (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |
  1118. (($key[0] & 0x00000200) >> 8) | (($key[0] & 0x00000002) >> 1)
  1119. ,
  1120. (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |
  1121. (($key[0] & 0x00000020) >> 2) | (($key[0] & 0x00000800) >> 9) |
  1122. (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >> 8)
  1123. ,
  1124. (($key[0] & 0x00001000) >> 7) | (($key[0] & 0x00000088) >> 3) |
  1125. (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) << 2) |
  1126. (($key[0] & 0x00400000) >> 21)
  1127. ,
  1128. (($key[0] & 0x00000400) >> 5) | (($key[0] & 0x00004000) >> 10) |
  1129. (($key[0] & 0x00000040) >> 3) | (($key[0] & 0x00100000) >> 18) |
  1130. (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)
  1131. );
  1132. $keys[] = $temp;
  1133. }
  1134. $temp = array(
  1135. CRYPT_DES_ENCRYPT => $keys,
  1136. CRYPT_DES_DECRYPT => array_reverse($keys)
  1137. );
  1138. return $temp;
  1139. }
  1140. /**
  1141. * String Shift
  1142. *
  1143. * Inspired by array_shift
  1144. *
  1145. * @param String $string
  1146. * @param optional Integer $index
  1147. * @return String
  1148. * @access private
  1149. */
  1150. function _string_shift(&$string, $index = 1)
  1151. {
  1152. $substr = substr($string, 0, $index);
  1153. $string = substr($string, $index);
  1154. return $substr;
  1155. }
  1156. }
  1157. // vim: ts=4:sw=4:et:
  1158. // vim6: fdl=1: