[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/Cipher/ -> des.php (source)

   1  <?php
   2  /**
   3   * The Cipher_des:: class implements the Cipher interface encryption data
   4   * using the Data Encryption Standard (DES) algorithm as define in FIPS46-3.
   5   *
   6   * $Horde: framework/Cipher/Cipher/des.php,v 1.4.12.7 2006/01/01 21:28:10 jan Exp $
   7   *
   8   * Copyright 2003-2006 Mike Cochrane <mike@graftonhall.co.nz>
   9   *
  10   * See the enclosed file COPYING for license information (LGPL). If you
  11   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12   *
  13   * @author  Mike Cochrane <mike@graftonhall.co.nz>
  14   * @since   Horde 3.0
  15   * @package Horde_Cipher
  16   */
  17  class Horde_Cipher_des extends Horde_Cipher {
  18  
  19      /**
  20       * Initial Permutation
  21       *
  22       * @var array
  23       */
  24      var $ip = array(
  25          58, 50, 42, 34, 26, 18, 10,  2,
  26          60, 52, 44, 36, 28, 20, 12,  4,
  27          62, 54, 46, 38, 30, 22, 14,  6,
  28          64, 56, 48, 40, 32, 24, 16,  8,
  29          57, 49, 41, 33, 25, 17,  9,  1,
  30          59, 51, 43, 35, 27, 19, 11,  3,
  31          61, 53, 45, 37, 29, 21, 13,  5,
  32          63, 55, 47, 39, 31, 23, 15,  7
  33      );
  34  
  35      /**
  36       * Final Permutation IP^-1
  37       *
  38       * @var array
  39       */
  40      var $fp = array(
  41          40,  8, 48, 16, 56, 24, 64, 32,
  42          39,  7, 47, 15, 55, 23, 63, 31,
  43          38,  6, 46, 14, 54, 22, 62, 30,
  44          37,  5, 45, 13, 53, 21, 61, 29,
  45          36,  4, 44, 12, 52, 20, 60, 28,
  46          35,  3, 43, 11, 51, 19, 59, 27,
  47          34,  2, 42, 10, 50, 18, 58, 26,
  48          33,  1, 41,  9, 49, 17, 57, 25
  49      );
  50  
  51      /**
  52       * E Bit Selection Table
  53       *
  54       * @var array
  55       */
  56      var $e = array(
  57          32,  1,  2,  3,  4,  5,
  58           4,  5,  6,  7,  8,  9,
  59           8,  9, 10, 11, 12, 13,
  60          12, 13, 14, 15, 16, 17,
  61          16, 17, 18, 19, 20, 21,
  62          20, 21, 22, 23, 24, 25,
  63          24, 25, 26, 27, 28, 29,
  64          28, 29, 30, 31, 32,  1
  65      );
  66  
  67      /**
  68       * S boxes
  69       *
  70       * @var array
  71       */
  72      var $_s = array(
  73          /* S1 */
  74          1 => array(
  75             14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  76              0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  77              4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  78             15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13
  79          ),
  80  
  81          /* S2 */
  82          2 => array(
  83             15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  84              3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  85              0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  86             13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  87          ),
  88  
  89          /* S3 */
  90          3 => array(
  91             10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  92             13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  93             13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  94              1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  95          ),
  96  
  97          /* S4 */
  98          4 => array(
  99              7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
 100             13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
 101             10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
 102              3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
 103          ),
 104  
 105          /* S5 */
 106          5 => array(
 107              2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
 108             14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
 109              4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
 110             11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
 111          ),
 112  
 113          /* S6 */
 114          6 => array(
 115             12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
 116             10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
 117              9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
 118              4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
 119          ),
 120  
 121          /* S7 */
 122          7 => array(
 123              4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
 124             13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
 125              1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
 126              6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
 127          ),
 128  
 129          /* S8 */
 130          8 => array(
 131              13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
 132               1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
 133               7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
 134               2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
 135          )
 136      );
 137  
 138      /**
 139       * Primitive function
 140       *
 141       * @var array
 142       */
 143      var $p = array(
 144          16,  7, 20, 21,
 145          29, 12, 28, 17,
 146           1, 15, 23, 26,
 147           5, 18, 31, 10,
 148           2,  8, 24, 14,
 149          32, 27,  3,  9,
 150          19, 13, 30,  6,
 151          22, 11,  4, 25
 152      );
 153  
 154      /**
 155       * Permuted Choice Table
 156       *
 157       * @var array
 158       */
 159      var $pc1 = array(
 160          57, 49, 41, 33, 25, 17,  9,
 161           1, 58, 50, 42, 34, 26, 18,
 162          10,  2, 59, 51, 43, 35, 27,
 163          19, 11,  3, 60, 52, 44, 36,
 164  
 165          63, 55, 47, 39, 31, 23, 15,
 166           7, 62, 54, 46, 38, 30, 22,
 167          14,  6, 61, 53, 45, 37, 29,
 168          21, 13,  5, 28, 20, 12,  4
 169      );
 170  
 171      /**
 172       * Number left rotations of pc1
 173       *
 174       * @var array
 175       */
 176      var $shifts = array(
 177          1, 1, 2, 2, 2, 2, 2, 2,
 178          1, 2, 2, 2, 2, 2, 2, 1
 179      );
 180  
 181      /**
 182       * Permuted Choice Table 2
 183       *
 184       * @var array
 185       */
 186      var $pc2 = array(
 187          14, 17, 11, 24,  1,  5,
 188           3, 28, 15,  6, 21, 10,
 189          23, 19, 12,  4, 26,  8,
 190          16,  7, 27, 20, 13,  2,
 191          41, 52, 31, 37, 47, 55,
 192          30, 40, 51, 45, 33, 48,
 193          44, 49, 39, 56, 34, 53,
 194          46, 42, 50, 36, 29, 32
 195      );
 196  
 197      /**
 198       * Key Schedule
 199       *
 200       * @var array
 201       */
 202      var $_Ks = array();
 203  
 204      /**
 205       * Set the key to be used for en/decryption.
 206       *
 207       * @param string $key  The key to use.
 208       */
 209      function setKey($key)
 210      {
 211          if (!is_null($key)) {
 212              $this->_Ks = $this->_keySchedule($key);
 213          }
 214      }
 215  
 216      /**
 217       * Return the size of the blocks that this cipher needs.
 218       *
 219       * @return integer  The number of characters per block.
 220       */
 221      function getBlockSize()
 222      {
 223          return 8;
 224      }
 225  
 226      /**
 227       * Encrypt a block of data.
 228       *
 229       * @param string $block  The data to encrypt.
 230       * @param string $key    The key to use.
 231       *
 232       * @return string  The encrypted output.
 233       */
 234      function encryptBlock($block, $key = null)
 235      {
 236          if (!is_null($key)) {
 237              $this->_Ks = $this->_keySchedule($key);
 238          }
 239  
 240          $block = $this->_initialPerm($block);
 241  
 242          $L = substr($block, 0, 4);
 243          $R = substr($block, 4, 4);
 244  
 245          for ($i = 1; $i <= 16; $i++ ) {
 246              $R_prev = $R;
 247              $L_prev = $L;
 248  
 249              $L = $R;
 250              $R = $L_prev ^ $this->_f($R_prev, $i);
 251          }
 252  
 253          $block = $R . $L;
 254          $block = $this->_finalPerm($block);
 255  
 256          return $block;
 257      }
 258  
 259      /**
 260       * Decrypt a block of data.
 261       *
 262       * @param string $block  The data to decrypt.
 263       * @param string $key    The key to use.
 264       *
 265       * @return string  The decrypted output.
 266       */
 267      function decryptBlock($block, $key = null)
 268      {
 269          $block = $this->_initialPerm($block);
 270  
 271          if (!is_null($key)) {
 272              $this->_Ks = $this->_keySchedule($key);
 273          }
 274  
 275          $L = substr($block, 0, 4);
 276          $R = substr($block, 4, 4);
 277  
 278          for ($i = 16; $i >= 1; $i-- ) {
 279              $R_prev = $R;
 280              $L_prev = $L;
 281  
 282              $L = $R_prev;
 283              $R = $L_prev ^ $this->_f($R_prev, $i);
 284          }
 285  
 286          $block = $R . $L;
 287          $block = $this->_finalPerm($block);
 288  
 289          return $block;
 290      }
 291  
 292      /**
 293       * Put an input string through an initial permutation
 294       *
 295       * @param string $input  Input string.
 296       *
 297       * @return string  Permutated string.
 298       */
 299      function _initialPerm($input)
 300      {
 301          // TODO: Some stylie bitwise thing instead.
 302  
 303          $input_bin = '';
 304          for ($i = 0; $i < 8; $i++) {
 305              $input_bin .= str_pad(decbin(ord($input[$i])), 8, '0', STR_PAD_LEFT);
 306          }
 307  
 308          $output_bin = '';
 309          foreach ($this->ip as $offset) {
 310              $output_bin .= $input_bin[$offset - 1];
 311          }
 312  
 313          $output = '';
 314          for ($i = 0; $i < 8; $i++) {
 315              $output .= chr(bindec(substr($output_bin, 8 * $i, 8)));
 316          }
 317  
 318          return $output;
 319      }
 320  
 321      /**
 322       * Put an input string through a final permutation.
 323       *
 324       * @param string $input  Input string.
 325       *
 326       * @return string  Permutated string.
 327       */
 328      function _finalPerm($input)
 329      {
 330          // TODO: Some stylie bitwise thing instead.
 331          $input_bin = '';
 332          for ($i = 0; $i < 8; $i++) {
 333              $input_bin .= str_pad(decbin(ord($input[$i])), 8, '0', STR_PAD_LEFT);
 334          }
 335  
 336          $output_bin = '';
 337          foreach ($this->fp as $offset) {
 338              $output_bin .= $input_bin[$offset - 1];
 339          }
 340  
 341          $output = '';
 342          for ($i = 0; $i < 8; $i++) {
 343              $output .= chr(bindec(substr($output_bin, 8 * $i, 8)));
 344          }
 345  
 346          return $output;
 347      }
 348  
 349  
 350      /**
 351       * f() The permutation function.
 352       *
 353       * @param string $input  Input string.
 354       * @param integer $round  The round.
 355       *
 356       * @return string  The output string.
 357       */
 358      function _f($input, $round)
 359      {
 360          // TODO: Some stylie bitwise thing instead.
 361          $key = $this->_Ks[$round];
 362  
 363          $input_bin = '';
 364          for ($i = 0; $i < 4; $i++) {
 365              $input_bin .= str_pad(decbin(ord($input[$i])), 8, '0', STR_PAD_LEFT);
 366          }
 367  
 368          $expanded_bin = '';
 369          foreach ($this->e as $offset) {
 370              $expanded_bin .= $input_bin[$offset - 1];
 371          }
 372  
 373          $expanded = array();
 374          for ($i = 0; $i < 8; $i++) {
 375              $expanded[$i] = bindec('00' . substr($expanded_bin, $i * 6, 6)) ^ $key[$i];
 376          }
 377  
 378          $combined_bin = '';
 379          for ($i = 0; $i < 8; $i++) {
 380              $s_index = (($expanded[$i] & 0x20) >> 4) | ($expanded[$i] & 0x01);
 381              $s_index = 16 * $s_index + (($expanded[$i] & 0x1E) >> 1);
 382              $val = $this->_s[$i + 1][$s_index];
 383              $combined_bin .= str_pad(decbin($val), 4, '0', STR_PAD_LEFT);
 384          }
 385  
 386          $output_bin = '';
 387          foreach ($this->p as $offset) {
 388              $output_bin .= $combined_bin[$offset - 1];
 389          }
 390  
 391          $output = '';
 392          for ($i = 0; $i < 4; $i++) {
 393              $output .= chr(bindec(substr($output_bin, $i * 8, 8)));
 394          }
 395  
 396          return $output;
 397      }
 398  
 399      /**
 400       * Create the complete key shedule.
 401       *
 402       * @param string $key  The key to use.
 403       *
 404       * @return array Key schedule.
 405       */
 406      function _keySchedule($key)
 407      {
 408          $key = str_pad($key, 8, "\0");
 409          $ks = array();
 410  
 411          $key_bin = '';
 412          for ($i = 0; $i < 8; $i++) {
 413              $key_bin .= str_pad(decbin(ord($key[$i])), 8, '0', STR_PAD_LEFT);
 414          }
 415  
 416          $c = '';
 417          $d = '';
 418          for ($i = 0; $i < 28; $i++) {
 419              $c .= $key_bin[$this->pc1[$i] - 1];
 420              $d .= $key_bin[$this->pc1[28 + $i] - 1];
 421          }
 422  
 423          for ($i = 0; $i < 16; $i++) {
 424              $c = substr($c, $this->shifts[$i]) . substr($c, 0, $this->shifts[$i]);
 425              $d = substr($d, $this->shifts[$i]) . substr($d, 0, $this->shifts[$i]);
 426  
 427              $cd = $c . $d;
 428  
 429              $permutated_bin = '';
 430              foreach ($this->pc2 as $offset) {
 431                  $permutated_bin .= $cd[$offset - 1];
 432              }
 433  
 434              for ($j = 0; $j < 8; $j++) {
 435                  $ks[$i + 1][] = bindec('00' . substr($permutated_bin, $j * 6, 6));
 436              }
 437          }
 438  
 439          return $ks;
 440      }
 441  
 442  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7