Completed
Push — development ( a4b5b1...dcab98 )
by Nils
07:16
created
includes/libraries/phpcrypt/ciphers/Rijndael.php 1 patch
Braces   +103 added lines, -77 removed lines patch added patch discarded remove patch
@@ -188,12 +188,13 @@  discard block
 block discarded – undo
188 188
         // if the key and block size is 16, do 10 rounds
189 189
         // if the key or block size is 24, and neither is longer than 24, do 12 rounds
190 190
         // if either key or block size is 32, do 14 rounds
191
-        if ($key_sz == 16 && $blk_sz == 16)
192
-            $loops = 10;
193
-        else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24)
194
-            $loops = 12;
195
-        else if ($key_sz == 32 || $blk_sz == 32)
196
-            $loops = 14;
191
+        if ($key_sz == 16 && $blk_sz == 16) {
192
+                    $loops = 10;
193
+        } else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) {
194
+                    $loops = 12;
195
+        } else if ($key_sz == 32 || $blk_sz == 32) {
196
+                    $loops = 14;
197
+        }
197 198
 
198 199
         // now begin the encryption
199 200
         $this->addRoundKey($text, 0);
@@ -204,8 +205,9 @@  discard block
 block discarded – undo
204 205
             $this->shiftRow($text);
205 206
 
206 207
             // the last iteration does not use mixColumn
207
-            if ($i < $loops)
208
-                $this->mixColumn($text);
208
+            if ($i < $loops) {
209
+                            $this->mixColumn($text);
210
+            }
209 211
 
210 212
             $this->addRoundKey($text, $i);
211 213
         }
@@ -232,12 +234,13 @@  discard block
 block discarded – undo
232 234
         // if the key and block size is 16, do 10 rounds
233 235
         // if the key or block size is 24, and neither is longer than 24, do 12 rounds
234 236
         // if either key or block size is 32, do 14 rounds
235
-        if ($key_sz == 16 && $blk_sz == 16)
236
-            $loops = 10;
237
-        else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24)
238
-            $loops = 12;
239
-        else if ($key_sz == 32 || $blk_sz == 32)
240
-            $loops = 14;
237
+        if ($key_sz == 16 && $blk_sz == 16) {
238
+                    $loops = 10;
239
+        } else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) {
240
+                    $loops = 12;
241
+        } else if ($key_sz == 32 || $blk_sz == 32) {
242
+                    $loops = 14;
243
+        }
241 244
 
242 245
         // now begin the decryption
243 246
         $this->addRoundKey($text, 0);
@@ -249,8 +252,9 @@  discard block
 block discarded – undo
249 252
             $this->addRoundKey($text, $i);
250 253
 
251 254
             // the last iteration does not use mixColumn
252
-            if ($i < $loops)
253
-                $this->mixColumn($text);
255
+            if ($i < $loops) {
256
+                            $this->mixColumn($text);
257
+            }
254 258
         }
255 259
 
256 260
         return true;
@@ -280,8 +284,9 @@  discard block
 block discarded – undo
280 284
     protected function mixColumnMultiply($m, $byte)
281 285
     {
282 286
         // if multiplying by 1, then we just return the same number
283
-        if ($m == 0x01)
284
-            return $byte;
287
+        if ($m == 0x01) {
288
+                    return $byte;
289
+        }
285 290
 
286 291
         $hex = parent::dec2Hex($byte);
287 292
         $row = parent::hex2Dec($hex[0]);
@@ -289,28 +294,34 @@  discard block
 block discarded – undo
289 294
         $pos = ($row * 16) + $col;
290 295
 
291 296
         // multiply by 2 (comes from self::$_matrix_mult during encryption)
292
-        if ($m == 0x02)
293
-            return self::$_gm2[$pos];
297
+        if ($m == 0x02) {
298
+                    return self::$_gm2[$pos];
299
+        }
294 300
 
295 301
         // multiply by 3 (comes from self::$_matrix_mult during encryption)
296
-        if ($m == 0x03)
297
-            return self::$_gm3[$pos];
302
+        if ($m == 0x03) {
303
+                    return self::$_gm3[$pos];
304
+        }
298 305
 
299 306
         // multiply by 9 (comes from self::$_matrix_mult_inv during decryption)
300
-        if ($m == 0x09)
301
-            return self::$_gm9[$pos];
307
+        if ($m == 0x09) {
308
+                    return self::$_gm9[$pos];
309
+        }
302 310
 
303 311
         // multiply by 11 (comes from self::$_matrix_mult_inv during decryption)
304
-        if ($m == 0x0b)
305
-            return self::$_gm11[$pos];
312
+        if ($m == 0x0b) {
313
+                    return self::$_gm11[$pos];
314
+        }
306 315
 
307 316
         // multiply by 13 (comes from self::$_matrix_mult_inv during decryption)
308
-        if ($m == 0x0d)
309
-            return self::$_gm13[$pos];
317
+        if ($m == 0x0d) {
318
+                    return self::$_gm13[$pos];
319
+        }
310 320
 
311 321
         // multiply by 14 (comes from self::$_matrix_mult_inv during decryption)
312
-        if ($m == 0x0e)
313
-            return self::$_gm14[$pos];
322
+        if ($m == 0x0e) {
323
+                    return self::$_gm14[$pos];
324
+        }
314 325
     }
315 326
 
316 327
 
@@ -331,13 +342,15 @@  discard block
 block discarded – undo
331 342
         $ek_len = strlen($this->xkey);
332 343
         $len = $this->blockSize();
333 344
 
334
-        if ($this->operation() == parent::ENCRYPT)
335
-            $offset = $round * $len;
336
-        else
337
-            $offset = ($ek_len - ($round * $len)) - $len;
345
+        if ($this->operation() == parent::ENCRYPT) {
346
+                    $offset = $round * $len;
347
+        } else {
348
+                    $offset = ($ek_len - ($round * $len)) - $len;
349
+        }
338 350
 
339
-        for ($i = 0; $i < $len; ++$i)
340
-            $text[$i] = $text[$i] ^ $this->xkey[$offset + $i];
351
+        for ($i = 0; $i < $len; ++$i) {
352
+                    $text[$i] = $text[$i] ^ $this->xkey[$offset + $i];
353
+        }
341 354
     }
342 355
 
343 356
 
@@ -361,10 +374,12 @@  discard block
 block discarded – undo
361 374
             $pos = ($row * 16) + $col;
362 375
 
363 376
             // return the corresponding value from the sbox
364
-            if ($this->operation() == parent::ENCRYPT)
365
-                $text[$i] = chr(self::$_s[$pos]);
366
-            else // parent::DECRYPT uses the inverse sbox
377
+            if ($this->operation() == parent::ENCRYPT) {
378
+                            $text[$i] = chr(self::$_s[$pos]);
379
+            } else {
380
+                // parent::DECRYPT uses the inverse sbox
367 381
                 $text[$i] = chr(self::$_s_inv[$pos]);
382
+            }
368 383
         }
369 384
     }
370 385
 
@@ -381,10 +396,12 @@  discard block
 block discarded – undo
381 396
         $tmp = $t;
382 397
 
383 398
         // the matrix we use depends on if we are encrypting or decrypting
384
-        if ($this->operation() == parent::ENCRYPT)
385
-            $m = self::$_matrix_mult;
386
-        else // parent::DECRYPT
399
+        if ($this->operation() == parent::ENCRYPT) {
400
+                    $m = self::$_matrix_mult;
401
+        } else {
402
+            // parent::DECRYPT
387 403
             $m = self::$_matrix_mult_inv;
404
+        }
388 405
 
389 406
         // the number of rounds we make depends on the block size of the text
390 407
         // used during encryption/decryption
@@ -619,12 +636,13 @@  discard block
 block discarded – undo
619 636
      */
620 637
     protected function expandKey()
621 638
     {
622
-        if ($this->keySize() == 16)
623
-            return $this->expandKey128();
624
-        else if ($this->keySize() == 24)
625
-            return $this->expandKey192();
626
-        else if ($this->keySize() == 32)
627
-            return $this->expandKey256();
639
+        if ($this->keySize() == 16) {
640
+                    return $this->expandKey128();
641
+        } else if ($this->keySize() == 24) {
642
+                    return $this->expandKey192();
643
+        } else if ($this->keySize() == 32) {
644
+                    return $this->expandKey256();
645
+        }
628 646
     }
629 647
 
630 648
 
@@ -642,19 +660,22 @@  discard block
 block discarded – undo
642 660
 
643 661
         // the number of rounds we make depends on the block size of the text
644 662
         // used during encryption/decryption
645
-        if ($this->blockSize() == 16)
646
-            $max = 44;
647
-        if ($this->blockSize() == 24)
648
-            $max = 78;
649
-        if ($this->blockSize() == 32)
650
-            $max = 120;
663
+        if ($this->blockSize() == 16) {
664
+                    $max = 44;
665
+        }
666
+        if ($this->blockSize() == 24) {
667
+                    $max = 78;
668
+        }
669
+        if ($this->blockSize() == 32) {
670
+                    $max = 120;
671
+        }
651 672
 
652 673
         // 16 byte key expands to 176 bytes
653 674
         for ($i = 0; $i < $max; ++$i)
654 675
         {
655
-            if ($i >= 0 && $i <= 3)
656
-                $this->xkey .= $this->k($i * 4);
657
-            else if (($i % 4) == 0)
676
+            if ($i >= 0 && $i <= 3) {
677
+                            $this->xkey .= $this->k($i * 4);
678
+            } else if (($i % 4) == 0)
658 679
             {
659 680
                 // rotate the 4 bytes
660 681
                 $subword = $this->rotWord($this->ek(($i - 1) * 4));
@@ -701,19 +722,22 @@  discard block
 block discarded – undo
701 722
 
702 723
         // the number of rounds we make depends on the block size of the text
703 724
         // used during encryption/decryption
704
-        if ($this->blockSize() == 16)
705
-            $max = 52;
706
-        if ($this->blockSize() == 24)
707
-            $max = 78;
708
-        if ($this->blockSize() == 32)
709
-            $max = 120;
725
+        if ($this->blockSize() == 16) {
726
+                    $max = 52;
727
+        }
728
+        if ($this->blockSize() == 24) {
729
+                    $max = 78;
730
+        }
731
+        if ($this->blockSize() == 32) {
732
+                    $max = 120;
733
+        }
710 734
 
711 735
         // 24 byte key expands to 208 bytes
712 736
         for ($i = 0; $i < $max; ++$i)
713 737
         {
714
-            if ($i >= 0 && $i <= 5)
715
-                $this->xkey .= $this->k($i * 4);
716
-            else if (($i % 6) == 0)
738
+            if ($i >= 0 && $i <= 5) {
739
+                            $this->xkey .= $this->k($i * 4);
740
+            } else if (($i % 6) == 0)
717 741
             {
718 742
                 // rotate the 4 bytes
719 743
                 $subword = $this->rotWord($this->ek(($i - 1) * 4));
@@ -760,19 +784,22 @@  discard block
 block discarded – undo
760 784
 
761 785
         // the number of rounds we make depends on the block size of the text
762 786
         // used during encryption/decryption
763
-        if ($this->blockSize() == 16)
764
-            $max = 60;
765
-        if ($this->blockSize() == 24)
766
-            $max = 90;
767
-        if ($this->blockSize() == 32)
768
-            $max = 120;
787
+        if ($this->blockSize() == 16) {
788
+                    $max = 60;
789
+        }
790
+        if ($this->blockSize() == 24) {
791
+                    $max = 90;
792
+        }
793
+        if ($this->blockSize() == 32) {
794
+                    $max = 120;
795
+        }
769 796
 
770 797
         // 32 byte key expands to 240 bytes
771 798
         for ($i = 0; $i < $max; ++$i)
772 799
         {
773
-            if ($i >= 0 && $i <= 7)
774
-                $this->xkey .= $this->k($i * 4);
775
-            else if ($i % 8 == 0)
800
+            if ($i >= 0 && $i <= 7) {
801
+                            $this->xkey .= $this->k($i * 4);
802
+            } else if ($i % 8 == 0)
776 803
             {
777 804
                 // rotate the 4 bytes
778 805
                 $subword = $this->rotWord($this->ek(($i - 1) * 4));
@@ -791,8 +818,7 @@  discard block
 block discarded – undo
791 818
                 $h3 = parent::str2Hex($ek);
792 819
                 $res = parent::xorHex($h1, $h2, $h3);
793 820
                 $this->xkey .= parent::hex2Str($res);
794
-            }
795
-            else if ($i % 4 == 0)
821
+            } else if ($i % 4 == 0)
796 822
             {
797 823
                 // get the subsitution from the s-box
798 824
                 $subword = $this->ek(($i - 1) * 4);
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/SimpleXOR.php 1 patch
Braces   +3 added lines, -2 removed lines patch added patch discarded remove patch
@@ -121,8 +121,9 @@
 block discarded – undo
121 121
         {
122 122
             // if the current position in the key reaches the end of the key,
123 123
             // start over at position 0 of the key
124
-            if ($pos >= $this->keySize())
125
-                $pos = 0;
124
+            if ($pos >= $this->keySize()) {
125
+                            $pos = 0;
126
+            }
126 127
 
127 128
             $text[$i] = $text[$i] ^ $key[$pos];
128 129
             ++$pos;
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/ARC4.php 1 patch
Braces   +6 added lines, -4 removed lines patch added patch discarded remove patch
@@ -106,8 +106,9 @@  discard block
 block discarded – undo
106 106
         $len = strlen($text);
107 107
         $this->prga($len);
108 108
 
109
-        for ($i = 0; $i < $len; ++$i)
110
-            $text[$i] = $text[$i] ^ $this->_key_stream[$i];
109
+        for ($i = 0; $i < $len; ++$i) {
110
+                    $text[$i] = $text[$i] ^ $this->_key_stream[$i];
111
+        }
111 112
 
112 113
         return true;
113 114
     }
@@ -126,8 +127,9 @@  discard block
 block discarded – undo
126 127
         $key = $this->key();
127 128
 
128 129
         // fill $this->_s with all the values from 0-255
129
-        for ($i = 0; $i < 256; ++$i)
130
-            $this->_s[$i] = $i;
130
+        for ($i = 0; $i < 256; ++$i) {
131
+                    $this->_s[$i] = $i;
132
+        }
131 133
 
132 134
         // the changing S List
133 135
         for ($i = 0; $i < 256; ++$i)
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/CAST256.php 1 patch
Braces   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -81,8 +81,7 @@  discard block
 block discarded – undo
81 81
         {
82 82
             $key = substr($key, 0, self::BYTES_KEY_MAX);
83 83
             $keylen = self::BYTES_KEY_MAX;
84
-        }
85
-        else if (!in_array($keylen, self::$_req_key_sizes))
84
+        } else if (!in_array($keylen, self::$_req_key_sizes))
86 85
         {
87 86
             $msg  = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, ";
88 87
             $msg .= "20, 24, 28, or 32 bytes.";
@@ -298,8 +297,9 @@  discard block
 block discarded – undo
298 297
 
299 298
         // if the key is less than 32 bytes, pad it to 32 bytes
300 299
         // for the key expansion
301
-        if ($this->keySize() < 32)
302
-            $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT);
300
+        if ($this->keySize() < 32) {
301
+                    $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT);
302
+        }
303 303
 
304 304
         // split the key up into 4 byte parts, reverse the string,
305 305
         // then convert each part into a 32 bit integer
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/3Way.php 1 patch
Braces   +8 added lines, -6 removed lines patch added patch discarded remove patch
@@ -140,10 +140,11 @@  discard block
 block discarded – undo
140 140
         $key = array_map("parent::str2Dec", $key);
141 141
 
142 142
         // determine which round constant to use
143
-        if ($this->operation() == parent::ENCRYPT)
144
-            $rcon = self::$_rcon_enc;
145
-        else
146
-            $rcon = self::$_rcon_dec;
143
+        if ($this->operation() == parent::ENCRYPT) {
144
+                    $rcon = self::$_rcon_enc;
145
+        } else {
146
+                    $rcon = self::$_rcon_dec;
147
+        }
147 148
 
148 149
         if ($this->operation() == parent::DECRYPT)
149 150
         {
@@ -168,8 +169,9 @@  discard block
 block discarded – undo
168 169
 
169 170
         $this->theta($data);
170 171
 
171
-        if ($this->operation() == parent::DECRYPT)
172
-            $this->invertBits($data);
172
+        if ($this->operation() == parent::DECRYPT) {
173
+                    $this->invertBits($data);
174
+        }
173 175
 
174 176
         // assemble the three 32 bit parts back to a 96 bit string
175 177
         $data = parent::dec2Str($data[0], 4).parent::dec2Str($data[1], 4).
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/Vigenere.php 1 patch
Braces   +6 added lines, -5 removed lines patch added patch discarded remove patch
@@ -175,9 +175,9 @@  discard block
 block discarded – undo
175 175
         // The key must be prepared so that it is the same length as the
176 176
         // message. If it is longer or shorter we need to modify it
177 177
         // to make it the correct length
178
-        if ($keylen > $len)
179
-            $this->expanded_key = substr($this->expanded_key, 0, $len);
180
-        else if ($len > $keylen)
178
+        if ($keylen > $len) {
179
+                    $this->expanded_key = substr($this->expanded_key, 0, $len);
180
+        } else if ($len > $keylen)
181 181
         {
182 182
             // if the key is shorter than the message, then we need pad the key
183 183
             // by repeating it until it is the correct length
@@ -186,8 +186,9 @@  discard block
 block discarded – undo
186 186
 
187 187
             for ($i = 0; $i < $diff; ++$i)
188 188
             {
189
-                if ($pos >= $keylen)
190
-                    $pos = 0;
189
+                if ($pos >= $keylen) {
190
+                                    $pos = 0;
191
+                }
191 192
 
192 193
                 $this->expanded_key .= $this->expanded_key[$pos];
193 194
                 ++$pos;
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/ciphers/DES.php 1 patch
Indentation   +552 added lines, -552 removed lines patch added patch discarded remove patch
@@ -38,563 +38,563 @@
 block discarded – undo
38 38
  */
39 39
 class Cipher_DES extends Cipher
40 40
 {
41
-	/** @type integer BYTES_BLOCK The block size, in bytes */
42
-	const BYTES_BLOCK = 8; // 64 bits
41
+    /** @type integer BYTES_BLOCK The block size, in bytes */
42
+    const BYTES_BLOCK = 8; // 64 bits
43 43
 
44
-	/** @type integer BYTES_KEY The key size, in bytes */
45
-	const BYTES_KEY = 8; // 64 bits
44
+    /** @type integer BYTES_KEY The key size, in bytes */
45
+    const BYTES_KEY = 8; // 64 bits
46 46
 
47
-	/** @type array $sub_keys The permutated subkeys */
48
-	protected $sub_keys = array();
47
+    /** @type array $sub_keys The permutated subkeys */
48
+    protected $sub_keys = array();
49 49
 
50
-	/*
50
+    /*
51 51
 	 * Tables initialized in the initTables()
52 52
 	 */
53 53
 
54
-	/**
55
-	 * @type array $_pc1 Permutated choice 1 (PC1),
56
-	 * This should be considered a constant
57
-	 */
58
-	protected static $_pc1 = array();
59
-
60
-	/**
61
-	 * @type array $_pc2 Permutated choice 2 (PC2),
62
-	 * This should be considered a constant
63
-	 */
64
-	protected static $_pc2 = array();
65
-
66
-	/**
67
-	 * @type array $_key_sched The key schedule,
68
-	 * This should be considered a constant
69
-	 */
70
-	protected static $_key_sched = array();
71
-
72
-	/**
73
-	 * @type array $_ip The Initial Permutation (IP),
74
-	 * This should be considered a constant
75
-	 */
76
-	private static $_ip = array();
77
-
78
-	/**
79
-	 * @type array $_e The Expansion table (E),
80
-	 * This should be considered a constant
81
-	 */
82
-	private static $_e = array();
83
-
84
-	/**
85
-	 * @type array $_s The Substitution Box (S),
86
-	 * This should be considered a constant
87
-	 */
88
-	private static $_s = array();
89
-
90
-	/**
91
-	 * @type array $_p The Permutation table (P),
92
-	 * This should be considered a constant
93
-	 */
94
-	private static $_p = array();
95
-
96
-	/**
97
-	 * @type array $_ip The The Final Permutation table (FP),
98
-	 * This should be considered a constant
99
-	 */
100
-	private static $_fp = array();
101
-
102
-
103
-	/**
104
-	 * Constructor, used only when calling this class directly
105
-	 * for classes that extend this class, call __construct1()
106
-	 *
107
-	 * @param string $key The key used for Encryption/Decryption
108
-	 * @return void
109
-	 */
110
-	public function __construct($key)
111
-	{
112
-		// set the DES key
113
-		parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY);
114
-
115
-		// initialize variables
116
-		$this->initTables();
117
-
118
-		// DES requires that data is 64 bits
119
-		$this->blockSize(self::BYTES_BLOCK);
120
-
121
-		// create the 16 rounds of 56 bit keys
122
-		$this->keyPermutation();
123
-	}
124
-
125
-
126
-	/**
127
-	 * Second Constructor, used only by child classes that extend this class
128
-	 *
129
-	 * @param string $cipher The name of the cipher extending this class
130
-	 * @param string $key The key used for Encryption/Decryption
131
-	 * @param integer $key_byte_sz The required byte size of the extending cipher
132
-	 * @return void
133
-	 */
134
-	protected function __construct1($cipher, $key, $key_byte_sz)
135
-	{
136
-		// set the key and key size
137
-		parent::__construct($cipher, $key, $key_byte_sz);
138
-
139
-		// initialize variables
140
-		$this->initTables();
141
-	}
142
-
143
-
144
-	/**
145
-	 * Destructor
146
-	 *
147
-	 * @return void
148
-	 */
149
-	public function __destruct()
150
-	{
151
-		parent::__destruct();
152
-	}
153
-
154
-
155
-	/**
156
-	 * Encrypt plain text data using DES
157
-	 *
158
-	 * @return boolean Returns true
159
-	 */
160
-	public function encrypt(&$text)
161
-	{
162
-		$this->operation(parent::ENCRYPT);
163
-		return $this->des($text);
164
-	}
165
-
166
-
167
-	/**
168
-	 * Decrypt a DES encrypted string
169
-	 *
170
-	 * @return boolean Returns true
171
-	 */
172
-	public function decrypt(&$text)
173
-	{
174
-		$this->operation(parent::DECRYPT);
175
-		return $this->des($text);
176
-	}
177
-
178
-
179
-	/**
180
-	 * This is where the actual encrypt/decryption takes place. Since
181
-	 * encryption and decryption are the same algorithm in DES, we only
182
-	 * need one function to do both.
183
-	 *
184
-	 * @param string $data The string to be encrypted or decrypted
185
-	 * @return boolean Returns true
186
-	 */
187
-	protected function des(&$data)
188
-	{
189
-		$l = array();
190
-		$r = array();
191
-
192
-		// step two: Initial Permutation (IP) of plaintext
193
-		$data = $this->ip($data);
194
-
195
-		// divide the permuted block IP into a left half L0 of 32 bits,
196
-		// and a right half R0 of 32 bits
197
-		$l[0] = substr($data, 0, 32);
198
-		$r[0] = substr($data, 32, 32);
199
-
200
-		for ($n = 1; $n <= 16; ++$n)
201
-		{
202
-			$l[$n] = $r[$n - 1];
203
-
204
-			if ($this->operation() == parent::DECRYPT) {
205
-							$f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]);
206
-			} else {
207
-							$f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]);
208
-			}
209
-
210
-			// XOR F with Ln
211
-			$r[$n] = $this->xorBin($l[$n - 1], $f);
212
-		}
213
-
214
-		// now we combine L[16] and R[16] back into a 64-bit string, but we reverse
215
-		// L[16] and R[16] so that it becomes R[16]L[16]
216
-		$data = $r[16].$l[16];
217
-
218
-		// now do the final permutation
219
-		$data = $this->fp($data);
220
-		$data = parent::bin2Str($data);
221
-
222
-		return true;
223
-	}
224
-
225
-
226
-	/**
227
-	 * The Key permutation, based on tables $_pc1 and $_pc2
228
-	 * Create 16 subkeys, each of which is 48-bits long.
229
-	 *
230
-	 * @return void
231
-	 */
232
-	private function keyPermutation()
233
-	{
234
-		$this->sub_keys = array();
235
-		$pc1m = array();
236
-		$pcr = array();
237
-		$c = array();
238
-		$d = array();
239
-
240
-		// convert the key to binary
241
-		$binkey = parent::str2Bin($this->key());
242
-
243
-		// reduce the key down to 56bits based on table $_pc1
244
-		for ($i = 0; $i < 56; ++$i) {
245
-		            $pc1m[$i] = $binkey[self::$_pc1[$i] - 1];
246
-		}
247
-
248
-		// split $pc1m in half (C0 and D0)
249
-		$c[0] = array_slice($pc1m, 0, 28);
250
-		$d[0] = array_slice($pc1m, 28, 28);
251
-
252
-		// now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn
253
-		// where 1 <= n <= 16
254
-		for ($i = 1; $i <= 16; ++$i)
255
-		{
256
-			// now set the next Cn and Dn as the previous Cn and Dn
257
-			$c[$i] = $c[$i - 1];
258
-			$d[$i] = $d[$i - 1];
259
-
260
-			for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j)
261
-			{
262
-				// do a left shift, move each bit one place to the left,
263
-				// except for the first bit, which is cycled to the end
264
-				// of the block.
265
-				$c[$i][] = array_shift($c[$i]);
266
-				$d[$i][] = array_shift($d[$i]);
267
-			}
268
-
269
-			// We now form the sub_keys (Kn), for 1<=n<=16, by applying the
270
-			// following permutation table to each of the concatenated
271
-			// pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48
272
-			// of these.
273
-			$CnDn = array_merge($c[$i], $d[$i]);
274
-			$this->sub_keys[$i - 1] = "";
275
-			for ($j = 0; $j < 48; ++$j) {
276
-							$this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1];
277
-			}
278
-		}
279
-
280
-		// the sub_keys are created, we are done with the key permutation
281
-	}
282
-
283
-
284
-	/**
285
-	 * Initial Permutation (IP)
286
-	 * Now we encode each 64-bit block of data. There is an initial permutation IP of
287
-	 * the 64 bits of the message data M. This rearranges the bits according to the
288
-	 * following table, where the entries in the table show the new arrangement of the
289
-	 * bits from their initial order. The 58th bit of M becomes the first bit of IP.
290
-	 * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last
291
-	 * bit of IP.
292
-	 *
293
-	 * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271):
294
-	 * The initial permution was used to make it easier to load plain text and cipher text
295
-	 * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP
296
-	 * are not necessary in software implementations and do not affect the security. However,
297
-	 * the IP and FP are part of the DES standard and not implementing it would deviate from
298
-	 * the standard, so we will do it here in phpCrypt.
299
-	 *
300
-	 * @param string $text
301
-	 * @return string the Initial Permutation (IP)
302
-	 */
303
-	private function ip($text)
304
-	{
305
-		$text = parent::str2Bin($text);
306
-		$ip = "";
307
-
308
-		// loop through the 64 bit block, ordering it occording to $_ip
309
-		for ($i = 0; $i < 64; ++$i) {
310
-					$ip .= $text[self::$_ip[$i] - 1];
311
-		}
312
-
313
-		return $ip;
314
-	}
315
-
316
-
317
-	/**
318
-	 * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits.
319
-	 * This is done by using a selection table that repeats some of the bits in Rn-1. We'll
320
-	 * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input
321
-	 * block, and a 48 bit output block.
322
-	 *
323
-	 * @param string $r  32 bit binary, each bit in an array element
324
-	 * @param string $k 48 bit binary string
325
-	 * @return string 48 bit binary string
326
-	 */
327
-	private function f($r, $k)
328
-	{
329
-		$bin = parent::xorBin($k, $this->E($r));
330
-
331
-		// create a 32-bit string from $bits by passing it through the S-Boxes
332
-		$bin = $this->s($bin);
333
-
334
-		// now send permute $bin as defined by table self::$_p
335
-		$bin = $this->p($bin);
336
-
337
-		return $bin;
338
-	}
339
-
340
-
341
-	/**
342
-	 * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of
343
-	 * 6 bits each, are obtained by selecting the bits in its inputs in order according
344
-	 * to the self::$_e[] table.
345
-	 * This is only used in the F() function
346
-	 *
347
-	 * @param string $r 32 bit binary, each bit in an array element
348
-	 * @return string 48 bit binary string
349
-	 */
350
-	private function e($r)
351
-	{
352
-		$e = "";
353
-		for ($i = 0; $i < 48; ++$i) {
354
-					$e .= $r[self::$_e[$i] - 1];
355
-		}
356
-
357
-		return $e;
358
-	}
359
-
360
-
361
-	/**
362
-	 * S-Box
363
-	 * Take a 48-bit string from F() and run it through the S-Boxes, this requires
364
-	 * us to break up the 48-bit string into 8 groups of 6 bits before sending it
365
-	 * through the S-Boxes
366
-	 *
367
-	 * @param string $bits The 48-bit string from F() to be processed
368
-	 * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes
369
-	 */
370
-	private function s($bits)
371
-	{
372
-		$s = "";
373
-
374
-		for ($i = 0; $i <= 42; $i += 6)
375
-		{
376
-			$sbits = substr($bits, $i, 6);
377
-
378
-			// we need to determine the S-Box column number and row number
379
-			// from the 6 bit string passed in, this is done using the following method:
380
-			// The First & Last bits represent a number between 0-3, used to determine which row
381
-			// The middle 4 bits represent a number between 0-15, used to determine the column
382
-			$row = bindec("{$sbits[0]}{$sbits[5]}");
383
-			$col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}");
384
-
385
-			// determine the position in the S-BOX, S-Box table is in self::$_s[]
386
-			$pos = ($row * 16) + $col;
387
-
388
-			// get the integer from the S-Box and convert it to binary
389
-			$bin = decbin(self::$_s[($i / 6)][$pos]);
390
-			$s .= str_pad($bin, 4, "0", STR_PAD_LEFT);
391
-		}
392
-
393
-		return $s;
394
-	}
395
-
396
-
397
-	/**
398
-	 * Permutation P
399
-	 * The permutation P is defined in self::$_p. P() returns a 32-bit output
400
-	 * from a 32-bit input from a binary string from the S-BOX by permuting
401
-	 * the bits of the input block.
402
-	 * This is only used inside of F() function
403
-	 *
404
-	 * @param string $s A 32-bit string originating from being passed through S-Box
405
-	 * @return string A 32-bit string, which is $s permuted through table self::$_p
406
-	 */
407
-	private function p($s)
408
-	{
409
-		$p = "";
410
-		for ($i = 0; $i < 32; ++$i) {
411
-					$p .= $s[self::$_p[$i] - 1];
412
-		}
413
-
414
-		return $p;
415
-	}
416
-
417
-
418
-	/**
419
-	 * Final Permutation (FP)
420
-	 * Read the comment about IP and FP being unecessary in software implmented DES (though
421
-	 * we will do it to follow the DES standard).
422
-	 *
423
-	 * @param string $bin A 64-bit binary string
424
-	 * @return string A 64-bit binary string that has been run through self::$_fp[] table
425
-	 */
426
-	private function fp($bin)
427
-	{
428
-		$fp = "";
429
-		for ($i = 0; $i < 64; ++$i) {
430
-					$fp .= $bin[self::$_fp[$i] - 1];
431
-		}
432
-
433
-		return $fp;
434
-	}
435
-
436
-
437
-	/**
438
-	 * Initialize all the tables, this function is called inside the constructor
439
-	 *
440
-	 * @return void
441
-	 */
442
-	private function initTables()
443
-	{
444
-		// permuted choice 1 (PC1)
445
-		// these values are chars and should be run through chr() when used
446
-		self::$_pc1 = array(
447
-			57, 49, 41, 33, 25, 17, 9,
448
-			 1, 58, 50, 42, 34, 26, 18,
449
-			10, 2, 59, 51, 43, 35, 27,
450
-			19, 11, 3, 60, 52, 44, 36,
451
-			63, 55, 47, 39, 31, 23, 15,
452
-			 7, 62, 54, 46, 38, 30, 22,
453
-			14, 6, 61, 53, 45, 37, 29,
454
-			21, 13, 5, 28, 20, 12, 4
455
-		);
456
-
457
-		// permuted choice 2 (PC2)
458
-		// these values are chars and should be run through chr() when used
459
-		self::$_pc2 = array(
460
-			14, 17, 11, 24, 1, 5,
461
-			 3, 28, 15, 6, 21, 10,
462
-			23, 19, 12, 4, 26, 8,
463
-			16, 7, 27, 20, 13, 2,
464
-			41, 52, 31, 37, 47, 55,
465
-			30, 40, 51, 45, 33, 48,
466
-			44, 49, 39, 56, 34, 53,
467
-			46, 42, 50, 36, 29, 32
468
-		);
469
-
470
-		// initial permutation (IP)
471
-		self::$_ip = array(
472
-			58, 50, 42, 34, 26, 18, 10, 2,
473
-			60, 52, 44, 36, 28, 20, 12, 4,
474
-			62, 54, 46, 38, 30, 22, 14, 6,
475
-			64, 56, 48, 40, 32, 24, 16, 8,
476
-			57, 49, 41, 33, 25, 17, 9, 1,
477
-			59, 51, 43, 35, 27, 19, 11, 3,
478
-			61, 53, 45, 37, 29, 21, 13, 5,
479
-			63, 55, 47, 39, 31, 23, 15, 7
480
-		);
481
-
482
-		// expansion (E)
483
-		self::$_e = array(
484
-			32, 1, 2, 3, 4, 5,
485
-			 4, 5, 6, 7, 8, 9,
486
-			 8, 9, 10, 11, 12, 13,
487
-			12, 13, 14, 15, 16, 17,
488
-			16, 17, 18, 19, 20, 21,
489
-			20, 21, 22, 23, 24, 25,
490
-			24, 25, 26, 27, 28, 29,
491
-			28, 29, 30, 31, 32, 1
492
-		);
493
-
494
-		// substition box (S)
495
-		self::$_s = array(
496
-			/* S1 */
497
-			array(
498
-				14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
499
-				 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
500
-				 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
501
-				15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
502
-			),
503
-
504
-			/* S2 */
505
-			array(
506
-				15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
507
-				 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
508
-				 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
509
-				13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
510
-			),
511
-
512
-			/* S3 */
513
-			array(
514
-				10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
515
-				13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
516
-				13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
517
-				 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
518
-			),
519
-
520
-			/* S4 */
521
-			array(
522
-				 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
523
-				13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
524
-				10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
525
-				 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
526
-			),
527
-
528
-			/* S5 */
529
-			array(
530
-				 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
531
-				14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
532
-				 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
533
-				11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
534
-			),
535
-
536
-			/* S6 */
537
-			array(
538
-				12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
539
-				10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
540
-				 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
541
-				 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
542
-			),
543
-
544
-			/* S7 */
545
-			array(
546
-				 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
547
-				13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
548
-				 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
549
-				 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
550
-			),
551
-
552
-			/* S8 */
553
-			array(
554
-				13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
555
-				 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
556
-				 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
557
-				 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
558
-			)
559
-		);
560
-
561
-		// permutation (P)
562
-		self::$_p = array(
563
-			16, 7, 20, 21,
564
-			29, 12, 28, 17,
565
-			 1, 15, 23, 26,
566
-			 5, 18, 31, 10,
567
-			 2, 8, 24, 14,
568
-			32, 27, 3, 9,
569
-			19, 13, 30, 6,
570
-			22, 11, 4, 25
571
-		);
572
-
573
-		// final permutation (FP)
574
-		self::$_fp = array(
575
-			40, 8, 48, 16, 56, 24, 64, 32,
576
-			39, 7, 47, 15, 55, 23, 63, 31,
577
-			38, 6, 46, 14, 54, 22, 62, 30,
578
-			37, 5, 45, 13, 53, 21, 61, 29,
579
-			36, 4, 44, 12, 52, 20, 60, 28,
580
-			35, 3, 43, 11, 51, 19, 59, 27,
581
-			34, 2, 42, 10, 50, 18, 58, 26,
582
-			33, 1, 41, 9, 49, 17, 57, 25
583
-		);
584
-
585
-		// key schedule used in KeyPermutation()
586
-		self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1);
587
-	}
588
-
589
-
590
-	/**
591
-	 * Indicates this is a block cipher
592
-	 *
593
-	 * @return integer Returns Cipher::BLOCK
594
-	 */
595
-	public function type()
596
-	{
597
-		return parent::BLOCK;
598
-	}
54
+    /**
55
+     * @type array $_pc1 Permutated choice 1 (PC1),
56
+     * This should be considered a constant
57
+     */
58
+    protected static $_pc1 = array();
59
+
60
+    /**
61
+     * @type array $_pc2 Permutated choice 2 (PC2),
62
+     * This should be considered a constant
63
+     */
64
+    protected static $_pc2 = array();
65
+
66
+    /**
67
+     * @type array $_key_sched The key schedule,
68
+     * This should be considered a constant
69
+     */
70
+    protected static $_key_sched = array();
71
+
72
+    /**
73
+     * @type array $_ip The Initial Permutation (IP),
74
+     * This should be considered a constant
75
+     */
76
+    private static $_ip = array();
77
+
78
+    /**
79
+     * @type array $_e The Expansion table (E),
80
+     * This should be considered a constant
81
+     */
82
+    private static $_e = array();
83
+
84
+    /**
85
+     * @type array $_s The Substitution Box (S),
86
+     * This should be considered a constant
87
+     */
88
+    private static $_s = array();
89
+
90
+    /**
91
+     * @type array $_p The Permutation table (P),
92
+     * This should be considered a constant
93
+     */
94
+    private static $_p = array();
95
+
96
+    /**
97
+     * @type array $_ip The The Final Permutation table (FP),
98
+     * This should be considered a constant
99
+     */
100
+    private static $_fp = array();
101
+
102
+
103
+    /**
104
+     * Constructor, used only when calling this class directly
105
+     * for classes that extend this class, call __construct1()
106
+     *
107
+     * @param string $key The key used for Encryption/Decryption
108
+     * @return void
109
+     */
110
+    public function __construct($key)
111
+    {
112
+        // set the DES key
113
+        parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY);
114
+
115
+        // initialize variables
116
+        $this->initTables();
117
+
118
+        // DES requires that data is 64 bits
119
+        $this->blockSize(self::BYTES_BLOCK);
120
+
121
+        // create the 16 rounds of 56 bit keys
122
+        $this->keyPermutation();
123
+    }
124
+
125
+
126
+    /**
127
+     * Second Constructor, used only by child classes that extend this class
128
+     *
129
+     * @param string $cipher The name of the cipher extending this class
130
+     * @param string $key The key used for Encryption/Decryption
131
+     * @param integer $key_byte_sz The required byte size of the extending cipher
132
+     * @return void
133
+     */
134
+    protected function __construct1($cipher, $key, $key_byte_sz)
135
+    {
136
+        // set the key and key size
137
+        parent::__construct($cipher, $key, $key_byte_sz);
138
+
139
+        // initialize variables
140
+        $this->initTables();
141
+    }
142
+
143
+
144
+    /**
145
+     * Destructor
146
+     *
147
+     * @return void
148
+     */
149
+    public function __destruct()
150
+    {
151
+        parent::__destruct();
152
+    }
153
+
154
+
155
+    /**
156
+     * Encrypt plain text data using DES
157
+     *
158
+     * @return boolean Returns true
159
+     */
160
+    public function encrypt(&$text)
161
+    {
162
+        $this->operation(parent::ENCRYPT);
163
+        return $this->des($text);
164
+    }
165
+
166
+
167
+    /**
168
+     * Decrypt a DES encrypted string
169
+     *
170
+     * @return boolean Returns true
171
+     */
172
+    public function decrypt(&$text)
173
+    {
174
+        $this->operation(parent::DECRYPT);
175
+        return $this->des($text);
176
+    }
177
+
178
+
179
+    /**
180
+     * This is where the actual encrypt/decryption takes place. Since
181
+     * encryption and decryption are the same algorithm in DES, we only
182
+     * need one function to do both.
183
+     *
184
+     * @param string $data The string to be encrypted or decrypted
185
+     * @return boolean Returns true
186
+     */
187
+    protected function des(&$data)
188
+    {
189
+        $l = array();
190
+        $r = array();
191
+
192
+        // step two: Initial Permutation (IP) of plaintext
193
+        $data = $this->ip($data);
194
+
195
+        // divide the permuted block IP into a left half L0 of 32 bits,
196
+        // and a right half R0 of 32 bits
197
+        $l[0] = substr($data, 0, 32);
198
+        $r[0] = substr($data, 32, 32);
199
+
200
+        for ($n = 1; $n <= 16; ++$n)
201
+        {
202
+            $l[$n] = $r[$n - 1];
203
+
204
+            if ($this->operation() == parent::DECRYPT) {
205
+                            $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]);
206
+            } else {
207
+                            $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]);
208
+            }
209
+
210
+            // XOR F with Ln
211
+            $r[$n] = $this->xorBin($l[$n - 1], $f);
212
+        }
213
+
214
+        // now we combine L[16] and R[16] back into a 64-bit string, but we reverse
215
+        // L[16] and R[16] so that it becomes R[16]L[16]
216
+        $data = $r[16].$l[16];
217
+
218
+        // now do the final permutation
219
+        $data = $this->fp($data);
220
+        $data = parent::bin2Str($data);
221
+
222
+        return true;
223
+    }
224
+
225
+
226
+    /**
227
+     * The Key permutation, based on tables $_pc1 and $_pc2
228
+     * Create 16 subkeys, each of which is 48-bits long.
229
+     *
230
+     * @return void
231
+     */
232
+    private function keyPermutation()
233
+    {
234
+        $this->sub_keys = array();
235
+        $pc1m = array();
236
+        $pcr = array();
237
+        $c = array();
238
+        $d = array();
239
+
240
+        // convert the key to binary
241
+        $binkey = parent::str2Bin($this->key());
242
+
243
+        // reduce the key down to 56bits based on table $_pc1
244
+        for ($i = 0; $i < 56; ++$i) {
245
+                    $pc1m[$i] = $binkey[self::$_pc1[$i] - 1];
246
+        }
247
+
248
+        // split $pc1m in half (C0 and D0)
249
+        $c[0] = array_slice($pc1m, 0, 28);
250
+        $d[0] = array_slice($pc1m, 28, 28);
251
+
252
+        // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn
253
+        // where 1 <= n <= 16
254
+        for ($i = 1; $i <= 16; ++$i)
255
+        {
256
+            // now set the next Cn and Dn as the previous Cn and Dn
257
+            $c[$i] = $c[$i - 1];
258
+            $d[$i] = $d[$i - 1];
259
+
260
+            for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j)
261
+            {
262
+                // do a left shift, move each bit one place to the left,
263
+                // except for the first bit, which is cycled to the end
264
+                // of the block.
265
+                $c[$i][] = array_shift($c[$i]);
266
+                $d[$i][] = array_shift($d[$i]);
267
+            }
268
+
269
+            // We now form the sub_keys (Kn), for 1<=n<=16, by applying the
270
+            // following permutation table to each of the concatenated
271
+            // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48
272
+            // of these.
273
+            $CnDn = array_merge($c[$i], $d[$i]);
274
+            $this->sub_keys[$i - 1] = "";
275
+            for ($j = 0; $j < 48; ++$j) {
276
+                            $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1];
277
+            }
278
+        }
279
+
280
+        // the sub_keys are created, we are done with the key permutation
281
+    }
282
+
283
+
284
+    /**
285
+     * Initial Permutation (IP)
286
+     * Now we encode each 64-bit block of data. There is an initial permutation IP of
287
+     * the 64 bits of the message data M. This rearranges the bits according to the
288
+     * following table, where the entries in the table show the new arrangement of the
289
+     * bits from their initial order. The 58th bit of M becomes the first bit of IP.
290
+     * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last
291
+     * bit of IP.
292
+     *
293
+     * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271):
294
+     * The initial permution was used to make it easier to load plain text and cipher text
295
+     * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP
296
+     * are not necessary in software implementations and do not affect the security. However,
297
+     * the IP and FP are part of the DES standard and not implementing it would deviate from
298
+     * the standard, so we will do it here in phpCrypt.
299
+     *
300
+     * @param string $text
301
+     * @return string the Initial Permutation (IP)
302
+     */
303
+    private function ip($text)
304
+    {
305
+        $text = parent::str2Bin($text);
306
+        $ip = "";
307
+
308
+        // loop through the 64 bit block, ordering it occording to $_ip
309
+        for ($i = 0; $i < 64; ++$i) {
310
+                    $ip .= $text[self::$_ip[$i] - 1];
311
+        }
312
+
313
+        return $ip;
314
+    }
315
+
316
+
317
+    /**
318
+     * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits.
319
+     * This is done by using a selection table that repeats some of the bits in Rn-1. We'll
320
+     * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input
321
+     * block, and a 48 bit output block.
322
+     *
323
+     * @param string $r  32 bit binary, each bit in an array element
324
+     * @param string $k 48 bit binary string
325
+     * @return string 48 bit binary string
326
+     */
327
+    private function f($r, $k)
328
+    {
329
+        $bin = parent::xorBin($k, $this->E($r));
330
+
331
+        // create a 32-bit string from $bits by passing it through the S-Boxes
332
+        $bin = $this->s($bin);
333
+
334
+        // now send permute $bin as defined by table self::$_p
335
+        $bin = $this->p($bin);
336
+
337
+        return $bin;
338
+    }
339
+
340
+
341
+    /**
342
+     * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of
343
+     * 6 bits each, are obtained by selecting the bits in its inputs in order according
344
+     * to the self::$_e[] table.
345
+     * This is only used in the F() function
346
+     *
347
+     * @param string $r 32 bit binary, each bit in an array element
348
+     * @return string 48 bit binary string
349
+     */
350
+    private function e($r)
351
+    {
352
+        $e = "";
353
+        for ($i = 0; $i < 48; ++$i) {
354
+                    $e .= $r[self::$_e[$i] - 1];
355
+        }
356
+
357
+        return $e;
358
+    }
359
+
360
+
361
+    /**
362
+     * S-Box
363
+     * Take a 48-bit string from F() and run it through the S-Boxes, this requires
364
+     * us to break up the 48-bit string into 8 groups of 6 bits before sending it
365
+     * through the S-Boxes
366
+     *
367
+     * @param string $bits The 48-bit string from F() to be processed
368
+     * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes
369
+     */
370
+    private function s($bits)
371
+    {
372
+        $s = "";
373
+
374
+        for ($i = 0; $i <= 42; $i += 6)
375
+        {
376
+            $sbits = substr($bits, $i, 6);
377
+
378
+            // we need to determine the S-Box column number and row number
379
+            // from the 6 bit string passed in, this is done using the following method:
380
+            // The First & Last bits represent a number between 0-3, used to determine which row
381
+            // The middle 4 bits represent a number between 0-15, used to determine the column
382
+            $row = bindec("{$sbits[0]}{$sbits[5]}");
383
+            $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}");
384
+
385
+            // determine the position in the S-BOX, S-Box table is in self::$_s[]
386
+            $pos = ($row * 16) + $col;
387
+
388
+            // get the integer from the S-Box and convert it to binary
389
+            $bin = decbin(self::$_s[($i / 6)][$pos]);
390
+            $s .= str_pad($bin, 4, "0", STR_PAD_LEFT);
391
+        }
392
+
393
+        return $s;
394
+    }
395
+
396
+
397
+    /**
398
+     * Permutation P
399
+     * The permutation P is defined in self::$_p. P() returns a 32-bit output
400
+     * from a 32-bit input from a binary string from the S-BOX by permuting
401
+     * the bits of the input block.
402
+     * This is only used inside of F() function
403
+     *
404
+     * @param string $s A 32-bit string originating from being passed through S-Box
405
+     * @return string A 32-bit string, which is $s permuted through table self::$_p
406
+     */
407
+    private function p($s)
408
+    {
409
+        $p = "";
410
+        for ($i = 0; $i < 32; ++$i) {
411
+                    $p .= $s[self::$_p[$i] - 1];
412
+        }
413
+
414
+        return $p;
415
+    }
416
+
417
+
418
+    /**
419
+     * Final Permutation (FP)
420
+     * Read the comment about IP and FP being unecessary in software implmented DES (though
421
+     * we will do it to follow the DES standard).
422
+     *
423
+     * @param string $bin A 64-bit binary string
424
+     * @return string A 64-bit binary string that has been run through self::$_fp[] table
425
+     */
426
+    private function fp($bin)
427
+    {
428
+        $fp = "";
429
+        for ($i = 0; $i < 64; ++$i) {
430
+                    $fp .= $bin[self::$_fp[$i] - 1];
431
+        }
432
+
433
+        return $fp;
434
+    }
435
+
436
+
437
+    /**
438
+     * Initialize all the tables, this function is called inside the constructor
439
+     *
440
+     * @return void
441
+     */
442
+    private function initTables()
443
+    {
444
+        // permuted choice 1 (PC1)
445
+        // these values are chars and should be run through chr() when used
446
+        self::$_pc1 = array(
447
+            57, 49, 41, 33, 25, 17, 9,
448
+                1, 58, 50, 42, 34, 26, 18,
449
+            10, 2, 59, 51, 43, 35, 27,
450
+            19, 11, 3, 60, 52, 44, 36,
451
+            63, 55, 47, 39, 31, 23, 15,
452
+                7, 62, 54, 46, 38, 30, 22,
453
+            14, 6, 61, 53, 45, 37, 29,
454
+            21, 13, 5, 28, 20, 12, 4
455
+        );
456
+
457
+        // permuted choice 2 (PC2)
458
+        // these values are chars and should be run through chr() when used
459
+        self::$_pc2 = array(
460
+            14, 17, 11, 24, 1, 5,
461
+                3, 28, 15, 6, 21, 10,
462
+            23, 19, 12, 4, 26, 8,
463
+            16, 7, 27, 20, 13, 2,
464
+            41, 52, 31, 37, 47, 55,
465
+            30, 40, 51, 45, 33, 48,
466
+            44, 49, 39, 56, 34, 53,
467
+            46, 42, 50, 36, 29, 32
468
+        );
469
+
470
+        // initial permutation (IP)
471
+        self::$_ip = array(
472
+            58, 50, 42, 34, 26, 18, 10, 2,
473
+            60, 52, 44, 36, 28, 20, 12, 4,
474
+            62, 54, 46, 38, 30, 22, 14, 6,
475
+            64, 56, 48, 40, 32, 24, 16, 8,
476
+            57, 49, 41, 33, 25, 17, 9, 1,
477
+            59, 51, 43, 35, 27, 19, 11, 3,
478
+            61, 53, 45, 37, 29, 21, 13, 5,
479
+            63, 55, 47, 39, 31, 23, 15, 7
480
+        );
481
+
482
+        // expansion (E)
483
+        self::$_e = array(
484
+            32, 1, 2, 3, 4, 5,
485
+                4, 5, 6, 7, 8, 9,
486
+                8, 9, 10, 11, 12, 13,
487
+            12, 13, 14, 15, 16, 17,
488
+            16, 17, 18, 19, 20, 21,
489
+            20, 21, 22, 23, 24, 25,
490
+            24, 25, 26, 27, 28, 29,
491
+            28, 29, 30, 31, 32, 1
492
+        );
493
+
494
+        // substition box (S)
495
+        self::$_s = array(
496
+            /* S1 */
497
+            array(
498
+                14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
499
+                    0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
500
+                    4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
501
+                15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
502
+            ),
503
+
504
+            /* S2 */
505
+            array(
506
+                15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
507
+                    3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
508
+                    0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
509
+                13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
510
+            ),
511
+
512
+            /* S3 */
513
+            array(
514
+                10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
515
+                13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
516
+                13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
517
+                    1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
518
+            ),
519
+
520
+            /* S4 */
521
+            array(
522
+                    7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
523
+                13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
524
+                10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
525
+                    3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
526
+            ),
527
+
528
+            /* S5 */
529
+            array(
530
+                    2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
531
+                14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
532
+                    4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
533
+                11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
534
+            ),
535
+
536
+            /* S6 */
537
+            array(
538
+                12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
539
+                10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
540
+                    9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
541
+                    4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
542
+            ),
543
+
544
+            /* S7 */
545
+            array(
546
+                    4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
547
+                13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
548
+                    1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
549
+                    6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
550
+            ),
551
+
552
+            /* S8 */
553
+            array(
554
+                13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
555
+                    1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
556
+                    7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
557
+                    2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
558
+            )
559
+        );
560
+
561
+        // permutation (P)
562
+        self::$_p = array(
563
+            16, 7, 20, 21,
564
+            29, 12, 28, 17,
565
+                1, 15, 23, 26,
566
+                5, 18, 31, 10,
567
+                2, 8, 24, 14,
568
+            32, 27, 3, 9,
569
+            19, 13, 30, 6,
570
+            22, 11, 4, 25
571
+        );
572
+
573
+        // final permutation (FP)
574
+        self::$_fp = array(
575
+            40, 8, 48, 16, 56, 24, 64, 32,
576
+            39, 7, 47, 15, 55, 23, 63, 31,
577
+            38, 6, 46, 14, 54, 22, 62, 30,
578
+            37, 5, 45, 13, 53, 21, 61, 29,
579
+            36, 4, 44, 12, 52, 20, 60, 28,
580
+            35, 3, 43, 11, 51, 19, 59, 27,
581
+            34, 2, 42, 10, 50, 18, 58, 26,
582
+            33, 1, 41, 9, 49, 17, 57, 25
583
+        );
584
+
585
+        // key schedule used in KeyPermutation()
586
+        self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1);
587
+    }
588
+
589
+
590
+    /**
591
+     * Indicates this is a block cipher
592
+     *
593
+     * @return integer Returns Cipher::BLOCK
594
+     */
595
+    public function type()
596
+    {
597
+        return parent::BLOCK;
598
+    }
599 599
 }
600 600
 ?>
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/Cipher.php 1 patch
Braces   +18 added lines, -14 removed lines patch added patch discarded remove patch
@@ -150,8 +150,9 @@  discard block
 block discarded – undo
150 150
      */
151 151
     public function operation($op = 0)
152 152
     {
153
-        if ($op == self::ENCRYPT || $op == self::DECRYPT)
154
-            $this->operation = $op;
153
+        if ($op == self::ENCRYPT || $op == self::DECRYPT) {
154
+                    $this->operation = $op;
155
+        }
155 156
 
156 157
         return $this->operation;
157 158
     }
@@ -164,8 +165,9 @@  discard block
 block discarded – undo
164 165
      */
165 166
     public function name($name = "")
166 167
     {
167
-        if ($name != "")
168
-            $this->cipher_name = $name;
168
+        if ($name != "") {
169
+                    $this->cipher_name = $name;
170
+        }
169 171
 
170 172
         return $this->cipher_name;
171 173
     }
@@ -179,13 +181,15 @@  discard block
 block discarded – undo
179 181
      */
180 182
     public function blockSize($bytes = 0)
181 183
     {
182
-        if ($bytes > 0)
183
-            $this->block_size = $bytes;
184
+        if ($bytes > 0) {
185
+                    $this->block_size = $bytes;
186
+        }
184 187
 
185 188
         // in some cases a blockSize is not set, such as stream ciphers.
186 189
         // so just return 0 for the block size
187
-        if (!isset($this->block_size))
188
-            return 0;
190
+        if (!isset($this->block_size)) {
191
+                    return 0;
192
+        }
189 193
 
190 194
         return $this->block_size;
191 195
     }
@@ -224,10 +228,11 @@  discard block
 block discarded – undo
224 228
             // given, we need to make sure the new key meets the size
225 229
             // requirements. This can be determined from the $this->key_len
226 230
             // member set from the previous key
227
-            if ($this->key_len > 0 && $req_sz == 0)
228
-                $req_sz = $this->key_len;
229
-            else
230
-                $this->key_len = strlen($key);
231
+            if ($this->key_len > 0 && $req_sz == 0) {
232
+                            $req_sz = $this->key_len;
233
+            } else {
234
+                            $this->key_len = strlen($key);
235
+            }
231 236
 
232 237
             if ($req_sz > 0)
233 238
             {
@@ -236,8 +241,7 @@  discard block
 block discarded – undo
236 241
                     // shorten the key length
237 242
                     $key = substr($key, 0, $req_sz);
238 243
                     $this->key_len = $req_sz;
239
-                }
240
-                else if ($this->key_len < $req_sz)
244
+                } else if ($this->key_len < $req_sz)
241 245
                 {
242 246
                     // send a notice that the key was too small
243 247
                     // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!!
Please login to merge, or discard this patch.
includes/libraries/phpcrypt/Mode.php 1 patch
Braces   +12 added lines, -8 removed lines patch added patch discarded remove patch
@@ -163,8 +163,9 @@  discard block
 block discarded – undo
163 163
     public function createIV($src = PHP_Crypt::RAND)
164 164
     {
165 165
         // if the mode does not use an IV, lets not waste time
166
-        if (!$this->requiresIV())
167
-            return false;
166
+        if (!$this->requiresIV()) {
167
+                    return false;
168
+        }
168 169
 
169 170
         $iv = Core::randBytes($src, $this->block_size);
170 171
         return $this->IV($iv);
@@ -227,8 +228,9 @@  discard block
 block discarded – undo
227 228
      */
228 229
     public function name($name = "")
229 230
     {
230
-        if ($name != "")
231
-            $this->mode_name = $name;
231
+        if ($name != "") {
232
+                    $this->mode_name = $name;
233
+        }
232 234
 
233 235
         return $this->mode_name;
234 236
     }
@@ -244,8 +246,9 @@  discard block
 block discarded – undo
244 246
      */
245 247
     public function padding($type = "")
246 248
     {
247
-        if ($type != "")
248
-            $this->padding = $type;
249
+        if ($type != "") {
250
+                    $this->padding = $type;
251
+        }
249 252
 
250 253
         return $this->padding;
251 254
     }
@@ -263,8 +266,9 @@  discard block
 block discarded – undo
263 266
      */
264 267
     public function cipher($cipher = null)
265 268
     {
266
-        if (is_object($cipher))
267
-            $this->cipher = $cipher;
269
+        if (is_object($cipher)) {
270
+                    $this->cipher = $cipher;
271
+        }
268 272
 
269 273
         return $this->cipher;
270 274
     }
Please login to merge, or discard this patch.