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

/vendor_user/windid_client/src/library/utility/phpseclib/Crypt/TripleDES.php

https://github.com/gyyinzjut/EduSoho
PHP | 690 lines | 331 code | 59 blank | 300 comment | 60 complexity | cae334228bae6ba26a60cc68292a239f MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of Triple DES.
  5. *
  6. * Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * Here's a short example of how to use this library:
  11. * <code>
  12. * <?php
  13. * include('Crypt/TripleDES.php');
  14. *
  15. * $des = new Crypt_TripleDES();
  16. *
  17. * $des->setKey('abcdefghijklmnopqrstuvwx');
  18. *
  19. * $size = 10 * 1024;
  20. * $plaintext = '';
  21. * for ($i = 0; $i < $size; $i++) {
  22. * $plaintext.= 'a';
  23. * }
  24. *
  25. * echo $des->decrypt($des->encrypt($plaintext));
  26. * ?>
  27. * </code>
  28. *
  29. * LICENSE: This library is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU Lesser General Public
  31. * License as published by the Free Software Foundation; either
  32. * version 2.1 of the License, or (at your option) any later version.
  33. *
  34. * This library is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * Lesser General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Lesser General Public
  40. * License along with this library; if not, write to the Free Software
  41. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  42. * MA 02111-1307 USA
  43. *
  44. * @category Crypt
  45. * @package Crypt_TripleDES
  46. * @author Jim Wigginton <terrafrost@php.net>
  47. * @copyright MMVII Jim Wigginton
  48. * @license http://www.gnu.org/licenses/lgpl.txt
  49. * @version $Id: TripleDES.php 21939 2012-12-17 07:13:16Z long.shi $
  50. * @link http://phpseclib.sourceforge.net
  51. */
  52. /**
  53. * Include Crypt_DES
  54. */
  55. require_once 'DES.php';
  56. /**
  57. * Encrypt / decrypt using inner chaining
  58. *
  59. * Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3).
  60. */
  61. define('CRYPT_DES_MODE_3CBC', 3);
  62. /**
  63. * Encrypt / decrypt using outer chaining
  64. *
  65. * Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC.
  66. */
  67. define('CRYPT_DES_MODE_CBC3', CRYPT_DES_MODE_CBC);
  68. /**
  69. * Pure-PHP implementation of Triple DES.
  70. *
  71. * @author Jim Wigginton <terrafrost@php.net>
  72. * @version 0.1.0
  73. * @access public
  74. * @package Crypt_TerraDES
  75. */
  76. class Crypt_TripleDES {
  77. /**
  78. * The Three Keys
  79. *
  80. * @see Crypt_TripleDES::setKey()
  81. * @var String
  82. * @access private
  83. */
  84. var $key = "\0\0\0\0\0\0\0\0";
  85. /**
  86. * The Encryption Mode
  87. *
  88. * @see Crypt_TripleDES::Crypt_TripleDES()
  89. * @var Integer
  90. * @access private
  91. */
  92. var $mode = CRYPT_DES_MODE_CBC;
  93. /**
  94. * Continuous Buffer status
  95. *
  96. * @see Crypt_TripleDES::enableContinuousBuffer()
  97. * @var Boolean
  98. * @access private
  99. */
  100. var $continuousBuffer = false;
  101. /**
  102. * Padding status
  103. *
  104. * @see Crypt_TripleDES::enablePadding()
  105. * @var Boolean
  106. * @access private
  107. */
  108. var $padding = true;
  109. /**
  110. * The Initialization Vector
  111. *
  112. * @see Crypt_TripleDES::setIV()
  113. * @var String
  114. * @access private
  115. */
  116. var $iv = "\0\0\0\0\0\0\0\0";
  117. /**
  118. * A "sliding" Initialization Vector
  119. *
  120. * @see Crypt_TripleDES::enableContinuousBuffer()
  121. * @var String
  122. * @access private
  123. */
  124. var $encryptIV = "\0\0\0\0\0\0\0\0";
  125. /**
  126. * A "sliding" Initialization Vector
  127. *
  128. * @see Crypt_TripleDES::enableContinuousBuffer()
  129. * @var String
  130. * @access private
  131. */
  132. var $decryptIV = "\0\0\0\0\0\0\0\0";
  133. /**
  134. * The Crypt_DES objects
  135. *
  136. * @var Array
  137. * @access private
  138. */
  139. var $des;
  140. /**
  141. * mcrypt resource for encryption
  142. *
  143. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  144. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  145. *
  146. * @see Crypt_AES::encrypt()
  147. * @var String
  148. * @access private
  149. */
  150. var $enmcrypt;
  151. /**
  152. * mcrypt resource for decryption
  153. *
  154. * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
  155. * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
  156. *
  157. * @see Crypt_AES::decrypt()
  158. * @var String
  159. * @access private
  160. */
  161. var $demcrypt;
  162. /**
  163. * Does the (en|de)mcrypt resource need to be (re)initialized?
  164. *
  165. * @see setKey()
  166. * @see setIV()
  167. * @var Boolean
  168. * @access private
  169. */
  170. var $changed = true;
  171. /**
  172. * Default Constructor.
  173. *
  174. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  175. * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
  176. *
  177. * @param optional Integer $mode
  178. * @return Crypt_TripleDES
  179. * @access public
  180. */
  181. function Crypt_TripleDES($mode = CRYPT_DES_MODE_CBC)
  182. {
  183. if ( !defined('CRYPT_DES_MODE') ) {
  184. switch (true) {
  185. case extension_loaded('mcrypt'):
  186. // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),
  187. // but since that can be changed after the object has been created, there doesn't seem to be
  188. // a lot of point...
  189. define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
  190. break;
  191. default:
  192. define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
  193. }
  194. }
  195. if ( $mode == CRYPT_DES_MODE_3CBC ) {
  196. $this->mode = CRYPT_DES_MODE_3CBC;
  197. $this->des = array(
  198. new Crypt_DES(CRYPT_DES_MODE_CBC),
  199. new Crypt_DES(CRYPT_DES_MODE_CBC),
  200. new Crypt_DES(CRYPT_DES_MODE_CBC)
  201. );
  202. // we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
  203. $this->des[0]->disablePadding();
  204. $this->des[1]->disablePadding();
  205. $this->des[2]->disablePadding();
  206. return;
  207. }
  208. switch ( CRYPT_DES_MODE ) {
  209. case CRYPT_DES_MODE_MCRYPT:
  210. switch ($mode) {
  211. case CRYPT_DES_MODE_ECB:
  212. $this->mode = MCRYPT_MODE_ECB;
  213. break;
  214. case CRYPT_DES_MODE_CTR:
  215. $this->mode = 'ctr';
  216. break;
  217. case CRYPT_DES_MODE_CBC:
  218. default:
  219. $this->mode = MCRYPT_MODE_CBC;
  220. }
  221. break;
  222. default:
  223. $this->des = array(
  224. new Crypt_DES(CRYPT_DES_MODE_ECB),
  225. new Crypt_DES(CRYPT_DES_MODE_ECB),
  226. new Crypt_DES(CRYPT_DES_MODE_ECB)
  227. );
  228. // we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
  229. $this->des[0]->disablePadding();
  230. $this->des[1]->disablePadding();
  231. $this->des[2]->disablePadding();
  232. switch ($mode) {
  233. case CRYPT_DES_MODE_ECB:
  234. case CRYPT_DES_MODE_CTR:
  235. case CRYPT_DES_MODE_CBC:
  236. $this->mode = $mode;
  237. break;
  238. default:
  239. $this->mode = CRYPT_DES_MODE_CBC;
  240. }
  241. }
  242. }
  243. /**
  244. * Sets the key.
  245. *
  246. * Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
  247. * 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
  248. *
  249. * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
  250. *
  251. * If the key is not explicitly set, it'll be assumed to be all zero's.
  252. *
  253. * @access public
  254. * @param String $key
  255. */
  256. function setKey($key)
  257. {
  258. $length = strlen($key);
  259. if ($length > 8) {
  260. $key = str_pad($key, 24, chr(0));
  261. // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
  262. // http://php.net/function.mcrypt-encrypt#47973
  263. //$key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
  264. }
  265. $this->key = $key;
  266. switch (true) {
  267. case CRYPT_DES_MODE == CRYPT_DES_MODE_INTERNAL:
  268. case $this->mode == CRYPT_DES_MODE_3CBC:
  269. $this->des[0]->setKey(substr($key, 0, 8));
  270. $this->des[1]->setKey(substr($key, 8, 8));
  271. $this->des[2]->setKey(substr($key, 16, 8));
  272. }
  273. $this->changed = true;
  274. }
  275. /**
  276. * Sets the initialization vector. (optional)
  277. *
  278. * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
  279. * to be all zero's.
  280. *
  281. * @access public
  282. * @param String $iv
  283. */
  284. function setIV($iv)
  285. {
  286. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
  287. if ($this->mode == CRYPT_DES_MODE_3CBC) {
  288. $this->des[0]->setIV($iv);
  289. $this->des[1]->setIV($iv);
  290. $this->des[2]->setIV($iv);
  291. }
  292. $this->changed = true;
  293. }
  294. /**
  295. * Generate CTR XOR encryption key
  296. *
  297. * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
  298. * plaintext / ciphertext in CTR mode.
  299. *
  300. * @see Crypt_DES::decrypt()
  301. * @see Crypt_DES::encrypt()
  302. * @access public
  303. * @param Integer $length
  304. * @param String $iv
  305. */
  306. function _generate_xor($length, &$iv)
  307. {
  308. $xor = '';
  309. $num_blocks = ($length + 7) >> 3;
  310. for ($i = 0; $i < $num_blocks; $i++) {
  311. $xor.= $iv;
  312. for ($j = 4; $j <= 8; $j+=4) {
  313. $temp = substr($iv, -$j, 4);
  314. switch ($temp) {
  315. case "\xFF\xFF\xFF\xFF":
  316. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  317. break;
  318. case "\x7F\xFF\xFF\xFF":
  319. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  320. break 2;
  321. default:
  322. extract(unpack('Ncount', $temp));
  323. $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  324. break 2;
  325. }
  326. }
  327. }
  328. return $xor;
  329. }
  330. /**
  331. * Encrypts a message.
  332. *
  333. * @access public
  334. * @param String $plaintext
  335. */
  336. function encrypt($plaintext)
  337. {
  338. if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {
  339. $plaintext = $this->_pad($plaintext);
  340. }
  341. // if the key is smaller then 8, do what we'd normally do
  342. if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) {
  343. $ciphertext = $this->des[2]->encrypt($this->des[1]->decrypt($this->des[0]->encrypt($plaintext)));
  344. return $ciphertext;
  345. }
  346. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  347. if ($this->changed) {
  348. if (!isset($this->enmcrypt)) {
  349. $this->enmcrypt = mcrypt_module_open(MCRYPT_3DES, '', $this->mode, '');
  350. }
  351. mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
  352. $this->changed = false;
  353. }
  354. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  355. if (!$this->continuousBuffer) {
  356. mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
  357. }
  358. return $ciphertext;
  359. }
  360. if (strlen($this->key) <= 8) {
  361. $this->des[0]->mode = $this->mode;
  362. return $this->des[0]->encrypt($plaintext);
  363. }
  364. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  365. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  366. $plaintext = str_pad($plaintext, ceil(strlen($plaintext) / 8) * 8, chr(0));
  367. $des = $this->des;
  368. $ciphertext = '';
  369. switch ($this->mode) {
  370. case CRYPT_DES_MODE_ECB:
  371. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  372. $block = substr($plaintext, $i, 8);
  373. $block = $des[0]->_processBlock($block, CRYPT_DES_ENCRYPT);
  374. $block = $des[1]->_processBlock($block, CRYPT_DES_DECRYPT);
  375. $block = $des[2]->_processBlock($block, CRYPT_DES_ENCRYPT);
  376. $ciphertext.= $block;
  377. }
  378. break;
  379. case CRYPT_DES_MODE_CBC:
  380. $xor = $this->encryptIV;
  381. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  382. $block = substr($plaintext, $i, 8) ^ $xor;
  383. $block = $des[0]->_processBlock($block, CRYPT_DES_ENCRYPT);
  384. $block = $des[1]->_processBlock($block, CRYPT_DES_DECRYPT);
  385. $block = $des[2]->_processBlock($block, CRYPT_DES_ENCRYPT);
  386. $xor = $block;
  387. $ciphertext.= $block;
  388. }
  389. if ($this->continuousBuffer) {
  390. $this->encryptIV = $xor;
  391. }
  392. break;
  393. case CRYPT_DES_MODE_CTR:
  394. $xor = $this->encryptIV;
  395. for ($i = 0; $i < strlen($plaintext); $i+=8) {
  396. $key = $this->_generate_xor(8, $xor);
  397. $key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
  398. $key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
  399. $key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
  400. $block = substr($plaintext, $i, 8);
  401. $ciphertext.= $block ^ $key;
  402. }
  403. if ($this->continuousBuffer) {
  404. $this->encryptIV = $xor;
  405. }
  406. }
  407. return $ciphertext;
  408. }
  409. /**
  410. * Decrypts a message.
  411. *
  412. * @access public
  413. * @param String $ciphertext
  414. */
  415. function decrypt($ciphertext)
  416. {
  417. if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) {
  418. $plaintext = $this->des[0]->decrypt($this->des[1]->encrypt($this->des[2]->decrypt($ciphertext)));
  419. return $this->_unpad($plaintext);
  420. }
  421. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  422. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  423. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
  424. if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
  425. if ($this->changed) {
  426. if (!isset($this->demcrypt)) {
  427. $this->demcrypt = mcrypt_module_open(MCRYPT_3DES, '', $this->mode, '');
  428. }
  429. mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
  430. $this->changed = false;
  431. }
  432. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  433. if (!$this->continuousBuffer) {
  434. mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
  435. }
  436. return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;
  437. }
  438. if (strlen($this->key) <= 8) {
  439. $this->des[0]->mode = $this->mode;
  440. return $this->_unpad($this->des[0]->decrypt($plaintext));
  441. }
  442. $des = $this->des;
  443. $plaintext = '';
  444. switch ($this->mode) {
  445. case CRYPT_DES_MODE_ECB:
  446. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  447. $block = substr($ciphertext, $i, 8);
  448. $block = $des[2]->_processBlock($block, CRYPT_DES_DECRYPT);
  449. $block = $des[1]->_processBlock($block, CRYPT_DES_ENCRYPT);
  450. $block = $des[0]->_processBlock($block, CRYPT_DES_DECRYPT);
  451. $plaintext.= $block;
  452. }
  453. break;
  454. case CRYPT_DES_MODE_CBC:
  455. $xor = $this->decryptIV;
  456. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  457. $orig = $block = substr($ciphertext, $i, 8);
  458. $block = $des[2]->_processBlock($block, CRYPT_DES_DECRYPT);
  459. $block = $des[1]->_processBlock($block, CRYPT_DES_ENCRYPT);
  460. $block = $des[0]->_processBlock($block, CRYPT_DES_DECRYPT);
  461. $plaintext.= $block ^ $xor;
  462. $xor = $orig;
  463. }
  464. if ($this->continuousBuffer) {
  465. $this->decryptIV = $xor;
  466. }
  467. break;
  468. case CRYPT_DES_MODE_CTR:
  469. $xor = $this->decryptIV;
  470. for ($i = 0; $i < strlen($ciphertext); $i+=8) {
  471. $key = $this->_generate_xor(8, $xor);
  472. $key = $des[0]->_processBlock($key, CRYPT_DES_ENCRYPT);
  473. $key = $des[1]->_processBlock($key, CRYPT_DES_DECRYPT);
  474. $key = $des[2]->_processBlock($key, CRYPT_DES_ENCRYPT);
  475. $block = substr($ciphertext, $i, 8);
  476. $plaintext.= $block ^ $key;
  477. }
  478. if ($this->continuousBuffer) {
  479. $this->decryptIV = $xor;
  480. }
  481. }
  482. return $this->mode != CRYPT_DES_MODE_CTR ? $this->_unpad($plaintext) : $plaintext;
  483. }
  484. /**
  485. * Treat consecutive "packets" as if they are a continuous buffer.
  486. *
  487. * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  488. * will yield different outputs:
  489. *
  490. * <code>
  491. * echo $des->encrypt(substr($plaintext, 0, 8));
  492. * echo $des->encrypt(substr($plaintext, 8, 8));
  493. * </code>
  494. * <code>
  495. * echo $des->encrypt($plaintext);
  496. * </code>
  497. *
  498. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  499. * another, as demonstrated with the following:
  500. *
  501. * <code>
  502. * $des->encrypt(substr($plaintext, 0, 8));
  503. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  504. * </code>
  505. * <code>
  506. * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
  507. * </code>
  508. *
  509. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  510. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  511. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  512. *
  513. * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
  514. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  515. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  516. * however, they are also less intuitive and more likely to cause you problems.
  517. *
  518. * @see Crypt_TripleDES::disableContinuousBuffer()
  519. * @access public
  520. */
  521. function enableContinuousBuffer()
  522. {
  523. $this->continuousBuffer = true;
  524. if ($this->mode == CRYPT_DES_MODE_3CBC) {
  525. $this->des[0]->enableContinuousBuffer();
  526. $this->des[1]->enableContinuousBuffer();
  527. $this->des[2]->enableContinuousBuffer();
  528. }
  529. }
  530. /**
  531. * Treat consecutive packets as if they are a discontinuous buffer.
  532. *
  533. * The default behavior.
  534. *
  535. * @see Crypt_TripleDES::enableContinuousBuffer()
  536. * @access public
  537. */
  538. function disableContinuousBuffer()
  539. {
  540. $this->continuousBuffer = false;
  541. $this->encryptIV = $this->iv;
  542. $this->decryptIV = $this->iv;
  543. if ($this->mode == CRYPT_DES_MODE_3CBC) {
  544. $this->des[0]->disableContinuousBuffer();
  545. $this->des[1]->disableContinuousBuffer();
  546. $this->des[2]->disableContinuousBuffer();
  547. }
  548. }
  549. /**
  550. * Pad "packets".
  551. *
  552. * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
  553. * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
  554. *
  555. * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
  556. * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
  557. * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
  558. * transmitted separately)
  559. *
  560. * @see Crypt_TripleDES::disablePadding()
  561. * @access public
  562. */
  563. function enablePadding()
  564. {
  565. $this->padding = true;
  566. }
  567. /**
  568. * Do not pad packets.
  569. *
  570. * @see Crypt_TripleDES::enablePadding()
  571. * @access public
  572. */
  573. function disablePadding()
  574. {
  575. $this->padding = false;
  576. }
  577. /**
  578. * Pads a string
  579. *
  580. * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
  581. * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
  582. *
  583. * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
  584. * and padding will, hence forth, be enabled.
  585. *
  586. * @see Crypt_TripleDES::_unpad()
  587. * @access private
  588. */
  589. function _pad($text)
  590. {
  591. $length = strlen($text);
  592. if (!$this->padding) {
  593. if (($length & 7) == 0) {
  594. return $text;
  595. } else {
  596. user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
  597. $this->padding = true;
  598. }
  599. }
  600. $pad = 8 - ($length & 7);
  601. return str_pad($text, $length + $pad, chr($pad));
  602. }
  603. /**
  604. * Unpads a string
  605. *
  606. * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
  607. * and false will be returned.
  608. *
  609. * @see Crypt_TripleDES::_pad()
  610. * @access private
  611. */
  612. function _unpad($text)
  613. {
  614. if (!$this->padding) {
  615. return $text;
  616. }
  617. $length = ord($text[strlen($text) - 1]);
  618. if (!$length || $length > 8) {
  619. return false;
  620. }
  621. return substr($text, 0, -$length);
  622. }
  623. }
  624. // vim: ts=4:sw=4:et:
  625. // vim6: fdl=1: