@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | /** |
238 | 238 | * CAST-256 F2 function |
239 | 239 | * |
240 | - * @param $d integer The the data input |
|
240 | + * @param integer $d integer The the data input |
|
241 | 241 | * @param $m integer The 32 bit masking key |
242 | 242 | * @param $r integer The round number |
243 | 243 | * @return integer The value after the F2 calculation |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | /** |
259 | 259 | * CAST-256 F3 function |
260 | 260 | * |
261 | - * @param $d integer The the data input |
|
261 | + * @param integer $d integer The the data input |
|
262 | 262 | * @param $m integer The 32 bit masking key |
263 | 263 | * @param $r integer The round number |
264 | 264 | * @return integer The value after the F3 calculation |
@@ -38,619 +38,619 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_CAST_256 extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 16; // 128 bits; |
|
43 | - |
|
44 | - //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | - |
|
46 | - /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | - const BYTES_KEY_MAX = 32; |
|
48 | - |
|
49 | - /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | - private static $_s1 = array(); |
|
51 | - |
|
52 | - /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | - private static $_s2 = array(); |
|
54 | - |
|
55 | - /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | - private static $_s3 = array(); |
|
57 | - |
|
58 | - /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | - private static $_s4 = array(); |
|
60 | - |
|
61 | - private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | - |
|
63 | - /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | - private $_mkey = array(); |
|
65 | - |
|
66 | - /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | - private $_rkey = array(); |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Constructor |
|
72 | - * |
|
73 | - * @param string $key The key used for Encryption/Decryption |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function __construct($key) |
|
77 | - { |
|
78 | - $keylen = strlen($key); |
|
79 | - |
|
80 | - if($keylen > self::BYTES_KEY_MAX) |
|
81 | - { |
|
82 | - $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | - $keylen = self::BYTES_KEY_MAX; |
|
84 | - } |
|
85 | - else if(!in_array($keylen, self::$_req_key_sizes)) |
|
86 | - { |
|
87 | - $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | - $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | - trigger_error($msg, E_USER_WARNING); |
|
90 | - } |
|
91 | - |
|
92 | - // set the key, make sure the required length is set in bytes |
|
93 | - parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | - |
|
95 | - // set the block size |
|
96 | - $this->blockSize(self::BYTES_BLOCK); |
|
97 | - |
|
98 | - // initialize the sboxes constants |
|
99 | - $this->initTables(); |
|
100 | - |
|
101 | - // create the sub keys using the sboxes |
|
102 | - $this->createSubKeys(); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Destructor |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function __destruct() |
|
112 | - { |
|
113 | - parent::__destruct(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Encrypt plain text data |
|
119 | - * |
|
120 | - * @param string $data A 128 bit block of plain data |
|
121 | - * @return boolean Returns true |
|
122 | - */ |
|
123 | - public function encrypt(&$data) |
|
124 | - { |
|
125 | - $this->operation(parent::ENCRYPT); |
|
126 | - |
|
127 | - // first split the data into four 32 bit blocks, reverse |
|
128 | - // the string order of each block, convert the blocks of data to integers |
|
129 | - $data = str_split($data, 4); |
|
130 | - $data = array_map("strrev", $data); |
|
131 | - $data = array_map("parent::str2Dec", $data); |
|
132 | - |
|
133 | - // do the first 6 loops |
|
134 | - for($i = 0; $i < 6; ++$i) |
|
135 | - { |
|
136 | - |
|
137 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | - } |
|
142 | - |
|
143 | - // the second 6 loops are done in a different order |
|
144 | - for($i = 6; $i < 12; ++$i) |
|
145 | - { |
|
146 | - |
|
147 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | - } |
|
152 | - |
|
153 | - // convert the decimals back to a string, reverse the string so it's |
|
154 | - // in the correct order |
|
155 | - $data = array_map(function($v) { |
|
156 | - $v = Core::dec2Str($v, 4); |
|
157 | - return strrev($v); |
|
158 | - }, $data); |
|
159 | - |
|
160 | - // glue the string back together |
|
161 | - $data = implode("", $data); |
|
162 | - |
|
163 | - return true; |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | - * but in reverse. |
|
170 | - * |
|
171 | - * @param string $data A 128 bit block of encrypted data |
|
172 | - * @return boolean Returns true |
|
173 | - */ |
|
174 | - public function decrypt(&$data) |
|
175 | - { |
|
176 | - $this->operation(parent::DECRYPT); |
|
177 | - |
|
178 | - // first split the data into four 32 bit blocks, reverse |
|
179 | - // the string order of each block, convert the blocks of data to integers |
|
180 | - $data = str_split($data, 4); |
|
181 | - $data = array_map("strrev", $data); |
|
182 | - $data = array_map("parent::str2Dec", $data); |
|
183 | - |
|
184 | - // do the first 6 loops |
|
185 | - for($i = 11; $i >= 6; --$i) |
|
186 | - { |
|
187 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | - } |
|
192 | - |
|
193 | - // the second 6 loops are done in a different order |
|
194 | - for($i = 5; $i >= 0; --$i) |
|
195 | - { |
|
196 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | - } |
|
201 | - |
|
202 | - // convert the decimals back to a string, reverse the string so it's |
|
203 | - // in the correct order |
|
204 | - $data = array_map(function($v) { |
|
205 | - $v = Core::dec2Str($v, 4); |
|
206 | - return strrev($v); |
|
207 | - }, $data); |
|
208 | - |
|
209 | - // glue the string back together |
|
210 | - $data = implode("", $data); |
|
211 | - |
|
212 | - return true; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * CAST-256 F1 function |
|
218 | - * |
|
219 | - * @param $d integer The the data input |
|
220 | - * @param $m integer The 32 bit masking key |
|
221 | - * @param $r integer The round number |
|
222 | - * @return integer The value after the F1 calculation |
|
223 | - */ |
|
224 | - private function f1($d, $m, $r) |
|
225 | - { |
|
226 | - $n = parent::uInt32($m + $d); |
|
227 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | - $n = parent::dec2Str($n, 4); |
|
229 | - |
|
230 | - return parent::uInt32( |
|
231 | - ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | - self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * CAST-256 F2 function |
|
239 | - * |
|
240 | - * @param $d integer The the data input |
|
241 | - * @param $m integer The 32 bit masking key |
|
242 | - * @param $r integer The round number |
|
243 | - * @return integer The value after the F2 calculation |
|
244 | - */ |
|
245 | - private function f2($d, $m, $r) |
|
246 | - { |
|
247 | - $n = parent::uInt32($m ^ $d); |
|
248 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | - $n = parent::dec2Str($n, 4); |
|
250 | - |
|
251 | - return parent::uInt32( |
|
252 | - ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | - self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | - ); |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * CAST-256 F3 function |
|
260 | - * |
|
261 | - * @param $d integer The the data input |
|
262 | - * @param $m integer The 32 bit masking key |
|
263 | - * @param $r integer The round number |
|
264 | - * @return integer The value after the F3 calculation |
|
265 | - */ |
|
266 | - private function f3($d, $m, $r) |
|
267 | - { |
|
268 | - $n = parent::uInt32($m - $d); |
|
269 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | - $n = parent::dec2Str($n, 4); |
|
271 | - |
|
272 | - return parent::uInt32( |
|
273 | - ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | - self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | - ); |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * Creates the subkeys $_mkey (the masking key) and |
|
281 | - * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | - * created from the original key. The original key is null |
|
283 | - * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | - * split in half to create $_mkey and $_rkey |
|
285 | - * |
|
286 | - * @return void |
|
287 | - */ |
|
288 | - private function createSubKeys() |
|
289 | - { |
|
290 | - $cm = 0x5A827999; |
|
291 | - $mm = 0x6ED9EBA1; |
|
292 | - $cr = 19; |
|
293 | - $mr = 17; |
|
294 | - $tm = array(); |
|
295 | - $tr = array(); |
|
296 | - $xkey = $this->key(); |
|
297 | - $tmpkey = array(); |
|
298 | - |
|
299 | - // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | - // for the key expansion |
|
301 | - if($this->keySize() < 32) |
|
302 | - $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | - |
|
304 | - // split the key up into 4 byte parts, reverse the string, |
|
305 | - // then convert each part into a 32 bit integer |
|
306 | - $xkey = str_split($xkey, 4); |
|
307 | - $xkey = array_map("strrev", $xkey); |
|
308 | - $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | - |
|
310 | - // set up the values need for creating round and masking keys |
|
311 | - for($i = 0; $i < 24; ++$i) |
|
312 | - { |
|
313 | - $tm[$i] = array(); |
|
314 | - $tr[$i] = array(); |
|
315 | - |
|
316 | - for($j = 0; $j < 8; ++$j) |
|
317 | - { |
|
318 | - $tm[$i][$j] = $cm; |
|
319 | - $cm = parent::uInt32($cm + $mm); |
|
320 | - $tr[$i][$j] = $cr; |
|
321 | - $cr = parent::uInt32($cr + $mr); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - // now create the round and masking keys |
|
326 | - for($i = 0; $i < 12; ++$i) |
|
327 | - { |
|
328 | - $j = 2 * $i; |
|
329 | - |
|
330 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | - |
|
339 | - $j = (2 * $i) + 1; |
|
340 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | - |
|
349 | - // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | - // to the round key |
|
351 | - $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | - $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | - $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | - $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | - |
|
356 | - // now create 32 byte masking keys |
|
357 | - $this->_mkey[$i][0] = $xkey[7]; |
|
358 | - $this->_mkey[$i][1] = $xkey[5]; |
|
359 | - $this->_mkey[$i][2] = $xkey[3]; |
|
360 | - $this->_mkey[$i][3] = $xkey[1]; |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Initialize the tables |
|
367 | - * |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - private function initTables() |
|
371 | - { |
|
372 | - // 256 unsigned 32 bit integers |
|
373 | - self::$_s1 = array( |
|
374 | - 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | - 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | - 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | - 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | - 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | - 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | - 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | - 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | - 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | - 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | - 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | - 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | - 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | - 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | - 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | - 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | - 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | - 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | - 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | - 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | - 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | - 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | - 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | - 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | - 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | - 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | - 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | - 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | - 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | - 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | - 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | - 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | - 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | - 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | - 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | - 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | - 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | - 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | - 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | - 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | - 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | - 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | - 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | - 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | - 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | - 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | - 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | - 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | - 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | - 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | - 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | - 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | - 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | - 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | - 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | - 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | - 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | - 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | - 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | - 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | - 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | - 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | - 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | - 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | - ); |
|
439 | - |
|
440 | - // 256 unsigned 32 bit integers |
|
441 | - self::$_s2 = array( |
|
442 | - 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | - 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | - 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | - 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | - 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | - 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | - 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | - 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | - 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | - 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | - 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | - 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | - 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | - 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | - 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | - 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | - 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | - 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | - 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | - 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | - 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | - 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | - 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | - 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | - 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | - 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | - 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | - 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | - 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | - 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | - 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | - 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | - 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | - 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | - 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | - 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | - 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | - 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | - 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | - 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | - 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | - 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | - 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | - 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | - 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | - 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | - 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | - 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | - 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | - 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | - 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | - 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | - 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | - 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | - 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | - 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | - 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | - 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | - 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | - 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | - 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | - 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | - 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | - 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | - ); |
|
507 | - |
|
508 | - // 256 unsigned 32 bit integers |
|
509 | - self::$_s3 = array( |
|
510 | - 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | - 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | - 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | - 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | - 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | - 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | - 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | - 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | - 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | - 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | - 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | - 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | - 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | - 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | - 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | - 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | - 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | - 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | - 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | - 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | - 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | - 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | - 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | - 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | - 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | - 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | - 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | - 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | - 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | - 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | - 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | - 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | - 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | - 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | - 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | - 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | - 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | - 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | - 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | - 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | - 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | - 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | - 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | - 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | - 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | - 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | - 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | - 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | - 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | - 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | - 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | - 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | - 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | - 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | - 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | - 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | - 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | - 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | - 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | - 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | - 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | - 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | - 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | - 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | - ); |
|
575 | - |
|
576 | - // 256 unsigned 32 bit integers |
|
577 | - self::$_s4 = array( |
|
578 | - 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | - 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | - 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | - 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | - 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | - 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | - 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | - 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | - 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | - 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | - 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | - 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | - 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | - 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | - 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | - 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | - 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | - 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | - 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | - 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | - 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | - 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | - 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | - 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | - 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | - 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | - 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | - 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | - 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | - 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | - 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | - 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | - 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | - 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | - 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | - 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | - 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | - 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | - 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | - 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | - 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | - 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | - 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | - 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | - 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | - 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | - 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | - 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | - 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | - 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | - 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | - 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | - 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | - 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | - 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | - 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | - 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | - 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | - 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | - 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | - 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | - 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | - 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | - 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * Indicates this is a block cipher |
|
648 | - * |
|
649 | - * @return integer Returns Cipher::BLOCK |
|
650 | - */ |
|
651 | - public function type() |
|
652 | - { |
|
653 | - return parent::BLOCK; |
|
654 | - } |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 16; // 128 bits; |
|
43 | + |
|
44 | + //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | + |
|
46 | + /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | + const BYTES_KEY_MAX = 32; |
|
48 | + |
|
49 | + /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | + private static $_s1 = array(); |
|
51 | + |
|
52 | + /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | + private static $_s2 = array(); |
|
54 | + |
|
55 | + /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | + private static $_s3 = array(); |
|
57 | + |
|
58 | + /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | + private static $_s4 = array(); |
|
60 | + |
|
61 | + private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | + |
|
63 | + /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | + private $_mkey = array(); |
|
65 | + |
|
66 | + /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | + private $_rkey = array(); |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Constructor |
|
72 | + * |
|
73 | + * @param string $key The key used for Encryption/Decryption |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function __construct($key) |
|
77 | + { |
|
78 | + $keylen = strlen($key); |
|
79 | + |
|
80 | + if($keylen > self::BYTES_KEY_MAX) |
|
81 | + { |
|
82 | + $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | + $keylen = self::BYTES_KEY_MAX; |
|
84 | + } |
|
85 | + else if(!in_array($keylen, self::$_req_key_sizes)) |
|
86 | + { |
|
87 | + $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | + $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | + trigger_error($msg, E_USER_WARNING); |
|
90 | + } |
|
91 | + |
|
92 | + // set the key, make sure the required length is set in bytes |
|
93 | + parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | + |
|
95 | + // set the block size |
|
96 | + $this->blockSize(self::BYTES_BLOCK); |
|
97 | + |
|
98 | + // initialize the sboxes constants |
|
99 | + $this->initTables(); |
|
100 | + |
|
101 | + // create the sub keys using the sboxes |
|
102 | + $this->createSubKeys(); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Destructor |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function __destruct() |
|
112 | + { |
|
113 | + parent::__destruct(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Encrypt plain text data |
|
119 | + * |
|
120 | + * @param string $data A 128 bit block of plain data |
|
121 | + * @return boolean Returns true |
|
122 | + */ |
|
123 | + public function encrypt(&$data) |
|
124 | + { |
|
125 | + $this->operation(parent::ENCRYPT); |
|
126 | + |
|
127 | + // first split the data into four 32 bit blocks, reverse |
|
128 | + // the string order of each block, convert the blocks of data to integers |
|
129 | + $data = str_split($data, 4); |
|
130 | + $data = array_map("strrev", $data); |
|
131 | + $data = array_map("parent::str2Dec", $data); |
|
132 | + |
|
133 | + // do the first 6 loops |
|
134 | + for($i = 0; $i < 6; ++$i) |
|
135 | + { |
|
136 | + |
|
137 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | + } |
|
142 | + |
|
143 | + // the second 6 loops are done in a different order |
|
144 | + for($i = 6; $i < 12; ++$i) |
|
145 | + { |
|
146 | + |
|
147 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | + } |
|
152 | + |
|
153 | + // convert the decimals back to a string, reverse the string so it's |
|
154 | + // in the correct order |
|
155 | + $data = array_map(function($v) { |
|
156 | + $v = Core::dec2Str($v, 4); |
|
157 | + return strrev($v); |
|
158 | + }, $data); |
|
159 | + |
|
160 | + // glue the string back together |
|
161 | + $data = implode("", $data); |
|
162 | + |
|
163 | + return true; |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | + * but in reverse. |
|
170 | + * |
|
171 | + * @param string $data A 128 bit block of encrypted data |
|
172 | + * @return boolean Returns true |
|
173 | + */ |
|
174 | + public function decrypt(&$data) |
|
175 | + { |
|
176 | + $this->operation(parent::DECRYPT); |
|
177 | + |
|
178 | + // first split the data into four 32 bit blocks, reverse |
|
179 | + // the string order of each block, convert the blocks of data to integers |
|
180 | + $data = str_split($data, 4); |
|
181 | + $data = array_map("strrev", $data); |
|
182 | + $data = array_map("parent::str2Dec", $data); |
|
183 | + |
|
184 | + // do the first 6 loops |
|
185 | + for($i = 11; $i >= 6; --$i) |
|
186 | + { |
|
187 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | + } |
|
192 | + |
|
193 | + // the second 6 loops are done in a different order |
|
194 | + for($i = 5; $i >= 0; --$i) |
|
195 | + { |
|
196 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | + } |
|
201 | + |
|
202 | + // convert the decimals back to a string, reverse the string so it's |
|
203 | + // in the correct order |
|
204 | + $data = array_map(function($v) { |
|
205 | + $v = Core::dec2Str($v, 4); |
|
206 | + return strrev($v); |
|
207 | + }, $data); |
|
208 | + |
|
209 | + // glue the string back together |
|
210 | + $data = implode("", $data); |
|
211 | + |
|
212 | + return true; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * CAST-256 F1 function |
|
218 | + * |
|
219 | + * @param $d integer The the data input |
|
220 | + * @param $m integer The 32 bit masking key |
|
221 | + * @param $r integer The round number |
|
222 | + * @return integer The value after the F1 calculation |
|
223 | + */ |
|
224 | + private function f1($d, $m, $r) |
|
225 | + { |
|
226 | + $n = parent::uInt32($m + $d); |
|
227 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | + $n = parent::dec2Str($n, 4); |
|
229 | + |
|
230 | + return parent::uInt32( |
|
231 | + ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | + self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * CAST-256 F2 function |
|
239 | + * |
|
240 | + * @param $d integer The the data input |
|
241 | + * @param $m integer The 32 bit masking key |
|
242 | + * @param $r integer The round number |
|
243 | + * @return integer The value after the F2 calculation |
|
244 | + */ |
|
245 | + private function f2($d, $m, $r) |
|
246 | + { |
|
247 | + $n = parent::uInt32($m ^ $d); |
|
248 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | + $n = parent::dec2Str($n, 4); |
|
250 | + |
|
251 | + return parent::uInt32( |
|
252 | + ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | + self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | + ); |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * CAST-256 F3 function |
|
260 | + * |
|
261 | + * @param $d integer The the data input |
|
262 | + * @param $m integer The 32 bit masking key |
|
263 | + * @param $r integer The round number |
|
264 | + * @return integer The value after the F3 calculation |
|
265 | + */ |
|
266 | + private function f3($d, $m, $r) |
|
267 | + { |
|
268 | + $n = parent::uInt32($m - $d); |
|
269 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | + $n = parent::dec2Str($n, 4); |
|
271 | + |
|
272 | + return parent::uInt32( |
|
273 | + ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | + self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | + ); |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * Creates the subkeys $_mkey (the masking key) and |
|
281 | + * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | + * created from the original key. The original key is null |
|
283 | + * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | + * split in half to create $_mkey and $_rkey |
|
285 | + * |
|
286 | + * @return void |
|
287 | + */ |
|
288 | + private function createSubKeys() |
|
289 | + { |
|
290 | + $cm = 0x5A827999; |
|
291 | + $mm = 0x6ED9EBA1; |
|
292 | + $cr = 19; |
|
293 | + $mr = 17; |
|
294 | + $tm = array(); |
|
295 | + $tr = array(); |
|
296 | + $xkey = $this->key(); |
|
297 | + $tmpkey = array(); |
|
298 | + |
|
299 | + // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | + // for the key expansion |
|
301 | + if($this->keySize() < 32) |
|
302 | + $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | + |
|
304 | + // split the key up into 4 byte parts, reverse the string, |
|
305 | + // then convert each part into a 32 bit integer |
|
306 | + $xkey = str_split($xkey, 4); |
|
307 | + $xkey = array_map("strrev", $xkey); |
|
308 | + $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | + |
|
310 | + // set up the values need for creating round and masking keys |
|
311 | + for($i = 0; $i < 24; ++$i) |
|
312 | + { |
|
313 | + $tm[$i] = array(); |
|
314 | + $tr[$i] = array(); |
|
315 | + |
|
316 | + for($j = 0; $j < 8; ++$j) |
|
317 | + { |
|
318 | + $tm[$i][$j] = $cm; |
|
319 | + $cm = parent::uInt32($cm + $mm); |
|
320 | + $tr[$i][$j] = $cr; |
|
321 | + $cr = parent::uInt32($cr + $mr); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + // now create the round and masking keys |
|
326 | + for($i = 0; $i < 12; ++$i) |
|
327 | + { |
|
328 | + $j = 2 * $i; |
|
329 | + |
|
330 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | + |
|
339 | + $j = (2 * $i) + 1; |
|
340 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | + |
|
349 | + // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | + // to the round key |
|
351 | + $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | + $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | + $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | + $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | + |
|
356 | + // now create 32 byte masking keys |
|
357 | + $this->_mkey[$i][0] = $xkey[7]; |
|
358 | + $this->_mkey[$i][1] = $xkey[5]; |
|
359 | + $this->_mkey[$i][2] = $xkey[3]; |
|
360 | + $this->_mkey[$i][3] = $xkey[1]; |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Initialize the tables |
|
367 | + * |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + private function initTables() |
|
371 | + { |
|
372 | + // 256 unsigned 32 bit integers |
|
373 | + self::$_s1 = array( |
|
374 | + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | + ); |
|
439 | + |
|
440 | + // 256 unsigned 32 bit integers |
|
441 | + self::$_s2 = array( |
|
442 | + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | + ); |
|
507 | + |
|
508 | + // 256 unsigned 32 bit integers |
|
509 | + self::$_s3 = array( |
|
510 | + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | + ); |
|
575 | + |
|
576 | + // 256 unsigned 32 bit integers |
|
577 | + self::$_s4 = array( |
|
578 | + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | + ); |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + /** |
|
647 | + * Indicates this is a block cipher |
|
648 | + * |
|
649 | + * @return integer Returns Cipher::BLOCK |
|
650 | + */ |
|
651 | + public function type() |
|
652 | + { |
|
653 | + return parent::BLOCK; |
|
654 | + } |
|
655 | 655 | } |
656 | 656 | ?> |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | { |
78 | 78 | $keylen = strlen($key); |
79 | 79 | |
80 | - if($keylen > self::BYTES_KEY_MAX) |
|
80 | + if ($keylen > self::BYTES_KEY_MAX) |
|
81 | 81 | { |
82 | 82 | $key = substr($key, 0, self::BYTES_KEY_MAX); |
83 | 83 | $keylen = self::BYTES_KEY_MAX; |
84 | 84 | } |
85 | - else if(!in_array($keylen, self::$_req_key_sizes)) |
|
85 | + else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | 86 | { |
87 | 87 | $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
88 | 88 | $msg .= "20, 24, 28, or 32 bytes."; |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $data = array_map("parent::str2Dec", $data); |
132 | 132 | |
133 | 133 | // do the first 6 loops |
134 | - for($i = 0; $i < 6; ++$i) |
|
134 | + for ($i = 0; $i < 6; ++$i) |
|
135 | 135 | { |
136 | 136 | |
137 | 137 | $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | } |
142 | 142 | |
143 | 143 | // the second 6 loops are done in a different order |
144 | - for($i = 6; $i < 12; ++$i) |
|
144 | + for ($i = 6; $i < 12; ++$i) |
|
145 | 145 | { |
146 | 146 | |
147 | 147 | $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $data = array_map("parent::str2Dec", $data); |
183 | 183 | |
184 | 184 | // do the first 6 loops |
185 | - for($i = 11; $i >= 6; --$i) |
|
185 | + for ($i = 11; $i >= 6; --$i) |
|
186 | 186 | { |
187 | 187 | $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
188 | 188 | $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | } |
192 | 192 | |
193 | 193 | // the second 6 loops are done in a different order |
194 | - for($i = 5; $i >= 0; --$i) |
|
194 | + for ($i = 5; $i >= 0; --$i) |
|
195 | 195 | { |
196 | 196 | $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
197 | 197 | $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | |
299 | 299 | // if the key is less than 32 bytes, pad it to 32 bytes |
300 | 300 | // for the key expansion |
301 | - if($this->keySize() < 32) |
|
301 | + if ($this->keySize() < 32) |
|
302 | 302 | $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
303 | 303 | |
304 | 304 | // split the key up into 4 byte parts, reverse the string, |
@@ -308,12 +308,12 @@ discard block |
||
308 | 308 | $xkey = array_map("parent::str2Dec", $xkey); |
309 | 309 | |
310 | 310 | // set up the values need for creating round and masking keys |
311 | - for($i = 0; $i < 24; ++$i) |
|
311 | + for ($i = 0; $i < 24; ++$i) |
|
312 | 312 | { |
313 | 313 | $tm[$i] = array(); |
314 | 314 | $tr[$i] = array(); |
315 | 315 | |
316 | - for($j = 0; $j < 8; ++$j) |
|
316 | + for ($j = 0; $j < 8; ++$j) |
|
317 | 317 | { |
318 | 318 | $tm[$i][$j] = $cm; |
319 | 319 | $cm = parent::uInt32($cm + $mm); |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | } |
324 | 324 | |
325 | 325 | // now create the round and masking keys |
326 | - for($i = 0; $i < 12; ++$i) |
|
326 | + for ($i = 0; $i < 12; ++$i) |
|
327 | 327 | { |
328 | 328 | $j = 2 * $i; |
329 | 329 | |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
337 | 337 | $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
338 | 338 | |
339 | - $j = (2 * $i) + 1; |
|
339 | + $j = (2 * $i) + 1; |
|
340 | 340 | $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
341 | 341 | $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
342 | 342 | $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
@@ -81,8 +81,7 @@ discard block |
||
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 |
||
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 |
@@ -155,7 +155,6 @@ discard block |
||
155 | 155 | /** |
156 | 156 | * Encrypt plain text data using DES |
157 | 157 | * |
158 | - * @param string $data A plain text string |
|
159 | 158 | * @return boolean Returns true |
160 | 159 | */ |
161 | 160 | public function encrypt(&$text) |
@@ -168,7 +167,6 @@ discard block |
||
168 | 167 | /** |
169 | 168 | * Decrypt a DES encrypted string |
170 | 169 | * |
171 | - * @param string $encrypted A DES encrypted string |
|
172 | 170 | * @return boolean Returns true |
173 | 171 | */ |
174 | 172 | public function decrypt(&$text) |
@@ -296,8 +294,8 @@ discard block |
||
296 | 294 | * the IP and FP are part of the DES standard and not implementing it would deviate from |
297 | 295 | * the standard, so we will do it here in phpCrypt. |
298 | 296 | * |
299 | - * @param string $m The plain text message |
|
300 | - * @return array the Initial Permutation (IP) |
|
297 | + * @param string $text |
|
298 | + * @return string the Initial Permutation (IP) |
|
301 | 299 | */ |
302 | 300 | private function ip($text) |
303 | 301 | { |
@@ -318,7 +316,7 @@ discard block |
||
318 | 316 | * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
319 | 317 | * block, and a 48 bit output block. |
320 | 318 | * |
321 | - * @param array $r 32 bit binary, each bit in an array element |
|
319 | + * @param string $r 32 bit binary, each bit in an array element |
|
322 | 320 | * @param string $k 48 bit binary string |
323 | 321 | * @return string 48 bit binary string |
324 | 322 | */ |
@@ -38,558 +38,558 @@ |
||
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 | - * @param string $data A plain text string |
|
159 | - * @return boolean Returns true |
|
160 | - */ |
|
161 | - public function encrypt(&$text) |
|
162 | - { |
|
163 | - $this->operation(parent::ENCRYPT); |
|
164 | - return $this->des($text); |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * Decrypt a DES encrypted string |
|
170 | - * |
|
171 | - * @param string $encrypted A DES encrypted string |
|
172 | - * @return boolean Returns true |
|
173 | - */ |
|
174 | - public function decrypt(&$text) |
|
175 | - { |
|
176 | - $this->operation(parent::DECRYPT); |
|
177 | - return $this->des($text); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * This is where the actual encrypt/decryption takes place. Since |
|
183 | - * encryption and decryption are the same algorithm in DES, we only |
|
184 | - * need one function to do both. |
|
185 | - * |
|
186 | - * @param string $data The string to be encrypted or decrypted |
|
187 | - * @return boolean Returns true |
|
188 | - */ |
|
189 | - protected function des(&$data) |
|
190 | - { |
|
191 | - $l = array(); |
|
192 | - $r = array(); |
|
193 | - |
|
194 | - // step two: Initial Permutation (IP) of plaintext |
|
195 | - $data = $this->ip($data); |
|
196 | - |
|
197 | - // divide the permuted block IP into a left half L0 of 32 bits, |
|
198 | - // and a right half R0 of 32 bits |
|
199 | - $l[0] = substr($data, 0, 32); |
|
200 | - $r[0] = substr($data, 32, 32); |
|
201 | - |
|
202 | - for($n = 1; $n <= 16; ++$n) |
|
203 | - { |
|
204 | - $l[$n] = $r[$n-1]; |
|
205 | - |
|
206 | - if($this->operation() == parent::DECRYPT) |
|
207 | - $f = $this->F($r[$n-1], $this->sub_keys[16-$n]); |
|
208 | - else |
|
209 | - $f = $this->F($r[$n-1], $this->sub_keys[$n-1]); |
|
210 | - |
|
211 | - // XOR F with Ln |
|
212 | - $r[$n] = $this->xorBin($l[$n-1], $f); |
|
213 | - } |
|
214 | - |
|
215 | - // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
216 | - // L[16] and R[16] so that it becomes R[16]L[16] |
|
217 | - $data = $r[16].$l[16]; |
|
218 | - |
|
219 | - // now do the final permutation |
|
220 | - $data = $this->fp($data); |
|
221 | - $data = parent::bin2Str($data); |
|
222 | - |
|
223 | - return true; |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * The Key permutation, based on tables $_pc1 and $_pc2 |
|
229 | - * Create 16 subkeys, each of which is 48-bits long. |
|
230 | - * |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - private function keyPermutation() |
|
234 | - { |
|
235 | - $this->sub_keys = array(); |
|
236 | - $pc1m = array(); |
|
237 | - $pcr = array(); |
|
238 | - $c = array(); |
|
239 | - $d = array(); |
|
240 | - |
|
241 | - // convert the key to binary |
|
242 | - $binkey = parent::str2Bin($this->key()); |
|
243 | - |
|
244 | - // reduce the key down to 56bits based on table $_pc1 |
|
245 | - for($i = 0; $i < 56; ++$i) |
|
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 | + * @param string $data A plain text string |
|
159 | + * @return boolean Returns true |
|
160 | + */ |
|
161 | + public function encrypt(&$text) |
|
162 | + { |
|
163 | + $this->operation(parent::ENCRYPT); |
|
164 | + return $this->des($text); |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * Decrypt a DES encrypted string |
|
170 | + * |
|
171 | + * @param string $encrypted A DES encrypted string |
|
172 | + * @return boolean Returns true |
|
173 | + */ |
|
174 | + public function decrypt(&$text) |
|
175 | + { |
|
176 | + $this->operation(parent::DECRYPT); |
|
177 | + return $this->des($text); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * This is where the actual encrypt/decryption takes place. Since |
|
183 | + * encryption and decryption are the same algorithm in DES, we only |
|
184 | + * need one function to do both. |
|
185 | + * |
|
186 | + * @param string $data The string to be encrypted or decrypted |
|
187 | + * @return boolean Returns true |
|
188 | + */ |
|
189 | + protected function des(&$data) |
|
190 | + { |
|
191 | + $l = array(); |
|
192 | + $r = array(); |
|
193 | + |
|
194 | + // step two: Initial Permutation (IP) of plaintext |
|
195 | + $data = $this->ip($data); |
|
196 | + |
|
197 | + // divide the permuted block IP into a left half L0 of 32 bits, |
|
198 | + // and a right half R0 of 32 bits |
|
199 | + $l[0] = substr($data, 0, 32); |
|
200 | + $r[0] = substr($data, 32, 32); |
|
201 | + |
|
202 | + for($n = 1; $n <= 16; ++$n) |
|
203 | + { |
|
204 | + $l[$n] = $r[$n-1]; |
|
205 | + |
|
206 | + if($this->operation() == parent::DECRYPT) |
|
207 | + $f = $this->F($r[$n-1], $this->sub_keys[16-$n]); |
|
208 | + else |
|
209 | + $f = $this->F($r[$n-1], $this->sub_keys[$n-1]); |
|
210 | + |
|
211 | + // XOR F with Ln |
|
212 | + $r[$n] = $this->xorBin($l[$n-1], $f); |
|
213 | + } |
|
214 | + |
|
215 | + // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
216 | + // L[16] and R[16] so that it becomes R[16]L[16] |
|
217 | + $data = $r[16].$l[16]; |
|
218 | + |
|
219 | + // now do the final permutation |
|
220 | + $data = $this->fp($data); |
|
221 | + $data = parent::bin2Str($data); |
|
222 | + |
|
223 | + return true; |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * The Key permutation, based on tables $_pc1 and $_pc2 |
|
229 | + * Create 16 subkeys, each of which is 48-bits long. |
|
230 | + * |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + private function keyPermutation() |
|
234 | + { |
|
235 | + $this->sub_keys = array(); |
|
236 | + $pc1m = array(); |
|
237 | + $pcr = array(); |
|
238 | + $c = array(); |
|
239 | + $d = array(); |
|
240 | + |
|
241 | + // convert the key to binary |
|
242 | + $binkey = parent::str2Bin($this->key()); |
|
243 | + |
|
244 | + // reduce the key down to 56bits based on table $_pc1 |
|
245 | + for($i = 0; $i < 56; ++$i) |
|
246 | 246 | $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
247 | 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 | - // the sub_keys are created, we are done with the key permutation |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * Initial Permutation (IP) |
|
285 | - * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
286 | - * the 64 bits of the message data M. This rearranges the bits according to the |
|
287 | - * following table, where the entries in the table show the new arrangement of the |
|
288 | - * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
289 | - * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
290 | - * bit of IP. |
|
291 | - * |
|
292 | - * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
293 | - * The initial permution was used to make it easier to load plain text and cipher text |
|
294 | - * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
295 | - * are not necessary in software implementations and do not affect the security. However, |
|
296 | - * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
297 | - * the standard, so we will do it here in phpCrypt. |
|
298 | - * |
|
299 | - * @param string $m The plain text message |
|
300 | - * @return array the Initial Permutation (IP) |
|
301 | - */ |
|
302 | - private function ip($text) |
|
303 | - { |
|
304 | - $text = parent::str2Bin($text); |
|
305 | - $ip = ""; |
|
306 | - |
|
307 | - // loop through the 64 bit block, ordering it occording to $_ip |
|
308 | - for($i = 0; $i < 64; ++$i) |
|
309 | - $ip .= $text[self::$_ip[$i] - 1]; |
|
310 | - |
|
311 | - return $ip; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
317 | - * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
318 | - * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
319 | - * block, and a 48 bit output block. |
|
320 | - * |
|
321 | - * @param array $r 32 bit binary, each bit in an array element |
|
322 | - * @param string $k 48 bit binary string |
|
323 | - * @return string 48 bit binary string |
|
324 | - */ |
|
325 | - private function f($r, $k) |
|
326 | - { |
|
327 | - $bin = parent::xorBin($k, $this->E($r)); |
|
328 | - |
|
329 | - // create a 32-bit string from $bits by passing it through the S-Boxes |
|
330 | - $bin = $this->s($bin); |
|
331 | - |
|
332 | - // now send permute $bin as defined by table self::$_p |
|
333 | - $bin = $this->p($bin); |
|
334 | - |
|
335 | - return $bin; |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
341 | - * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
342 | - * to the self::$_e[] table. |
|
343 | - * This is only used in the F() function |
|
344 | - * |
|
345 | - * @param array $r 32 bit binary, each bit in an array element |
|
346 | - * @return string 48 bit binary string |
|
347 | - */ |
|
348 | - private function e($r) |
|
349 | - { |
|
350 | - $e = ""; |
|
351 | - for($i = 0; $i < 48; ++$i) |
|
352 | - $e .= $r[self::$_e[$i] - 1]; |
|
353 | - |
|
354 | - return $e; |
|
355 | - } |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * S-Box |
|
360 | - * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
361 | - * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
362 | - * through the S-Boxes |
|
363 | - * |
|
364 | - * @param string $bits The 48-bit string from F() to be processed |
|
365 | - * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
366 | - */ |
|
367 | - private function s($bits) |
|
368 | - { |
|
369 | - $s = ""; |
|
370 | - |
|
371 | - for($i = 0; $i <= 42; $i += 6) |
|
372 | - { |
|
373 | - $sbits = substr($bits, $i, 6); |
|
374 | - |
|
375 | - // we need to determine the S-Box column number and row number |
|
376 | - // from the 6 bit string passed in, this is done using the following method: |
|
377 | - // The First & Last bits represent a number between 0-3, used to determine which row |
|
378 | - // The middle 4 bits represent a number between 0-15, used to determine the column |
|
379 | - $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
380 | - $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
381 | - |
|
382 | - // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
383 | - $pos = ($row * 16) + $col; |
|
384 | - |
|
385 | - // get the integer from the S-Box and convert it to binary |
|
386 | - $bin = decbin(self::$_s[($i/6)][$pos]); |
|
387 | - $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
388 | - } |
|
389 | - |
|
390 | - return $s; |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - /** |
|
395 | - * Permutation P |
|
396 | - * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
397 | - * from a 32-bit input from a binary string from the S-BOX by permuting |
|
398 | - * the bits of the input block. |
|
399 | - * This is only used inside of F() function |
|
400 | - * |
|
401 | - * @param string $s A 32-bit string originating from being passed through S-Box |
|
402 | - * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
403 | - */ |
|
404 | - private function p($s) |
|
405 | - { |
|
406 | - $p = ""; |
|
407 | - for($i = 0; $i < 32; ++$i) |
|
408 | - $p .= $s[self::$_p[$i] - 1]; |
|
409 | - |
|
410 | - return $p; |
|
411 | - } |
|
412 | - |
|
413 | - |
|
414 | - /** |
|
415 | - * Final Permutation (FP) |
|
416 | - * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
417 | - * we will do it to follow the DES standard). |
|
418 | - * |
|
419 | - * @param string $bin A 64-bit binary string |
|
420 | - * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
421 | - */ |
|
422 | - private function fp($bin) |
|
423 | - { |
|
424 | - $fp = ""; |
|
425 | - for($i = 0; $i < 64; ++$i) |
|
426 | - $fp .= $bin[self::$_fp[$i] - 1]; |
|
427 | - |
|
428 | - return $fp; |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * Initialize all the tables, this function is called inside the constructor |
|
434 | - * |
|
435 | - * @return void |
|
436 | - */ |
|
437 | - private function initTables() |
|
438 | - { |
|
439 | - // permuted choice 1 (PC1) |
|
440 | - // these values are chars and should be run through chr() when used |
|
441 | - self::$_pc1 = array( |
|
442 | - 57, 49, 41, 33, 25, 17, 9, |
|
443 | - 1, 58, 50, 42, 34, 26, 18, |
|
444 | - 10, 2, 59, 51, 43, 35, 27, |
|
445 | - 19, 11, 3, 60, 52, 44, 36, |
|
446 | - 63, 55, 47, 39, 31, 23, 15, |
|
447 | - 7, 62, 54, 46, 38, 30, 22, |
|
448 | - 14, 6, 61, 53, 45, 37, 29, |
|
449 | - 21, 13, 5, 28, 20, 12, 4 |
|
450 | - ); |
|
451 | - |
|
452 | - // permuted choice 2 (PC2) |
|
453 | - // these values are chars and should be run through chr() when used |
|
454 | - self::$_pc2 = array( |
|
455 | - 14, 17, 11, 24, 1, 5, |
|
456 | - 3, 28, 15, 6, 21, 10, |
|
457 | - 23, 19, 12, 4, 26, 8, |
|
458 | - 16, 7, 27, 20, 13, 2, |
|
459 | - 41, 52, 31, 37, 47, 55, |
|
460 | - 30, 40, 51, 45, 33, 48, |
|
461 | - 44, 49, 39, 56, 34, 53, |
|
462 | - 46, 42, 50, 36, 29, 32 |
|
463 | - ); |
|
464 | - |
|
465 | - // initial permutation (IP) |
|
466 | - self::$_ip = array( |
|
467 | - 58, 50, 42, 34, 26, 18, 10, 2, |
|
468 | - 60, 52, 44, 36, 28, 20, 12, 4, |
|
469 | - 62, 54, 46, 38, 30, 22, 14, 6, |
|
470 | - 64, 56, 48, 40, 32, 24, 16, 8, |
|
471 | - 57, 49, 41, 33, 25, 17, 9, 1, |
|
472 | - 59, 51, 43, 35, 27, 19, 11, 3, |
|
473 | - 61, 53, 45, 37, 29, 21, 13, 5, |
|
474 | - 63, 55, 47, 39, 31, 23, 15, 7 |
|
475 | - ); |
|
476 | - |
|
477 | - // expansion (E) |
|
478 | - self::$_e = array( |
|
479 | - 32, 1, 2, 3, 4, 5, |
|
480 | - 4, 5, 6, 7, 8, 9, |
|
481 | - 8, 9, 10, 11, 12, 13, |
|
482 | - 12, 13, 14, 15, 16, 17, |
|
483 | - 16, 17, 18, 19, 20, 21, |
|
484 | - 20, 21, 22, 23, 24, 25, |
|
485 | - 24, 25, 26, 27, 28, 29, |
|
486 | - 28, 29, 30, 31, 32, 1 |
|
487 | - ); |
|
488 | - |
|
489 | - // substition box (S) |
|
490 | - self::$_s = array( |
|
491 | - /* S1 */ |
|
492 | - array( |
|
493 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
494 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
495 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
496 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
497 | - ), |
|
498 | - |
|
499 | - /* S2 */ |
|
500 | - array( |
|
501 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
502 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
503 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
504 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
505 | - ), |
|
506 | - |
|
507 | - /* S3 */ |
|
508 | - array( |
|
509 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
510 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
511 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
512 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
513 | - ), |
|
514 | - |
|
515 | - /* S4 */ |
|
516 | - array( |
|
517 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
518 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
519 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
520 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
521 | - ), |
|
522 | - |
|
523 | - /* S5 */ |
|
524 | - array( |
|
525 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
526 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
527 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
528 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
529 | - ), |
|
530 | - |
|
531 | - /* S6 */ |
|
532 | - array( |
|
533 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
534 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
535 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
536 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
537 | - ), |
|
538 | - |
|
539 | - /* S7 */ |
|
540 | - array( |
|
541 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
542 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
543 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
544 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
545 | - ), |
|
546 | - |
|
547 | - /* S8 */ |
|
548 | - array( |
|
549 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
550 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
551 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
552 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
553 | - ) |
|
554 | - ); |
|
555 | - |
|
556 | - // permutation (P) |
|
557 | - self::$_p = array( |
|
558 | - 16, 7, 20, 21, |
|
559 | - 29, 12, 28, 17, |
|
560 | - 1, 15, 23, 26, |
|
561 | - 5, 18, 31, 10, |
|
562 | - 2, 8, 24, 14, |
|
563 | - 32, 27, 3, 9, |
|
564 | - 19, 13, 30, 6, |
|
565 | - 22, 11, 4, 25 |
|
566 | - ); |
|
567 | - |
|
568 | - // final permutation (FP) |
|
569 | - self::$_fp = array( |
|
570 | - 40, 8, 48, 16, 56, 24, 64, 32, |
|
571 | - 39, 7, 47, 15, 55, 23, 63, 31, |
|
572 | - 38, 6, 46, 14, 54, 22, 62, 30, |
|
573 | - 37, 5, 45, 13, 53, 21, 61, 29, |
|
574 | - 36, 4, 44, 12, 52, 20, 60, 28, |
|
575 | - 35, 3, 43, 11, 51, 19, 59, 27, |
|
576 | - 34, 2, 42, 10, 50, 18, 58, 26, |
|
577 | - 33, 1, 41, 9, 49, 17, 57, 25 |
|
578 | - ); |
|
579 | - |
|
580 | - // key schedule used in KeyPermutation() |
|
581 | - self::$_key_sched = array(1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1); |
|
582 | - } |
|
583 | - |
|
584 | - |
|
585 | - /** |
|
586 | - * Indicates this is a block cipher |
|
587 | - * |
|
588 | - * @return integer Returns Cipher::BLOCK |
|
589 | - */ |
|
590 | - public function type() |
|
591 | - { |
|
592 | - return parent::BLOCK; |
|
593 | - } |
|
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 | + // the sub_keys are created, we are done with the key permutation |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * Initial Permutation (IP) |
|
285 | + * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
286 | + * the 64 bits of the message data M. This rearranges the bits according to the |
|
287 | + * following table, where the entries in the table show the new arrangement of the |
|
288 | + * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
289 | + * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
290 | + * bit of IP. |
|
291 | + * |
|
292 | + * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
293 | + * The initial permution was used to make it easier to load plain text and cipher text |
|
294 | + * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
295 | + * are not necessary in software implementations and do not affect the security. However, |
|
296 | + * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
297 | + * the standard, so we will do it here in phpCrypt. |
|
298 | + * |
|
299 | + * @param string $m The plain text message |
|
300 | + * @return array the Initial Permutation (IP) |
|
301 | + */ |
|
302 | + private function ip($text) |
|
303 | + { |
|
304 | + $text = parent::str2Bin($text); |
|
305 | + $ip = ""; |
|
306 | + |
|
307 | + // loop through the 64 bit block, ordering it occording to $_ip |
|
308 | + for($i = 0; $i < 64; ++$i) |
|
309 | + $ip .= $text[self::$_ip[$i] - 1]; |
|
310 | + |
|
311 | + return $ip; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
317 | + * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
318 | + * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
319 | + * block, and a 48 bit output block. |
|
320 | + * |
|
321 | + * @param array $r 32 bit binary, each bit in an array element |
|
322 | + * @param string $k 48 bit binary string |
|
323 | + * @return string 48 bit binary string |
|
324 | + */ |
|
325 | + private function f($r, $k) |
|
326 | + { |
|
327 | + $bin = parent::xorBin($k, $this->E($r)); |
|
328 | + |
|
329 | + // create a 32-bit string from $bits by passing it through the S-Boxes |
|
330 | + $bin = $this->s($bin); |
|
331 | + |
|
332 | + // now send permute $bin as defined by table self::$_p |
|
333 | + $bin = $this->p($bin); |
|
334 | + |
|
335 | + return $bin; |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
341 | + * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
342 | + * to the self::$_e[] table. |
|
343 | + * This is only used in the F() function |
|
344 | + * |
|
345 | + * @param array $r 32 bit binary, each bit in an array element |
|
346 | + * @return string 48 bit binary string |
|
347 | + */ |
|
348 | + private function e($r) |
|
349 | + { |
|
350 | + $e = ""; |
|
351 | + for($i = 0; $i < 48; ++$i) |
|
352 | + $e .= $r[self::$_e[$i] - 1]; |
|
353 | + |
|
354 | + return $e; |
|
355 | + } |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * S-Box |
|
360 | + * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
361 | + * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
362 | + * through the S-Boxes |
|
363 | + * |
|
364 | + * @param string $bits The 48-bit string from F() to be processed |
|
365 | + * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
366 | + */ |
|
367 | + private function s($bits) |
|
368 | + { |
|
369 | + $s = ""; |
|
370 | + |
|
371 | + for($i = 0; $i <= 42; $i += 6) |
|
372 | + { |
|
373 | + $sbits = substr($bits, $i, 6); |
|
374 | + |
|
375 | + // we need to determine the S-Box column number and row number |
|
376 | + // from the 6 bit string passed in, this is done using the following method: |
|
377 | + // The First & Last bits represent a number between 0-3, used to determine which row |
|
378 | + // The middle 4 bits represent a number between 0-15, used to determine the column |
|
379 | + $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
380 | + $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
381 | + |
|
382 | + // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
383 | + $pos = ($row * 16) + $col; |
|
384 | + |
|
385 | + // get the integer from the S-Box and convert it to binary |
|
386 | + $bin = decbin(self::$_s[($i/6)][$pos]); |
|
387 | + $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
388 | + } |
|
389 | + |
|
390 | + return $s; |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + /** |
|
395 | + * Permutation P |
|
396 | + * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
397 | + * from a 32-bit input from a binary string from the S-BOX by permuting |
|
398 | + * the bits of the input block. |
|
399 | + * This is only used inside of F() function |
|
400 | + * |
|
401 | + * @param string $s A 32-bit string originating from being passed through S-Box |
|
402 | + * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
403 | + */ |
|
404 | + private function p($s) |
|
405 | + { |
|
406 | + $p = ""; |
|
407 | + for($i = 0; $i < 32; ++$i) |
|
408 | + $p .= $s[self::$_p[$i] - 1]; |
|
409 | + |
|
410 | + return $p; |
|
411 | + } |
|
412 | + |
|
413 | + |
|
414 | + /** |
|
415 | + * Final Permutation (FP) |
|
416 | + * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
417 | + * we will do it to follow the DES standard). |
|
418 | + * |
|
419 | + * @param string $bin A 64-bit binary string |
|
420 | + * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
421 | + */ |
|
422 | + private function fp($bin) |
|
423 | + { |
|
424 | + $fp = ""; |
|
425 | + for($i = 0; $i < 64; ++$i) |
|
426 | + $fp .= $bin[self::$_fp[$i] - 1]; |
|
427 | + |
|
428 | + return $fp; |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * Initialize all the tables, this function is called inside the constructor |
|
434 | + * |
|
435 | + * @return void |
|
436 | + */ |
|
437 | + private function initTables() |
|
438 | + { |
|
439 | + // permuted choice 1 (PC1) |
|
440 | + // these values are chars and should be run through chr() when used |
|
441 | + self::$_pc1 = array( |
|
442 | + 57, 49, 41, 33, 25, 17, 9, |
|
443 | + 1, 58, 50, 42, 34, 26, 18, |
|
444 | + 10, 2, 59, 51, 43, 35, 27, |
|
445 | + 19, 11, 3, 60, 52, 44, 36, |
|
446 | + 63, 55, 47, 39, 31, 23, 15, |
|
447 | + 7, 62, 54, 46, 38, 30, 22, |
|
448 | + 14, 6, 61, 53, 45, 37, 29, |
|
449 | + 21, 13, 5, 28, 20, 12, 4 |
|
450 | + ); |
|
451 | + |
|
452 | + // permuted choice 2 (PC2) |
|
453 | + // these values are chars and should be run through chr() when used |
|
454 | + self::$_pc2 = array( |
|
455 | + 14, 17, 11, 24, 1, 5, |
|
456 | + 3, 28, 15, 6, 21, 10, |
|
457 | + 23, 19, 12, 4, 26, 8, |
|
458 | + 16, 7, 27, 20, 13, 2, |
|
459 | + 41, 52, 31, 37, 47, 55, |
|
460 | + 30, 40, 51, 45, 33, 48, |
|
461 | + 44, 49, 39, 56, 34, 53, |
|
462 | + 46, 42, 50, 36, 29, 32 |
|
463 | + ); |
|
464 | + |
|
465 | + // initial permutation (IP) |
|
466 | + self::$_ip = array( |
|
467 | + 58, 50, 42, 34, 26, 18, 10, 2, |
|
468 | + 60, 52, 44, 36, 28, 20, 12, 4, |
|
469 | + 62, 54, 46, 38, 30, 22, 14, 6, |
|
470 | + 64, 56, 48, 40, 32, 24, 16, 8, |
|
471 | + 57, 49, 41, 33, 25, 17, 9, 1, |
|
472 | + 59, 51, 43, 35, 27, 19, 11, 3, |
|
473 | + 61, 53, 45, 37, 29, 21, 13, 5, |
|
474 | + 63, 55, 47, 39, 31, 23, 15, 7 |
|
475 | + ); |
|
476 | + |
|
477 | + // expansion (E) |
|
478 | + self::$_e = array( |
|
479 | + 32, 1, 2, 3, 4, 5, |
|
480 | + 4, 5, 6, 7, 8, 9, |
|
481 | + 8, 9, 10, 11, 12, 13, |
|
482 | + 12, 13, 14, 15, 16, 17, |
|
483 | + 16, 17, 18, 19, 20, 21, |
|
484 | + 20, 21, 22, 23, 24, 25, |
|
485 | + 24, 25, 26, 27, 28, 29, |
|
486 | + 28, 29, 30, 31, 32, 1 |
|
487 | + ); |
|
488 | + |
|
489 | + // substition box (S) |
|
490 | + self::$_s = array( |
|
491 | + /* S1 */ |
|
492 | + array( |
|
493 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
494 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
495 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
496 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
497 | + ), |
|
498 | + |
|
499 | + /* S2 */ |
|
500 | + array( |
|
501 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
502 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
503 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
504 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
505 | + ), |
|
506 | + |
|
507 | + /* S3 */ |
|
508 | + array( |
|
509 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
510 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
511 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
512 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
513 | + ), |
|
514 | + |
|
515 | + /* S4 */ |
|
516 | + array( |
|
517 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
518 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
519 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
520 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
521 | + ), |
|
522 | + |
|
523 | + /* S5 */ |
|
524 | + array( |
|
525 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
526 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
527 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
528 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
529 | + ), |
|
530 | + |
|
531 | + /* S6 */ |
|
532 | + array( |
|
533 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
534 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
535 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
536 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
537 | + ), |
|
538 | + |
|
539 | + /* S7 */ |
|
540 | + array( |
|
541 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
542 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
543 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
544 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
545 | + ), |
|
546 | + |
|
547 | + /* S8 */ |
|
548 | + array( |
|
549 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
550 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
551 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
552 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
553 | + ) |
|
554 | + ); |
|
555 | + |
|
556 | + // permutation (P) |
|
557 | + self::$_p = array( |
|
558 | + 16, 7, 20, 21, |
|
559 | + 29, 12, 28, 17, |
|
560 | + 1, 15, 23, 26, |
|
561 | + 5, 18, 31, 10, |
|
562 | + 2, 8, 24, 14, |
|
563 | + 32, 27, 3, 9, |
|
564 | + 19, 13, 30, 6, |
|
565 | + 22, 11, 4, 25 |
|
566 | + ); |
|
567 | + |
|
568 | + // final permutation (FP) |
|
569 | + self::$_fp = array( |
|
570 | + 40, 8, 48, 16, 56, 24, 64, 32, |
|
571 | + 39, 7, 47, 15, 55, 23, 63, 31, |
|
572 | + 38, 6, 46, 14, 54, 22, 62, 30, |
|
573 | + 37, 5, 45, 13, 53, 21, 61, 29, |
|
574 | + 36, 4, 44, 12, 52, 20, 60, 28, |
|
575 | + 35, 3, 43, 11, 51, 19, 59, 27, |
|
576 | + 34, 2, 42, 10, 50, 18, 58, 26, |
|
577 | + 33, 1, 41, 9, 49, 17, 57, 25 |
|
578 | + ); |
|
579 | + |
|
580 | + // key schedule used in KeyPermutation() |
|
581 | + self::$_key_sched = array(1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1); |
|
582 | + } |
|
583 | + |
|
584 | + |
|
585 | + /** |
|
586 | + * Indicates this is a block cipher |
|
587 | + * |
|
588 | + * @return integer Returns Cipher::BLOCK |
|
589 | + */ |
|
590 | + public function type() |
|
591 | + { |
|
592 | + return parent::BLOCK; |
|
593 | + } |
|
594 | 594 | } |
595 | 595 | ?> |
@@ -199,17 +199,17 @@ discard block |
||
199 | 199 | $l[0] = substr($data, 0, 32); |
200 | 200 | $r[0] = substr($data, 32, 32); |
201 | 201 | |
202 | - for($n = 1; $n <= 16; ++$n) |
|
202 | + for ($n = 1; $n <= 16; ++$n) |
|
203 | 203 | { |
204 | - $l[$n] = $r[$n-1]; |
|
204 | + $l[$n] = $r[$n - 1]; |
|
205 | 205 | |
206 | - if($this->operation() == parent::DECRYPT) |
|
207 | - $f = $this->F($r[$n-1], $this->sub_keys[16-$n]); |
|
206 | + if ($this->operation() == parent::DECRYPT) |
|
207 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
208 | 208 | else |
209 | - $f = $this->F($r[$n-1], $this->sub_keys[$n-1]); |
|
209 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
210 | 210 | |
211 | 211 | // XOR F with Ln |
212 | - $r[$n] = $this->xorBin($l[$n-1], $f); |
|
212 | + $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | $binkey = parent::str2Bin($this->key()); |
243 | 243 | |
244 | 244 | // reduce the key down to 56bits based on table $_pc1 |
245 | - for($i = 0; $i < 56; ++$i) |
|
245 | + for ($i = 0; $i < 56; ++$i) |
|
246 | 246 | $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
247 | 247 | |
248 | 248 | // split $pc1m in half (C0 and D0) |
@@ -251,13 +251,13 @@ discard block |
||
251 | 251 | |
252 | 252 | // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
253 | 253 | // where 1 <= n <= 16 |
254 | - for($i = 1; $i <= 16; ++$i) |
|
254 | + for ($i = 1; $i <= 16; ++$i) |
|
255 | 255 | { |
256 | 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]; |
|
257 | + $c[$i] = $c[$i - 1]; |
|
258 | + $d[$i] = $d[$i - 1]; |
|
259 | 259 | |
260 | - for($j = 0; $j < self::$_key_sched[$i-1]; ++$j) |
|
260 | + for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
261 | 261 | { |
262 | 262 | // do a left shift, move each bit one place to the left, |
263 | 263 | // except for the first bit, which is cycled to the end |
@@ -271,9 +271,9 @@ discard block |
||
271 | 271 | // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
272 | 272 | // of these. |
273 | 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]; |
|
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 | 277 | } |
278 | 278 | |
279 | 279 | // the sub_keys are created, we are done with the key permutation |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | $ip = ""; |
306 | 306 | |
307 | 307 | // loop through the 64 bit block, ordering it occording to $_ip |
308 | - for($i = 0; $i < 64; ++$i) |
|
308 | + for ($i = 0; $i < 64; ++$i) |
|
309 | 309 | $ip .= $text[self::$_ip[$i] - 1]; |
310 | 310 | |
311 | 311 | return $ip; |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | private function e($r) |
349 | 349 | { |
350 | 350 | $e = ""; |
351 | - for($i = 0; $i < 48; ++$i) |
|
351 | + for ($i = 0; $i < 48; ++$i) |
|
352 | 352 | $e .= $r[self::$_e[$i] - 1]; |
353 | 353 | |
354 | 354 | return $e; |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | { |
369 | 369 | $s = ""; |
370 | 370 | |
371 | - for($i = 0; $i <= 42; $i += 6) |
|
371 | + for ($i = 0; $i <= 42; $i += 6) |
|
372 | 372 | { |
373 | 373 | $sbits = substr($bits, $i, 6); |
374 | 374 | |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | $pos = ($row * 16) + $col; |
384 | 384 | |
385 | 385 | // get the integer from the S-Box and convert it to binary |
386 | - $bin = decbin(self::$_s[($i/6)][$pos]); |
|
386 | + $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
387 | 387 | $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
388 | 388 | } |
389 | 389 | |
@@ -404,7 +404,7 @@ discard block |
||
404 | 404 | private function p($s) |
405 | 405 | { |
406 | 406 | $p = ""; |
407 | - for($i = 0; $i < 32; ++$i) |
|
407 | + for ($i = 0; $i < 32; ++$i) |
|
408 | 408 | $p .= $s[self::$_p[$i] - 1]; |
409 | 409 | |
410 | 410 | return $p; |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | private function fp($bin) |
423 | 423 | { |
424 | 424 | $fp = ""; |
425 | - for($i = 0; $i < 64; ++$i) |
|
425 | + for ($i = 0; $i < 64; ++$i) |
|
426 | 426 | $fp .= $bin[self::$_fp[$i] - 1]; |
427 | 427 | |
428 | 428 | return $fp; |
@@ -439,23 +439,23 @@ discard block |
||
439 | 439 | // permuted choice 1 (PC1) |
440 | 440 | // these values are chars and should be run through chr() when used |
441 | 441 | self::$_pc1 = array( |
442 | - 57, 49, 41, 33, 25, 17, 9, |
|
442 | + 57, 49, 41, 33, 25, 17, 9, |
|
443 | 443 | 1, 58, 50, 42, 34, 26, 18, |
444 | - 10, 2, 59, 51, 43, 35, 27, |
|
445 | - 19, 11, 3, 60, 52, 44, 36, |
|
444 | + 10, 2, 59, 51, 43, 35, 27, |
|
445 | + 19, 11, 3, 60, 52, 44, 36, |
|
446 | 446 | 63, 55, 47, 39, 31, 23, 15, |
447 | 447 | 7, 62, 54, 46, 38, 30, 22, |
448 | - 14, 6, 61, 53, 45, 37, 29, |
|
449 | - 21, 13, 5, 28, 20, 12, 4 |
|
448 | + 14, 6, 61, 53, 45, 37, 29, |
|
449 | + 21, 13, 5, 28, 20, 12, 4 |
|
450 | 450 | ); |
451 | 451 | |
452 | 452 | // permuted choice 2 (PC2) |
453 | 453 | // these values are chars and should be run through chr() when used |
454 | 454 | self::$_pc2 = array( |
455 | - 14, 17, 11, 24, 1, 5, |
|
456 | - 3, 28, 15, 6, 21, 10, |
|
457 | - 23, 19, 12, 4, 26, 8, |
|
458 | - 16, 7, 27, 20, 13, 2, |
|
455 | + 14, 17, 11, 24, 1, 5, |
|
456 | + 3, 28, 15, 6, 21, 10, |
|
457 | + 23, 19, 12, 4, 26, 8, |
|
458 | + 16, 7, 27, 20, 13, 2, |
|
459 | 459 | 41, 52, 31, 37, 47, 55, |
460 | 460 | 30, 40, 51, 45, 33, 48, |
461 | 461 | 44, 49, 39, 56, 34, 53, |
@@ -468,7 +468,7 @@ discard block |
||
468 | 468 | 60, 52, 44, 36, 28, 20, 12, 4, |
469 | 469 | 62, 54, 46, 38, 30, 22, 14, 6, |
470 | 470 | 64, 56, 48, 40, 32, 24, 16, 8, |
471 | - 57, 49, 41, 33, 25, 17, 9, 1, |
|
471 | + 57, 49, 41, 33, 25, 17, 9, 1, |
|
472 | 472 | 59, 51, 43, 35, 27, 19, 11, 3, |
473 | 473 | 61, 53, 45, 37, 29, 21, 13, 5, |
474 | 474 | 63, 55, 47, 39, 31, 23, 15, 7 |
@@ -476,93 +476,93 @@ discard block |
||
476 | 476 | |
477 | 477 | // expansion (E) |
478 | 478 | self::$_e = array( |
479 | - 32, 1, 2, 3, 4, 5, |
|
480 | - 4, 5, 6, 7, 8, 9, |
|
481 | - 8, 9, 10, 11, 12, 13, |
|
479 | + 32, 1, 2, 3, 4, 5, |
|
480 | + 4, 5, 6, 7, 8, 9, |
|
481 | + 8, 9, 10, 11, 12, 13, |
|
482 | 482 | 12, 13, 14, 15, 16, 17, |
483 | 483 | 16, 17, 18, 19, 20, 21, |
484 | 484 | 20, 21, 22, 23, 24, 25, |
485 | 485 | 24, 25, 26, 27, 28, 29, |
486 | - 28, 29, 30, 31, 32, 1 |
|
486 | + 28, 29, 30, 31, 32, 1 |
|
487 | 487 | ); |
488 | 488 | |
489 | 489 | // substition box (S) |
490 | 490 | self::$_s = array( |
491 | 491 | /* S1 */ |
492 | 492 | array( |
493 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
494 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
495 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
496 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
493 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
494 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
495 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
496 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
497 | 497 | ), |
498 | 498 | |
499 | 499 | /* S2 */ |
500 | 500 | array( |
501 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
502 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
503 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
504 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
501 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
502 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
503 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
504 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
505 | 505 | ), |
506 | 506 | |
507 | 507 | /* S3 */ |
508 | 508 | array( |
509 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
510 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
511 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
512 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
509 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
510 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
511 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
512 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
513 | 513 | ), |
514 | 514 | |
515 | 515 | /* S4 */ |
516 | 516 | array( |
517 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
518 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
519 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
520 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
517 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
518 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
519 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
520 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
521 | 521 | ), |
522 | 522 | |
523 | 523 | /* S5 */ |
524 | 524 | array( |
525 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
526 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
527 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
528 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
525 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
526 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
527 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
528 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
529 | 529 | ), |
530 | 530 | |
531 | 531 | /* S6 */ |
532 | 532 | array( |
533 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
534 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
535 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
536 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
533 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
534 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
535 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
536 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
537 | 537 | ), |
538 | 538 | |
539 | 539 | /* S7 */ |
540 | 540 | array( |
541 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
542 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
543 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
544 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
541 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
542 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
543 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
544 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
545 | 545 | ), |
546 | 546 | |
547 | 547 | /* S8 */ |
548 | 548 | array( |
549 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
550 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
551 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
552 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
549 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
550 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
551 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
552 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
553 | 553 | ) |
554 | 554 | ); |
555 | 555 | |
556 | 556 | // permutation (P) |
557 | 557 | self::$_p = array( |
558 | - 16, 7, 20, 21, |
|
558 | + 16, 7, 20, 21, |
|
559 | 559 | 29, 12, 28, 17, |
560 | 560 | 1, 15, 23, 26, |
561 | 561 | 5, 18, 31, 10, |
562 | - 2, 8, 24, 14, |
|
563 | - 32, 27, 3, 9, |
|
564 | - 19, 13, 30, 6, |
|
565 | - 22, 11, 4, 25 |
|
562 | + 2, 8, 24, 14, |
|
563 | + 32, 27, 3, 9, |
|
564 | + 19, 13, 30, 6, |
|
565 | + 22, 11, 4, 25 |
|
566 | 566 | ); |
567 | 567 | |
568 | 568 | // final permutation (FP) |
@@ -574,11 +574,11 @@ discard block |
||
574 | 574 | 36, 4, 44, 12, 52, 20, 60, 28, |
575 | 575 | 35, 3, 43, 11, 51, 19, 59, 27, |
576 | 576 | 34, 2, 42, 10, 50, 18, 58, 26, |
577 | - 33, 1, 41, 9, 49, 17, 57, 25 |
|
577 | + 33, 1, 41, 9, 49, 17, 57, 25 |
|
578 | 578 | ); |
579 | 579 | |
580 | 580 | // key schedule used in KeyPermutation() |
581 | - self::$_key_sched = array(1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1); |
|
581 | + self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
582 | 582 | } |
583 | 583 | |
584 | 584 |
@@ -203,10 +203,11 @@ discard block |
||
203 | 203 | { |
204 | 204 | $l[$n] = $r[$n-1]; |
205 | 205 | |
206 | - if($this->operation() == parent::DECRYPT) |
|
207 | - $f = $this->F($r[$n-1], $this->sub_keys[16-$n]); |
|
208 | - else |
|
209 | - $f = $this->F($r[$n-1], $this->sub_keys[$n-1]); |
|
206 | + if($this->operation() == parent::DECRYPT) { |
|
207 | + $f = $this->F($r[$n-1], $this->sub_keys[16-$n]); |
|
208 | + } else { |
|
209 | + $f = $this->F($r[$n-1], $this->sub_keys[$n-1]); |
|
210 | + } |
|
210 | 211 | |
211 | 212 | // XOR F with Ln |
212 | 213 | $r[$n] = $this->xorBin($l[$n-1], $f); |
@@ -242,8 +243,9 @@ discard block |
||
242 | 243 | $binkey = parent::str2Bin($this->key()); |
243 | 244 | |
244 | 245 | // reduce the key down to 56bits based on table $_pc1 |
245 | - for($i = 0; $i < 56; ++$i) |
|
246 | - $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
|
246 | + for($i = 0; $i < 56; ++$i) { |
|
247 | + $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
|
248 | + } |
|
247 | 249 | |
248 | 250 | // split $pc1m in half (C0 and D0) |
249 | 251 | $c[0] = array_slice($pc1m, 0, 28); |
@@ -272,8 +274,9 @@ discard block |
||
272 | 274 | // of these. |
273 | 275 | $CnDn = array_merge($c[$i], $d[$i]); |
274 | 276 | $this->sub_keys[$i-1] = ""; |
275 | - for($j = 0; $j < 48; ++$j) |
|
276 | - $this->sub_keys[$i-1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
277 | + for($j = 0; $j < 48; ++$j) { |
|
278 | + $this->sub_keys[$i-1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
279 | + } |
|
277 | 280 | } |
278 | 281 | |
279 | 282 | // the sub_keys are created, we are done with the key permutation |
@@ -305,8 +308,9 @@ discard block |
||
305 | 308 | $ip = ""; |
306 | 309 | |
307 | 310 | // loop through the 64 bit block, ordering it occording to $_ip |
308 | - for($i = 0; $i < 64; ++$i) |
|
309 | - $ip .= $text[self::$_ip[$i] - 1]; |
|
311 | + for($i = 0; $i < 64; ++$i) { |
|
312 | + $ip .= $text[self::$_ip[$i] - 1]; |
|
313 | + } |
|
310 | 314 | |
311 | 315 | return $ip; |
312 | 316 | } |
@@ -348,8 +352,9 @@ discard block |
||
348 | 352 | private function e($r) |
349 | 353 | { |
350 | 354 | $e = ""; |
351 | - for($i = 0; $i < 48; ++$i) |
|
352 | - $e .= $r[self::$_e[$i] - 1]; |
|
355 | + for($i = 0; $i < 48; ++$i) { |
|
356 | + $e .= $r[self::$_e[$i] - 1]; |
|
357 | + } |
|
353 | 358 | |
354 | 359 | return $e; |
355 | 360 | } |
@@ -404,8 +409,9 @@ discard block |
||
404 | 409 | private function p($s) |
405 | 410 | { |
406 | 411 | $p = ""; |
407 | - for($i = 0; $i < 32; ++$i) |
|
408 | - $p .= $s[self::$_p[$i] - 1]; |
|
412 | + for($i = 0; $i < 32; ++$i) { |
|
413 | + $p .= $s[self::$_p[$i] - 1]; |
|
414 | + } |
|
409 | 415 | |
410 | 416 | return $p; |
411 | 417 | } |
@@ -422,8 +428,9 @@ discard block |
||
422 | 428 | private function fp($bin) |
423 | 429 | { |
424 | 430 | $fp = ""; |
425 | - for($i = 0; $i < 64; ++$i) |
|
426 | - $fp .= $bin[self::$_fp[$i] - 1]; |
|
431 | + for($i = 0; $i < 64; ++$i) { |
|
432 | + $fp .= $bin[self::$_fp[$i] - 1]; |
|
433 | + } |
|
427 | 434 | |
428 | 435 | return $fp; |
429 | 436 | } |
@@ -618,7 +618,7 @@ discard block |
||
618 | 618 | * Expands the key |
619 | 619 | * The key expands based on the block size as well as the key size |
620 | 620 | * |
621 | - * @return void |
|
621 | + * @return boolean|null |
|
622 | 622 | */ |
623 | 623 | protected function expandKey() |
624 | 624 | { |
@@ -635,7 +635,7 @@ discard block |
||
635 | 635 | * Expand a 16 byte key, the size it is expanded to varies |
636 | 636 | * based on the block size of the Rijndael implementation chosen |
637 | 637 | * |
638 | - * @return void |
|
638 | + * @return boolean |
|
639 | 639 | */ |
640 | 640 | private function expandKey128() |
641 | 641 | { |
@@ -695,7 +695,7 @@ discard block |
||
695 | 695 | * Expand a 24 byte key, the size it is expanded to varies |
696 | 696 | * based on the block size of the Rijndael implementation chosen |
697 | 697 | * |
698 | - * @return void |
|
698 | + * @return boolean |
|
699 | 699 | */ |
700 | 700 | private function expandKey192() |
701 | 701 | { |
@@ -755,7 +755,7 @@ discard block |
||
755 | 755 | * Expand a 32 byte key, the size it is expanded to varies |
756 | 756 | * based on the block size of the Rijndael implementation chosen |
757 | 757 | * |
758 | - * @return void |
|
758 | + * @return boolean |
|
759 | 759 | */ |
760 | 760 | private function expandKey256() |
761 | 761 | { |
@@ -42,1000 +42,1000 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class Cipher_Rijndael extends Cipher |
44 | 44 | { |
45 | - /** @type string $xkey The expanded key */ |
|
46 | - private $xkey = ""; |
|
47 | - |
|
48 | - /** |
|
49 | - * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | - * this should be considered a constant |
|
51 | - */ |
|
52 | - private static $_key_sizes = array(16, 24, 32); |
|
53 | - |
|
54 | - |
|
55 | - // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | - |
|
57 | - /** |
|
58 | - * @type array $_s The sbox, |
|
59 | - * this should be considered a constant |
|
60 | - */ |
|
61 | - private static $_s = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * @type array $_s_inv The inverse sbox |
|
65 | - * this should be considered a constant |
|
66 | - */ |
|
67 | - private static $_s_inv = array(); |
|
68 | - |
|
69 | - /** |
|
70 | - * @type array $_rcon The round constant, |
|
71 | - * this should be considered a constant |
|
72 | - */ |
|
73 | - private static $_rcon = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | - * this should be considered a constant |
|
78 | - */ |
|
79 | - private static $_matrix_mult = array(); |
|
80 | - |
|
81 | - /** |
|
82 | - * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | - * inverse table, this should be considered a constant |
|
84 | - */ |
|
85 | - private static $_matrix_mult_inv = array(); |
|
86 | - |
|
87 | - /* |
|
45 | + /** @type string $xkey The expanded key */ |
|
46 | + private $xkey = ""; |
|
47 | + |
|
48 | + /** |
|
49 | + * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | + * this should be considered a constant |
|
51 | + */ |
|
52 | + private static $_key_sizes = array(16, 24, 32); |
|
53 | + |
|
54 | + |
|
55 | + // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | + |
|
57 | + /** |
|
58 | + * @type array $_s The sbox, |
|
59 | + * this should be considered a constant |
|
60 | + */ |
|
61 | + private static $_s = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * @type array $_s_inv The inverse sbox |
|
65 | + * this should be considered a constant |
|
66 | + */ |
|
67 | + private static $_s_inv = array(); |
|
68 | + |
|
69 | + /** |
|
70 | + * @type array $_rcon The round constant, |
|
71 | + * this should be considered a constant |
|
72 | + */ |
|
73 | + private static $_rcon = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | + * this should be considered a constant |
|
78 | + */ |
|
79 | + private static $_matrix_mult = array(); |
|
80 | + |
|
81 | + /** |
|
82 | + * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | + * inverse table, this should be considered a constant |
|
84 | + */ |
|
85 | + private static $_matrix_mult_inv = array(); |
|
86 | + |
|
87 | + /* |
|
88 | 88 | * Galois Multiplication lookup tables, |
89 | 89 | * initialized in initTables() |
90 | 90 | */ |
91 | - /** |
|
92 | - * @type array $_gm2 The Galois Multiplication table |
|
93 | - * for muliplying by 2, this should be considered a constant |
|
94 | - */ |
|
95 | - private static $_gm2 = array(); |
|
96 | - |
|
97 | - /** |
|
98 | - * @type array $_gm3 The Galois Multiplication table |
|
99 | - * for muliplying by 3, this should be considered a constant |
|
100 | - */ |
|
101 | - private static $_gm3 = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * @type array $_gm9 The Galois Multiplication table |
|
105 | - * for muliplying by 9, this should be considered a constant |
|
106 | - */ |
|
107 | - private static $_gm9 = array(); |
|
108 | - |
|
109 | - /** |
|
110 | - * @type array $_gm11 The Galois Multiplication table |
|
111 | - * for muliplying by 11, this should be considered a constant |
|
112 | - */ |
|
113 | - private static $_gm11 = array(); |
|
114 | - |
|
115 | - /** |
|
116 | - * @type array $_gm13 The Galois Multiplication table |
|
117 | - * for muliplying by 13, this should be considered a constant |
|
118 | - */ |
|
119 | - private static $_gm13 = array(); |
|
120 | - |
|
121 | - /** |
|
122 | - * @type array $_gm14 The Galois Multiplication table |
|
123 | - * for muliplying by 14, this should be considered a constant |
|
124 | - */ |
|
125 | - private static $_gm14 = array(); |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Constructor |
|
130 | - * Sets the key used for encryption. Also sets the requied block size |
|
131 | - * |
|
132 | - * @param string The cipher name as set in a constant in the child class |
|
133 | - * @param string $key string containing the user supplied encryption key |
|
134 | - * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function __construct($cipher_name, $key, $len = 0) |
|
138 | - { |
|
139 | - // AES will pass in a $len, since it has a fixed key size, other |
|
140 | - // rijndael implementations can use variable key sizes, supported |
|
141 | - // sizes are stored in self::$_key_sizes |
|
142 | - if($len == 0) |
|
143 | - { |
|
144 | - // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | - $len = strlen($key); |
|
146 | - if(!in_array($len, self::$_key_sizes)) |
|
147 | - { |
|
148 | - $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | - $msg .= "Received $len bytes."; |
|
150 | - trigger_error($msg, E_USER_WARNING); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - // Setup the key |
|
155 | - parent::__construct($cipher_name, $key, $len); |
|
156 | - |
|
157 | - // initialize the tables used for rijndael/aes |
|
158 | - $this->initTables(); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Destructor |
|
164 | - * |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public function __destruct() |
|
168 | - { |
|
169 | - parent::__destruct(); |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Performs Rijndael encryption |
|
175 | - * |
|
176 | - * @param string @text The string to encrypt |
|
177 | - * @return boolean Returns true |
|
178 | - */ |
|
179 | - public function encrypt(&$text) |
|
180 | - { |
|
181 | - // set the operation to decryption |
|
182 | - $this->operation(parent::ENCRYPT); |
|
183 | - |
|
184 | - $loops = 0; |
|
185 | - $key_sz = $this->keySize(); |
|
186 | - $blk_sz = $this->blockSize(); |
|
187 | - |
|
188 | - // if the key and block size is 16, do 10 rounds |
|
189 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
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; |
|
197 | - |
|
198 | - // now begin the encryption |
|
199 | - $this->addRoundKey($text, 0); |
|
200 | - |
|
201 | - for($i = 1; $i <= $loops; ++$i) |
|
202 | - { |
|
203 | - $this->byteSub($text); |
|
204 | - $this->shiftRow($text); |
|
205 | - |
|
206 | - // the last iteration does not use mixColumn |
|
207 | - if($i < $loops) |
|
208 | - $this->mixColumn($text); |
|
209 | - |
|
210 | - $this->addRoundKey($text, $i); |
|
211 | - } |
|
212 | - |
|
213 | - return true; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * Performs Rijndael decryption |
|
219 | - * |
|
220 | - * @param string @text The string to decrypt |
|
221 | - * @return boolean Returns true |
|
222 | - */ |
|
223 | - public function decrypt(&$text) |
|
224 | - { |
|
225 | - // set the operation to decryption |
|
226 | - $this->operation(parent::DECRYPT); |
|
227 | - |
|
228 | - $loops = 0; |
|
229 | - $key_sz = $this->keySize(); |
|
230 | - $blk_sz = $this->blockSize(); |
|
231 | - |
|
232 | - // if the key and block size is 16, do 10 rounds |
|
233 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | - // 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; |
|
241 | - |
|
242 | - // now begin the decryption |
|
243 | - $this->addRoundKey($text, 0); |
|
244 | - |
|
245 | - for($i = 1; $i <= $loops; ++$i) |
|
246 | - { |
|
247 | - $this->shiftRow($text); |
|
248 | - $this->byteSub($text); |
|
249 | - $this->addRoundKey($text, $i); |
|
250 | - |
|
251 | - // the last iteration does not use mixColumn |
|
252 | - if($i < $loops) |
|
253 | - $this->mixColumn($text); |
|
254 | - } |
|
255 | - |
|
256 | - return true; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * Indicates that this is a block cipher |
|
262 | - * |
|
263 | - * @return integer Returns Cipher::BLOCK |
|
264 | - */ |
|
265 | - public function type() |
|
266 | - { |
|
267 | - return parent::BLOCK; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * Do the multiplication required in mixColumn() |
|
273 | - * We follow the description the multiplication from Wikipedia: |
|
274 | - * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | - * |
|
276 | - * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | - * @param integer $byte The value to multipy by $m |
|
278 | - * @return integer The result of the multiplication |
|
279 | - */ |
|
280 | - protected function mixColumnMultiply($m, $byte) |
|
281 | - { |
|
282 | - // if multiplying by 1, then we just return the same number |
|
283 | - if($m == 0x01) |
|
284 | - return $byte; |
|
285 | - |
|
286 | - $hex = parent::dec2Hex($byte); |
|
287 | - $row = parent::hex2Dec($hex[0]); |
|
288 | - $col = parent::hex2Dec($hex[1]); |
|
289 | - $pos = ($row * 16) + $col; |
|
290 | - |
|
291 | - // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | - if($m == 0x02) |
|
293 | - return self::$_gm2[$pos]; |
|
294 | - |
|
295 | - // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | - if($m == 0x03) |
|
297 | - return self::$_gm3[$pos]; |
|
298 | - |
|
299 | - // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | - if($m == 0x09) |
|
301 | - return self::$_gm9[$pos]; |
|
302 | - |
|
303 | - // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | - if($m == 0x0b) |
|
305 | - return self::$_gm11[$pos]; |
|
306 | - |
|
307 | - // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | - if($m == 0x0d) |
|
309 | - return self::$_gm13[$pos]; |
|
310 | - |
|
311 | - // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | - if($m == 0x0e) |
|
313 | - return self::$_gm14[$pos]; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | - * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | - * never has the same bytes used twice. Note that the first time |
|
321 | - * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | - * $round is given. Every call after that will be inside the rounds. |
|
323 | - * |
|
324 | - * @param string $text The text to encrypt/decrypt |
|
325 | - * @param integer $round The round we are on inside of rijndael() |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function addRoundKey(&$text, $round) |
|
329 | - { |
|
330 | - // length of the xkey |
|
331 | - $ek_len = strlen($this->xkey); |
|
332 | - $len = $this->blockSize(); |
|
333 | - |
|
334 | - if($this->operation() == parent::ENCRYPT) |
|
335 | - $offset = $round * $len; |
|
336 | - else |
|
337 | - $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | - |
|
339 | - for($i = 0; $i < $len; ++$i) |
|
340 | - $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Applies the Sbox to each byte of the string passed in |
|
346 | - * This is used in key expansione |
|
347 | - * |
|
348 | - * @param string $text The string to peform the byte subsitution |
|
349 | - * @return void |
|
350 | - */ |
|
351 | - private function byteSub(&$text) |
|
352 | - { |
|
353 | - $max = strlen($text); |
|
354 | - for($i = 0; $i < $max; ++$i) |
|
355 | - { |
|
356 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | - // and column is numbered in hex (from 0 - f) |
|
358 | - $hex = parent::str2Hex($text[$i]); |
|
359 | - $row = parent::hex2Dec($hex[0]); |
|
360 | - $col = parent::hex2Dec($hex[1]); |
|
361 | - $pos = ($row * 16) + $col; |
|
362 | - |
|
363 | - // 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 |
|
367 | - $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | - } |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * This function is hard to explain, the easiest way to understand it is to read |
|
374 | - * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | - * |
|
376 | - * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | - * @return void |
|
378 | - */ |
|
379 | - private function mixColumn(&$t) |
|
380 | - { |
|
381 | - $tmp = $t; |
|
382 | - |
|
383 | - // 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 |
|
387 | - $m = self::$_matrix_mult_inv; |
|
388 | - |
|
389 | - // the number of rounds we make depends on the block size of the text |
|
390 | - // used during encryption/decryption |
|
391 | - // 128 has a 4x4 matrix, loop 4 times |
|
392 | - // 192 has a 4x6 matrix, loop 6 times |
|
393 | - // 256 has a 4x8 matrix, loop 8 times |
|
394 | - $max_col = ($this->blockSize() * 8) / 32; |
|
395 | - |
|
396 | - // loop through each column of the matrix |
|
397 | - for($col = 0; $col < $max_col; ++$col) |
|
398 | - { |
|
399 | - $pos = $col * 4; |
|
400 | - |
|
401 | - $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | - $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | - $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | - $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | - $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | - |
|
407 | - $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | - $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | - $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | - $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | - $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | - |
|
413 | - $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | - $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | - $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | - $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | - $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | - |
|
419 | - $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | - $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | - $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | - $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | - $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | - * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | - * Row shifts depend on the bit size of the block $text. |
|
432 | - * Rijndael-128/AES: 4x4 matrix |
|
433 | - * Rijndael-192: 6x4 matrix |
|
434 | - * Rijndael-256: 8x4 matrix |
|
435 | - * |
|
436 | - * @param string $text A 16, 24, or 32 byte string |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - private function shiftRow(&$text) |
|
440 | - { |
|
441 | - /* |
|
91 | + /** |
|
92 | + * @type array $_gm2 The Galois Multiplication table |
|
93 | + * for muliplying by 2, this should be considered a constant |
|
94 | + */ |
|
95 | + private static $_gm2 = array(); |
|
96 | + |
|
97 | + /** |
|
98 | + * @type array $_gm3 The Galois Multiplication table |
|
99 | + * for muliplying by 3, this should be considered a constant |
|
100 | + */ |
|
101 | + private static $_gm3 = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * @type array $_gm9 The Galois Multiplication table |
|
105 | + * for muliplying by 9, this should be considered a constant |
|
106 | + */ |
|
107 | + private static $_gm9 = array(); |
|
108 | + |
|
109 | + /** |
|
110 | + * @type array $_gm11 The Galois Multiplication table |
|
111 | + * for muliplying by 11, this should be considered a constant |
|
112 | + */ |
|
113 | + private static $_gm11 = array(); |
|
114 | + |
|
115 | + /** |
|
116 | + * @type array $_gm13 The Galois Multiplication table |
|
117 | + * for muliplying by 13, this should be considered a constant |
|
118 | + */ |
|
119 | + private static $_gm13 = array(); |
|
120 | + |
|
121 | + /** |
|
122 | + * @type array $_gm14 The Galois Multiplication table |
|
123 | + * for muliplying by 14, this should be considered a constant |
|
124 | + */ |
|
125 | + private static $_gm14 = array(); |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Constructor |
|
130 | + * Sets the key used for encryption. Also sets the requied block size |
|
131 | + * |
|
132 | + * @param string The cipher name as set in a constant in the child class |
|
133 | + * @param string $key string containing the user supplied encryption key |
|
134 | + * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function __construct($cipher_name, $key, $len = 0) |
|
138 | + { |
|
139 | + // AES will pass in a $len, since it has a fixed key size, other |
|
140 | + // rijndael implementations can use variable key sizes, supported |
|
141 | + // sizes are stored in self::$_key_sizes |
|
142 | + if($len == 0) |
|
143 | + { |
|
144 | + // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | + $len = strlen($key); |
|
146 | + if(!in_array($len, self::$_key_sizes)) |
|
147 | + { |
|
148 | + $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | + $msg .= "Received $len bytes."; |
|
150 | + trigger_error($msg, E_USER_WARNING); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + // Setup the key |
|
155 | + parent::__construct($cipher_name, $key, $len); |
|
156 | + |
|
157 | + // initialize the tables used for rijndael/aes |
|
158 | + $this->initTables(); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Destructor |
|
164 | + * |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public function __destruct() |
|
168 | + { |
|
169 | + parent::__destruct(); |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Performs Rijndael encryption |
|
175 | + * |
|
176 | + * @param string @text The string to encrypt |
|
177 | + * @return boolean Returns true |
|
178 | + */ |
|
179 | + public function encrypt(&$text) |
|
180 | + { |
|
181 | + // set the operation to decryption |
|
182 | + $this->operation(parent::ENCRYPT); |
|
183 | + |
|
184 | + $loops = 0; |
|
185 | + $key_sz = $this->keySize(); |
|
186 | + $blk_sz = $this->blockSize(); |
|
187 | + |
|
188 | + // if the key and block size is 16, do 10 rounds |
|
189 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
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; |
|
197 | + |
|
198 | + // now begin the encryption |
|
199 | + $this->addRoundKey($text, 0); |
|
200 | + |
|
201 | + for($i = 1; $i <= $loops; ++$i) |
|
202 | + { |
|
203 | + $this->byteSub($text); |
|
204 | + $this->shiftRow($text); |
|
205 | + |
|
206 | + // the last iteration does not use mixColumn |
|
207 | + if($i < $loops) |
|
208 | + $this->mixColumn($text); |
|
209 | + |
|
210 | + $this->addRoundKey($text, $i); |
|
211 | + } |
|
212 | + |
|
213 | + return true; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * Performs Rijndael decryption |
|
219 | + * |
|
220 | + * @param string @text The string to decrypt |
|
221 | + * @return boolean Returns true |
|
222 | + */ |
|
223 | + public function decrypt(&$text) |
|
224 | + { |
|
225 | + // set the operation to decryption |
|
226 | + $this->operation(parent::DECRYPT); |
|
227 | + |
|
228 | + $loops = 0; |
|
229 | + $key_sz = $this->keySize(); |
|
230 | + $blk_sz = $this->blockSize(); |
|
231 | + |
|
232 | + // if the key and block size is 16, do 10 rounds |
|
233 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | + // 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; |
|
241 | + |
|
242 | + // now begin the decryption |
|
243 | + $this->addRoundKey($text, 0); |
|
244 | + |
|
245 | + for($i = 1; $i <= $loops; ++$i) |
|
246 | + { |
|
247 | + $this->shiftRow($text); |
|
248 | + $this->byteSub($text); |
|
249 | + $this->addRoundKey($text, $i); |
|
250 | + |
|
251 | + // the last iteration does not use mixColumn |
|
252 | + if($i < $loops) |
|
253 | + $this->mixColumn($text); |
|
254 | + } |
|
255 | + |
|
256 | + return true; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * Indicates that this is a block cipher |
|
262 | + * |
|
263 | + * @return integer Returns Cipher::BLOCK |
|
264 | + */ |
|
265 | + public function type() |
|
266 | + { |
|
267 | + return parent::BLOCK; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * Do the multiplication required in mixColumn() |
|
273 | + * We follow the description the multiplication from Wikipedia: |
|
274 | + * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | + * |
|
276 | + * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | + * @param integer $byte The value to multipy by $m |
|
278 | + * @return integer The result of the multiplication |
|
279 | + */ |
|
280 | + protected function mixColumnMultiply($m, $byte) |
|
281 | + { |
|
282 | + // if multiplying by 1, then we just return the same number |
|
283 | + if($m == 0x01) |
|
284 | + return $byte; |
|
285 | + |
|
286 | + $hex = parent::dec2Hex($byte); |
|
287 | + $row = parent::hex2Dec($hex[0]); |
|
288 | + $col = parent::hex2Dec($hex[1]); |
|
289 | + $pos = ($row * 16) + $col; |
|
290 | + |
|
291 | + // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | + if($m == 0x02) |
|
293 | + return self::$_gm2[$pos]; |
|
294 | + |
|
295 | + // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | + if($m == 0x03) |
|
297 | + return self::$_gm3[$pos]; |
|
298 | + |
|
299 | + // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | + if($m == 0x09) |
|
301 | + return self::$_gm9[$pos]; |
|
302 | + |
|
303 | + // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | + if($m == 0x0b) |
|
305 | + return self::$_gm11[$pos]; |
|
306 | + |
|
307 | + // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | + if($m == 0x0d) |
|
309 | + return self::$_gm13[$pos]; |
|
310 | + |
|
311 | + // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | + if($m == 0x0e) |
|
313 | + return self::$_gm14[$pos]; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | + * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | + * never has the same bytes used twice. Note that the first time |
|
321 | + * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | + * $round is given. Every call after that will be inside the rounds. |
|
323 | + * |
|
324 | + * @param string $text The text to encrypt/decrypt |
|
325 | + * @param integer $round The round we are on inside of rijndael() |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function addRoundKey(&$text, $round) |
|
329 | + { |
|
330 | + // length of the xkey |
|
331 | + $ek_len = strlen($this->xkey); |
|
332 | + $len = $this->blockSize(); |
|
333 | + |
|
334 | + if($this->operation() == parent::ENCRYPT) |
|
335 | + $offset = $round * $len; |
|
336 | + else |
|
337 | + $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | + |
|
339 | + for($i = 0; $i < $len; ++$i) |
|
340 | + $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Applies the Sbox to each byte of the string passed in |
|
346 | + * This is used in key expansione |
|
347 | + * |
|
348 | + * @param string $text The string to peform the byte subsitution |
|
349 | + * @return void |
|
350 | + */ |
|
351 | + private function byteSub(&$text) |
|
352 | + { |
|
353 | + $max = strlen($text); |
|
354 | + for($i = 0; $i < $max; ++$i) |
|
355 | + { |
|
356 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | + // and column is numbered in hex (from 0 - f) |
|
358 | + $hex = parent::str2Hex($text[$i]); |
|
359 | + $row = parent::hex2Dec($hex[0]); |
|
360 | + $col = parent::hex2Dec($hex[1]); |
|
361 | + $pos = ($row * 16) + $col; |
|
362 | + |
|
363 | + // 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 |
|
367 | + $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | + } |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * This function is hard to explain, the easiest way to understand it is to read |
|
374 | + * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | + * |
|
376 | + * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | + * @return void |
|
378 | + */ |
|
379 | + private function mixColumn(&$t) |
|
380 | + { |
|
381 | + $tmp = $t; |
|
382 | + |
|
383 | + // 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 |
|
387 | + $m = self::$_matrix_mult_inv; |
|
388 | + |
|
389 | + // the number of rounds we make depends on the block size of the text |
|
390 | + // used during encryption/decryption |
|
391 | + // 128 has a 4x4 matrix, loop 4 times |
|
392 | + // 192 has a 4x6 matrix, loop 6 times |
|
393 | + // 256 has a 4x8 matrix, loop 8 times |
|
394 | + $max_col = ($this->blockSize() * 8) / 32; |
|
395 | + |
|
396 | + // loop through each column of the matrix |
|
397 | + for($col = 0; $col < $max_col; ++$col) |
|
398 | + { |
|
399 | + $pos = $col * 4; |
|
400 | + |
|
401 | + $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | + $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | + $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | + $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | + $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | + |
|
407 | + $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | + $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | + $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | + $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | + $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | + |
|
413 | + $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | + $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | + $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | + $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | + $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | + |
|
419 | + $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | + $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | + $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | + $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | + $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | + * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | + * Row shifts depend on the bit size of the block $text. |
|
432 | + * Rijndael-128/AES: 4x4 matrix |
|
433 | + * Rijndael-192: 6x4 matrix |
|
434 | + * Rijndael-256: 8x4 matrix |
|
435 | + * |
|
436 | + * @param string $text A 16, 24, or 32 byte string |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + private function shiftRow(&$text) |
|
440 | + { |
|
441 | + /* |
|
442 | 442 | * Rijndael-128 / AES |
443 | 443 | */ |
444 | - if($this->blockSize() == 16) |
|
445 | - { |
|
446 | - if($this->operation() == parent::ENCRYPT) |
|
447 | - { |
|
448 | - // create a 4x4 matrix |
|
449 | - // row 0 is unchanged, |
|
450 | - // shift row 1 left 1 byte |
|
451 | - // shift row 2 left 2 bytes |
|
452 | - // shift row 3 left 3 bytes |
|
453 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | - $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | - $text[12].$text[1].$text[6].$text[11]; |
|
456 | - } |
|
457 | - else // parent::DECRYPT |
|
458 | - { |
|
459 | - // create a 4x4 matrix |
|
460 | - // row 0 is unchanged, |
|
461 | - // shift row 1 right 1 byte |
|
462 | - // shift row 2 right 2 bytes |
|
463 | - // shift row 3 right 3 bytes |
|
464 | - $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
465 | - $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
466 | - $text[12].$text[9].$text[6].$text[3]; |
|
467 | - } |
|
468 | - } |
|
469 | - |
|
470 | - /* |
|
444 | + if($this->blockSize() == 16) |
|
445 | + { |
|
446 | + if($this->operation() == parent::ENCRYPT) |
|
447 | + { |
|
448 | + // create a 4x4 matrix |
|
449 | + // row 0 is unchanged, |
|
450 | + // shift row 1 left 1 byte |
|
451 | + // shift row 2 left 2 bytes |
|
452 | + // shift row 3 left 3 bytes |
|
453 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | + $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | + $text[12].$text[1].$text[6].$text[11]; |
|
456 | + } |
|
457 | + else // parent::DECRYPT |
|
458 | + { |
|
459 | + // create a 4x4 matrix |
|
460 | + // row 0 is unchanged, |
|
461 | + // shift row 1 right 1 byte |
|
462 | + // shift row 2 right 2 bytes |
|
463 | + // shift row 3 right 3 bytes |
|
464 | + $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
465 | + $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
466 | + $text[12].$text[9].$text[6].$text[3]; |
|
467 | + } |
|
468 | + } |
|
469 | + |
|
470 | + /* |
|
471 | 471 | * Rijndael-192 |
472 | 472 | */ |
473 | - if($this->blockSize() == 24) |
|
474 | - { |
|
475 | - if($this->operation() == parent::ENCRYPT) |
|
476 | - { |
|
477 | - // create a 6x4 matrix |
|
478 | - // row 0 is unchanged |
|
479 | - // shift row 1 left 1 byte |
|
480 | - // shift row 2 left 2 bytes |
|
481 | - // shift row 3 left 3 bytes |
|
482 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
483 | - $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
484 | - $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
485 | - $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
486 | - |
|
487 | - } |
|
488 | - else // parent::DECRYPT |
|
489 | - { |
|
490 | - // create a 6x4 matrix |
|
491 | - // row 0 is unchanged |
|
492 | - // shift row 1 right 1 byte |
|
493 | - // shift row 2 right 2 bytes |
|
494 | - // shift row 3 right 3 bytes |
|
495 | - $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
496 | - $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
497 | - $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
498 | - $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
499 | - } |
|
500 | - } |
|
501 | - |
|
502 | - /* |
|
473 | + if($this->blockSize() == 24) |
|
474 | + { |
|
475 | + if($this->operation() == parent::ENCRYPT) |
|
476 | + { |
|
477 | + // create a 6x4 matrix |
|
478 | + // row 0 is unchanged |
|
479 | + // shift row 1 left 1 byte |
|
480 | + // shift row 2 left 2 bytes |
|
481 | + // shift row 3 left 3 bytes |
|
482 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
483 | + $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
484 | + $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
485 | + $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
486 | + |
|
487 | + } |
|
488 | + else // parent::DECRYPT |
|
489 | + { |
|
490 | + // create a 6x4 matrix |
|
491 | + // row 0 is unchanged |
|
492 | + // shift row 1 right 1 byte |
|
493 | + // shift row 2 right 2 bytes |
|
494 | + // shift row 3 right 3 bytes |
|
495 | + $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
496 | + $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
497 | + $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
498 | + $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
499 | + } |
|
500 | + } |
|
501 | + |
|
502 | + /* |
|
503 | 503 | * Rijndael-256 |
504 | 504 | */ |
505 | - if($this->blockSize() == 32) |
|
506 | - { |
|
507 | - if($this->operation() == parent::ENCRYPT) |
|
508 | - { |
|
509 | - // create an 8x4 matrix |
|
510 | - // row 0 is unchanged |
|
511 | - // shift row 1 left 1 byte |
|
512 | - // shift row 2 left 3 bytes |
|
513 | - // shift row 3 left 4 bytes |
|
514 | - $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
515 | - $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
516 | - $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
517 | - $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
518 | - $text[28].$text[1].$text[10].$text[15]; |
|
519 | - } |
|
520 | - else // parent::DECRYPT |
|
521 | - { |
|
522 | - // create an 8x4 matrix |
|
523 | - // row 0 is unchanged |
|
524 | - // shift row 1 right 1 byte |
|
525 | - // shift row 2 right 3 bytes |
|
526 | - // shift row 3 right 4 bytes |
|
527 | - $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
528 | - $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
529 | - $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
530 | - $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
531 | - $text[28].$text[25].$text[18].$text[15]; |
|
532 | - } |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * Applies the Sbox to each byte of the string passed in. |
|
539 | - * This is similar to subByte(), but Unlike subByte() we do not use |
|
540 | - * the _s_inv[] table. This function is only used in expandKey(), |
|
541 | - * which is implemented by the class that inherits this class |
|
542 | - * |
|
543 | - * @param string $text The string to peform the byte subsitution |
|
544 | - * @return string The string with the subsituted bytes |
|
545 | - */ |
|
546 | - private function subWord(&$text) |
|
547 | - { |
|
548 | - $max = strlen($text); |
|
549 | - for($i = 0; $i < $max; ++$i) |
|
550 | - { |
|
551 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
552 | - // and column is numbered in hex (from 0 - f) |
|
553 | - $hex = parent::str2Hex($text[$i]); |
|
554 | - $row = parent::hex2Dec($hex[0]); |
|
555 | - $col = parent::hex2Dec($hex[1]); |
|
556 | - $pos = ($row * 16) + $col; |
|
557 | - |
|
558 | - $text[$i] = chr(self::$_s[$pos]); |
|
559 | - } |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * Rotate a 4 byte block of the key, moving the first byte to |
|
565 | - * to the end, and shifting everything left |
|
566 | - * Used in key Expandsion |
|
567 | - * |
|
568 | - * @param string $key_block A 4 byte string |
|
569 | - * @return string The shifted 4 byte string |
|
570 | - */ |
|
571 | - private function rotWord($key_block) |
|
572 | - { |
|
573 | - return substr($key_block, 1, 3).$key_block[0]; |
|
574 | - } |
|
575 | - |
|
576 | - |
|
577 | - /** |
|
578 | - * Returns 4 bytes from the expanded key starting at the given offset |
|
579 | - * Used during expandKey() |
|
580 | - * |
|
581 | - * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
582 | - * @return string A 4 byte string from the key |
|
583 | - */ |
|
584 | - private function ek($offset) |
|
585 | - { |
|
586 | - return substr($this->xkey, $offset, 4); |
|
587 | - } |
|
588 | - |
|
589 | - |
|
590 | - /** |
|
591 | - * Returns 4 bytes of the original key from the given offset |
|
592 | - * Used during expandKey() |
|
593 | - * |
|
594 | - * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
595 | - * @return string A 4 byte string from the key |
|
596 | - */ |
|
597 | - private function k($offset) |
|
598 | - { |
|
599 | - return substr($this->key(), $offset, 4); |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - /** |
|
604 | - * Return the 4 byte round constant used during expandKey(). |
|
605 | - * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
606 | - * 0x01000000 to create a 4 byte value |
|
607 | - * |
|
608 | - * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
609 | - * @return integer A 4 byte value |
|
610 | - */ |
|
611 | - private function rcon($pos) |
|
612 | - { |
|
613 | - return (self::$_rcon[$pos] * 0x01000000); |
|
614 | - } |
|
615 | - |
|
616 | - |
|
617 | - /** |
|
618 | - * Expands the key |
|
619 | - * The key expands based on the block size as well as the key size |
|
620 | - * |
|
621 | - * @return void |
|
622 | - */ |
|
623 | - protected function expandKey() |
|
624 | - { |
|
625 | - if($this->keySize() == 16) |
|
626 | - return $this->expandKey128(); |
|
627 | - else if($this->keySize() == 24) |
|
628 | - return $this->expandKey192(); |
|
629 | - else if($this->keySize() == 32) |
|
630 | - return $this->expandKey256(); |
|
631 | - } |
|
632 | - |
|
633 | - |
|
634 | - /** |
|
635 | - * Expand a 16 byte key, the size it is expanded to varies |
|
636 | - * based on the block size of the Rijndael implementation chosen |
|
637 | - * |
|
638 | - * @return void |
|
639 | - */ |
|
640 | - private function expandKey128() |
|
641 | - { |
|
642 | - // clear the xkey, we're creating a new one |
|
643 | - $this->xkey = ""; |
|
644 | - $max = 0; |
|
645 | - |
|
646 | - // the number of rounds we make depends on the block size of the text |
|
647 | - // used during encryption/decryption |
|
648 | - if($this->blockSize() == 16) |
|
649 | - $max = 44; |
|
650 | - if($this->blockSize() == 24) |
|
651 | - $max = 78; |
|
652 | - if($this->blockSize() == 32) |
|
653 | - $max = 120; |
|
654 | - |
|
655 | - // 16 byte key expands to 176 bytes |
|
656 | - for($i = 0; $i < $max; ++$i) |
|
657 | - { |
|
658 | - if($i >= 0 && $i <= 3) |
|
659 | - $this->xkey .= $this->k($i * 4); |
|
660 | - else if(($i % 4) == 0) |
|
661 | - { |
|
662 | - // rotate the 4 bytes |
|
663 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
664 | - |
|
665 | - // apply the sbox |
|
666 | - $this->subWord($subword); |
|
667 | - |
|
668 | - // return 4 byte value based on self::$_rcon table |
|
669 | - //$rcon = $this->rcon(($i / 4) - 1); |
|
670 | - $rcon = $this->rcon(($i / 4)); |
|
671 | - |
|
672 | - // grab 4 bytes from $this->extended_key |
|
673 | - $ek = $this->ek(($i - 4) * 4); |
|
674 | - |
|
675 | - $h1 = parent::str2Hex($subword); |
|
676 | - $h2 = parent::dec2Hex($rcon); |
|
677 | - $h3 = parent::str2Hex($ek); |
|
678 | - $res = parent::xorHex($h1, $h2, $h3); |
|
679 | - $this->xkey .= parent::hex2Str($res); |
|
680 | - } |
|
681 | - else |
|
682 | - { |
|
683 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
684 | - $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
685 | - $res = parent::xorHex($h1, $h2); |
|
686 | - $this->xkey .= parent::hex2Str($res); |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - return true; |
|
691 | - } |
|
692 | - |
|
693 | - |
|
694 | - /** |
|
695 | - * Expand a 24 byte key, the size it is expanded to varies |
|
696 | - * based on the block size of the Rijndael implementation chosen |
|
697 | - * |
|
698 | - * @return void |
|
699 | - */ |
|
700 | - private function expandKey192() |
|
701 | - { |
|
702 | - // clear the xkey, we're creating a new one |
|
703 | - $this->xkey = ""; |
|
704 | - $max = 0; |
|
705 | - |
|
706 | - // the number of rounds we make depends on the block size of the text |
|
707 | - // used during encryption/decryption |
|
708 | - if($this->blockSize() == 16) |
|
709 | - $max = 52; |
|
710 | - if($this->blockSize() == 24) |
|
711 | - $max = 78; |
|
712 | - if($this->blockSize() == 32) |
|
713 | - $max = 120; |
|
714 | - |
|
715 | - // 24 byte key expands to 208 bytes |
|
716 | - for($i = 0; $i < $max; ++$i) |
|
717 | - { |
|
718 | - if($i >= 0 && $i <= 5) |
|
719 | - $this->xkey .= $this->k($i * 4); |
|
720 | - else if(($i % 6) == 0) |
|
721 | - { |
|
722 | - // rotate the 4 bytes |
|
723 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
724 | - |
|
725 | - // apply the sbox |
|
726 | - $this->subWord($subword); |
|
727 | - |
|
728 | - // return 4 byte value based on self::$_rcon table |
|
729 | - //$rcon = $this->rcon(($i / 6) - 1); |
|
730 | - $rcon = $this->rcon(($i / 6)); |
|
731 | - |
|
732 | - // grab 4 bytes from $this->extended_key |
|
733 | - $ek = $this->ek(($i - 6) * 4); |
|
734 | - |
|
735 | - $h1 = parent::str2Hex($subword); |
|
736 | - $h2 = parent::dec2Hex($rcon); |
|
737 | - $h3 = parent::str2Hex($ek); |
|
738 | - $res = parent::xorHex($h1, $h2, $h3); |
|
739 | - $this->xkey .= parent::hex2Str($res); |
|
740 | - } |
|
741 | - else |
|
742 | - { |
|
743 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
744 | - $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
745 | - $res = parent::xorHex($h1, $h2); |
|
746 | - $this->xkey .= parent::hex2Str($res); |
|
747 | - } |
|
748 | - } |
|
749 | - |
|
750 | - return true; |
|
751 | - } |
|
752 | - |
|
753 | - |
|
754 | - /** |
|
755 | - * Expand a 32 byte key, the size it is expanded to varies |
|
756 | - * based on the block size of the Rijndael implementation chosen |
|
757 | - * |
|
758 | - * @return void |
|
759 | - */ |
|
760 | - private function expandKey256() |
|
761 | - { |
|
762 | - // clear the xkey, we're creating a new one |
|
763 | - $this->xkey = ""; |
|
764 | - $max = 0; |
|
765 | - |
|
766 | - // the number of rounds we make depends on the block size of the text |
|
767 | - // used during encryption/decryption |
|
768 | - if($this->blockSize() == 16) |
|
769 | - $max = 60; |
|
770 | - if($this->blockSize() == 24) |
|
771 | - $max = 90; |
|
772 | - if($this->blockSize() == 32) |
|
773 | - $max = 120; |
|
774 | - |
|
775 | - // 32 byte key expands to 240 bytes |
|
776 | - for($i = 0; $i < $max; ++$i) |
|
777 | - { |
|
778 | - if($i >= 0 && $i <= 7) |
|
779 | - $this->xkey .= $this->k($i * 4); |
|
780 | - else if($i % 8 == 0) |
|
781 | - { |
|
782 | - // rotate the 4 bytes |
|
783 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
784 | - |
|
785 | - // apply the sbox |
|
786 | - $this->subWord($subword); |
|
787 | - |
|
788 | - // return 4 byte value based on self::$_rcon table |
|
789 | - $rcon = $this->rcon(($i / 8)); |
|
790 | - |
|
791 | - // grab 4 bytes from $this->extended_key |
|
792 | - $ek = $this->ek(($i - 8) * 4); |
|
793 | - |
|
794 | - $h1 = parent::str2Hex($subword); |
|
795 | - $h2 = parent::dec2Hex($rcon); |
|
796 | - $h3 = parent::str2Hex($ek); |
|
797 | - $res = parent::xorHex($h1, $h2, $h3); |
|
798 | - $this->xkey .= parent::hex2Str($res); |
|
799 | - } |
|
800 | - else if($i % 4 == 0) |
|
801 | - { |
|
802 | - // get the subsitution from the s-box |
|
803 | - $subword = $this->ek(($i - 1) * 4); |
|
804 | - $this->subWord($subword); |
|
805 | - |
|
806 | - // get the extended key part |
|
807 | - $ek = $this->ek(($i - 8) * 4); |
|
808 | - |
|
809 | - // xor the two parts |
|
810 | - $h1 = parent::str2Hex($subword); |
|
811 | - $h2 = parent::str2Hex($ek); |
|
812 | - $res = parent::xorHex($h1, $h2); |
|
813 | - $this->xkey .= parent::hex2Str($res); |
|
814 | - } |
|
815 | - else |
|
816 | - { |
|
817 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
818 | - $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
819 | - $res = parent::xorHex($h1, $h2); |
|
820 | - $this->xkey .= parent::hex2Str($res); |
|
821 | - } |
|
822 | - } |
|
823 | - |
|
824 | - return true; |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * Initalizes the tables used for Rijndael/AES encryption |
|
830 | - * |
|
831 | - * @return void |
|
832 | - */ |
|
833 | - private function initTables() |
|
834 | - { |
|
835 | - // the sbox used for encryption |
|
836 | - self::$_s = array( |
|
837 | - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
838 | - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
839 | - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
840 | - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
841 | - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
842 | - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
843 | - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
844 | - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
845 | - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
846 | - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
847 | - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
848 | - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
849 | - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
850 | - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
851 | - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
852 | - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
853 | - ); |
|
854 | - |
|
855 | - // the inverse sbox used for decryption |
|
856 | - self::$_s_inv = array( |
|
857 | - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
858 | - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
859 | - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
860 | - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
861 | - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
862 | - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
863 | - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
864 | - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
865 | - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
866 | - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
867 | - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
868 | - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
869 | - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
870 | - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
871 | - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
872 | - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
873 | - ); |
|
874 | - |
|
875 | - // used in mixColumn() during encryption |
|
876 | - self::$_matrix_mult = array( |
|
877 | - 0x02, 0x01, 0x01, 0x03, |
|
878 | - 0x03, 0x02, 0x01, 0x01, |
|
879 | - 0x01, 0x03, 0x02, 0x01, |
|
880 | - 0x01, 0x01, 0x03, 0x02 |
|
881 | - ); |
|
882 | - |
|
883 | - // used in mixColumn() during decryption |
|
884 | - self::$_matrix_mult_inv = array( |
|
885 | - 0x0e, 0x09, 0x0d, 0x0b, |
|
886 | - 0x0b, 0x0e, 0x09, 0x0d, |
|
887 | - 0x0d, 0x0b, 0x0e, 0x09, |
|
888 | - 0x09, 0x0d, 0x0b, 0x0e |
|
889 | - ); |
|
890 | - |
|
891 | - // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
892 | - // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
893 | - // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
894 | - // the other values are used for larger block/key combinations supported by Rijndael |
|
895 | - // NOTE: self::$_rcon[0] is never used |
|
896 | - self::$_rcon = array( |
|
897 | - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
898 | - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
899 | - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
900 | - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
901 | - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
902 | - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
903 | - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
904 | - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
905 | - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
906 | - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
907 | - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
908 | - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
909 | - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
910 | - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
911 | - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
912 | - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
913 | - ); |
|
914 | - |
|
915 | - /* |
|
505 | + if($this->blockSize() == 32) |
|
506 | + { |
|
507 | + if($this->operation() == parent::ENCRYPT) |
|
508 | + { |
|
509 | + // create an 8x4 matrix |
|
510 | + // row 0 is unchanged |
|
511 | + // shift row 1 left 1 byte |
|
512 | + // shift row 2 left 3 bytes |
|
513 | + // shift row 3 left 4 bytes |
|
514 | + $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
515 | + $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
516 | + $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
517 | + $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
518 | + $text[28].$text[1].$text[10].$text[15]; |
|
519 | + } |
|
520 | + else // parent::DECRYPT |
|
521 | + { |
|
522 | + // create an 8x4 matrix |
|
523 | + // row 0 is unchanged |
|
524 | + // shift row 1 right 1 byte |
|
525 | + // shift row 2 right 3 bytes |
|
526 | + // shift row 3 right 4 bytes |
|
527 | + $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
528 | + $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
529 | + $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
530 | + $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
531 | + $text[28].$text[25].$text[18].$text[15]; |
|
532 | + } |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * Applies the Sbox to each byte of the string passed in. |
|
539 | + * This is similar to subByte(), but Unlike subByte() we do not use |
|
540 | + * the _s_inv[] table. This function is only used in expandKey(), |
|
541 | + * which is implemented by the class that inherits this class |
|
542 | + * |
|
543 | + * @param string $text The string to peform the byte subsitution |
|
544 | + * @return string The string with the subsituted bytes |
|
545 | + */ |
|
546 | + private function subWord(&$text) |
|
547 | + { |
|
548 | + $max = strlen($text); |
|
549 | + for($i = 0; $i < $max; ++$i) |
|
550 | + { |
|
551 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
552 | + // and column is numbered in hex (from 0 - f) |
|
553 | + $hex = parent::str2Hex($text[$i]); |
|
554 | + $row = parent::hex2Dec($hex[0]); |
|
555 | + $col = parent::hex2Dec($hex[1]); |
|
556 | + $pos = ($row * 16) + $col; |
|
557 | + |
|
558 | + $text[$i] = chr(self::$_s[$pos]); |
|
559 | + } |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * Rotate a 4 byte block of the key, moving the first byte to |
|
565 | + * to the end, and shifting everything left |
|
566 | + * Used in key Expandsion |
|
567 | + * |
|
568 | + * @param string $key_block A 4 byte string |
|
569 | + * @return string The shifted 4 byte string |
|
570 | + */ |
|
571 | + private function rotWord($key_block) |
|
572 | + { |
|
573 | + return substr($key_block, 1, 3).$key_block[0]; |
|
574 | + } |
|
575 | + |
|
576 | + |
|
577 | + /** |
|
578 | + * Returns 4 bytes from the expanded key starting at the given offset |
|
579 | + * Used during expandKey() |
|
580 | + * |
|
581 | + * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
582 | + * @return string A 4 byte string from the key |
|
583 | + */ |
|
584 | + private function ek($offset) |
|
585 | + { |
|
586 | + return substr($this->xkey, $offset, 4); |
|
587 | + } |
|
588 | + |
|
589 | + |
|
590 | + /** |
|
591 | + * Returns 4 bytes of the original key from the given offset |
|
592 | + * Used during expandKey() |
|
593 | + * |
|
594 | + * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
595 | + * @return string A 4 byte string from the key |
|
596 | + */ |
|
597 | + private function k($offset) |
|
598 | + { |
|
599 | + return substr($this->key(), $offset, 4); |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + /** |
|
604 | + * Return the 4 byte round constant used during expandKey(). |
|
605 | + * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
606 | + * 0x01000000 to create a 4 byte value |
|
607 | + * |
|
608 | + * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
609 | + * @return integer A 4 byte value |
|
610 | + */ |
|
611 | + private function rcon($pos) |
|
612 | + { |
|
613 | + return (self::$_rcon[$pos] * 0x01000000); |
|
614 | + } |
|
615 | + |
|
616 | + |
|
617 | + /** |
|
618 | + * Expands the key |
|
619 | + * The key expands based on the block size as well as the key size |
|
620 | + * |
|
621 | + * @return void |
|
622 | + */ |
|
623 | + protected function expandKey() |
|
624 | + { |
|
625 | + if($this->keySize() == 16) |
|
626 | + return $this->expandKey128(); |
|
627 | + else if($this->keySize() == 24) |
|
628 | + return $this->expandKey192(); |
|
629 | + else if($this->keySize() == 32) |
|
630 | + return $this->expandKey256(); |
|
631 | + } |
|
632 | + |
|
633 | + |
|
634 | + /** |
|
635 | + * Expand a 16 byte key, the size it is expanded to varies |
|
636 | + * based on the block size of the Rijndael implementation chosen |
|
637 | + * |
|
638 | + * @return void |
|
639 | + */ |
|
640 | + private function expandKey128() |
|
641 | + { |
|
642 | + // clear the xkey, we're creating a new one |
|
643 | + $this->xkey = ""; |
|
644 | + $max = 0; |
|
645 | + |
|
646 | + // the number of rounds we make depends on the block size of the text |
|
647 | + // used during encryption/decryption |
|
648 | + if($this->blockSize() == 16) |
|
649 | + $max = 44; |
|
650 | + if($this->blockSize() == 24) |
|
651 | + $max = 78; |
|
652 | + if($this->blockSize() == 32) |
|
653 | + $max = 120; |
|
654 | + |
|
655 | + // 16 byte key expands to 176 bytes |
|
656 | + for($i = 0; $i < $max; ++$i) |
|
657 | + { |
|
658 | + if($i >= 0 && $i <= 3) |
|
659 | + $this->xkey .= $this->k($i * 4); |
|
660 | + else if(($i % 4) == 0) |
|
661 | + { |
|
662 | + // rotate the 4 bytes |
|
663 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
664 | + |
|
665 | + // apply the sbox |
|
666 | + $this->subWord($subword); |
|
667 | + |
|
668 | + // return 4 byte value based on self::$_rcon table |
|
669 | + //$rcon = $this->rcon(($i / 4) - 1); |
|
670 | + $rcon = $this->rcon(($i / 4)); |
|
671 | + |
|
672 | + // grab 4 bytes from $this->extended_key |
|
673 | + $ek = $this->ek(($i - 4) * 4); |
|
674 | + |
|
675 | + $h1 = parent::str2Hex($subword); |
|
676 | + $h2 = parent::dec2Hex($rcon); |
|
677 | + $h3 = parent::str2Hex($ek); |
|
678 | + $res = parent::xorHex($h1, $h2, $h3); |
|
679 | + $this->xkey .= parent::hex2Str($res); |
|
680 | + } |
|
681 | + else |
|
682 | + { |
|
683 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
684 | + $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
685 | + $res = parent::xorHex($h1, $h2); |
|
686 | + $this->xkey .= parent::hex2Str($res); |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + return true; |
|
691 | + } |
|
692 | + |
|
693 | + |
|
694 | + /** |
|
695 | + * Expand a 24 byte key, the size it is expanded to varies |
|
696 | + * based on the block size of the Rijndael implementation chosen |
|
697 | + * |
|
698 | + * @return void |
|
699 | + */ |
|
700 | + private function expandKey192() |
|
701 | + { |
|
702 | + // clear the xkey, we're creating a new one |
|
703 | + $this->xkey = ""; |
|
704 | + $max = 0; |
|
705 | + |
|
706 | + // the number of rounds we make depends on the block size of the text |
|
707 | + // used during encryption/decryption |
|
708 | + if($this->blockSize() == 16) |
|
709 | + $max = 52; |
|
710 | + if($this->blockSize() == 24) |
|
711 | + $max = 78; |
|
712 | + if($this->blockSize() == 32) |
|
713 | + $max = 120; |
|
714 | + |
|
715 | + // 24 byte key expands to 208 bytes |
|
716 | + for($i = 0; $i < $max; ++$i) |
|
717 | + { |
|
718 | + if($i >= 0 && $i <= 5) |
|
719 | + $this->xkey .= $this->k($i * 4); |
|
720 | + else if(($i % 6) == 0) |
|
721 | + { |
|
722 | + // rotate the 4 bytes |
|
723 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
724 | + |
|
725 | + // apply the sbox |
|
726 | + $this->subWord($subword); |
|
727 | + |
|
728 | + // return 4 byte value based on self::$_rcon table |
|
729 | + //$rcon = $this->rcon(($i / 6) - 1); |
|
730 | + $rcon = $this->rcon(($i / 6)); |
|
731 | + |
|
732 | + // grab 4 bytes from $this->extended_key |
|
733 | + $ek = $this->ek(($i - 6) * 4); |
|
734 | + |
|
735 | + $h1 = parent::str2Hex($subword); |
|
736 | + $h2 = parent::dec2Hex($rcon); |
|
737 | + $h3 = parent::str2Hex($ek); |
|
738 | + $res = parent::xorHex($h1, $h2, $h3); |
|
739 | + $this->xkey .= parent::hex2Str($res); |
|
740 | + } |
|
741 | + else |
|
742 | + { |
|
743 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
744 | + $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
745 | + $res = parent::xorHex($h1, $h2); |
|
746 | + $this->xkey .= parent::hex2Str($res); |
|
747 | + } |
|
748 | + } |
|
749 | + |
|
750 | + return true; |
|
751 | + } |
|
752 | + |
|
753 | + |
|
754 | + /** |
|
755 | + * Expand a 32 byte key, the size it is expanded to varies |
|
756 | + * based on the block size of the Rijndael implementation chosen |
|
757 | + * |
|
758 | + * @return void |
|
759 | + */ |
|
760 | + private function expandKey256() |
|
761 | + { |
|
762 | + // clear the xkey, we're creating a new one |
|
763 | + $this->xkey = ""; |
|
764 | + $max = 0; |
|
765 | + |
|
766 | + // the number of rounds we make depends on the block size of the text |
|
767 | + // used during encryption/decryption |
|
768 | + if($this->blockSize() == 16) |
|
769 | + $max = 60; |
|
770 | + if($this->blockSize() == 24) |
|
771 | + $max = 90; |
|
772 | + if($this->blockSize() == 32) |
|
773 | + $max = 120; |
|
774 | + |
|
775 | + // 32 byte key expands to 240 bytes |
|
776 | + for($i = 0; $i < $max; ++$i) |
|
777 | + { |
|
778 | + if($i >= 0 && $i <= 7) |
|
779 | + $this->xkey .= $this->k($i * 4); |
|
780 | + else if($i % 8 == 0) |
|
781 | + { |
|
782 | + // rotate the 4 bytes |
|
783 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
784 | + |
|
785 | + // apply the sbox |
|
786 | + $this->subWord($subword); |
|
787 | + |
|
788 | + // return 4 byte value based on self::$_rcon table |
|
789 | + $rcon = $this->rcon(($i / 8)); |
|
790 | + |
|
791 | + // grab 4 bytes from $this->extended_key |
|
792 | + $ek = $this->ek(($i - 8) * 4); |
|
793 | + |
|
794 | + $h1 = parent::str2Hex($subword); |
|
795 | + $h2 = parent::dec2Hex($rcon); |
|
796 | + $h3 = parent::str2Hex($ek); |
|
797 | + $res = parent::xorHex($h1, $h2, $h3); |
|
798 | + $this->xkey .= parent::hex2Str($res); |
|
799 | + } |
|
800 | + else if($i % 4 == 0) |
|
801 | + { |
|
802 | + // get the subsitution from the s-box |
|
803 | + $subword = $this->ek(($i - 1) * 4); |
|
804 | + $this->subWord($subword); |
|
805 | + |
|
806 | + // get the extended key part |
|
807 | + $ek = $this->ek(($i - 8) * 4); |
|
808 | + |
|
809 | + // xor the two parts |
|
810 | + $h1 = parent::str2Hex($subword); |
|
811 | + $h2 = parent::str2Hex($ek); |
|
812 | + $res = parent::xorHex($h1, $h2); |
|
813 | + $this->xkey .= parent::hex2Str($res); |
|
814 | + } |
|
815 | + else |
|
816 | + { |
|
817 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
818 | + $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
819 | + $res = parent::xorHex($h1, $h2); |
|
820 | + $this->xkey .= parent::hex2Str($res); |
|
821 | + } |
|
822 | + } |
|
823 | + |
|
824 | + return true; |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * Initalizes the tables used for Rijndael/AES encryption |
|
830 | + * |
|
831 | + * @return void |
|
832 | + */ |
|
833 | + private function initTables() |
|
834 | + { |
|
835 | + // the sbox used for encryption |
|
836 | + self::$_s = array( |
|
837 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
838 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
839 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
840 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
841 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
842 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
843 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
844 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
845 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
846 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
847 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
848 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
849 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
850 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
851 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
852 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
853 | + ); |
|
854 | + |
|
855 | + // the inverse sbox used for decryption |
|
856 | + self::$_s_inv = array( |
|
857 | + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
858 | + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
859 | + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
860 | + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
861 | + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
862 | + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
863 | + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
864 | + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
865 | + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
866 | + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
867 | + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
868 | + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
869 | + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
870 | + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
871 | + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
872 | + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
873 | + ); |
|
874 | + |
|
875 | + // used in mixColumn() during encryption |
|
876 | + self::$_matrix_mult = array( |
|
877 | + 0x02, 0x01, 0x01, 0x03, |
|
878 | + 0x03, 0x02, 0x01, 0x01, |
|
879 | + 0x01, 0x03, 0x02, 0x01, |
|
880 | + 0x01, 0x01, 0x03, 0x02 |
|
881 | + ); |
|
882 | + |
|
883 | + // used in mixColumn() during decryption |
|
884 | + self::$_matrix_mult_inv = array( |
|
885 | + 0x0e, 0x09, 0x0d, 0x0b, |
|
886 | + 0x0b, 0x0e, 0x09, 0x0d, |
|
887 | + 0x0d, 0x0b, 0x0e, 0x09, |
|
888 | + 0x09, 0x0d, 0x0b, 0x0e |
|
889 | + ); |
|
890 | + |
|
891 | + // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
892 | + // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
893 | + // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
894 | + // the other values are used for larger block/key combinations supported by Rijndael |
|
895 | + // NOTE: self::$_rcon[0] is never used |
|
896 | + self::$_rcon = array( |
|
897 | + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
898 | + 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
899 | + 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
900 | + 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
901 | + 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
902 | + 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
903 | + 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
904 | + 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
905 | + 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
906 | + 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
907 | + 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
908 | + 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
909 | + 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
910 | + 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
911 | + 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
912 | + 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
913 | + ); |
|
914 | + |
|
915 | + /* |
|
916 | 916 | * Galois Multiplication lookup tables |
917 | 917 | * See http://en.wikipedia.org/wiki/Rijndael_mix_columns#InverseMixColumns |
918 | 918 | */ |
919 | 919 | |
920 | - // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
921 | - self::$_gm2 = array( |
|
922 | - 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
923 | - 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
924 | - 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
925 | - 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
926 | - 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
927 | - 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
928 | - 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
929 | - 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
930 | - 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
931 | - 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
932 | - 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
933 | - 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
934 | - 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
935 | - 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
936 | - 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
937 | - 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
938 | - ); |
|
939 | - |
|
940 | - // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
941 | - self::$_gm3 = array( |
|
942 | - 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
943 | - 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
944 | - 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
945 | - 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
946 | - 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
947 | - 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
948 | - 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
949 | - 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
950 | - 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
951 | - 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
952 | - 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
953 | - 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
954 | - 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
955 | - 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
956 | - 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
957 | - 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
958 | - ); |
|
959 | - |
|
960 | - // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
961 | - self::$_gm9 = array( |
|
962 | - 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
963 | - 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
964 | - 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
965 | - 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
966 | - 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
967 | - 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
968 | - 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
969 | - 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
970 | - 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
971 | - 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
972 | - 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
973 | - 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
974 | - 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
975 | - 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
976 | - 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
977 | - 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
978 | - ); |
|
979 | - |
|
980 | - // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
981 | - self::$_gm11 = array( |
|
982 | - 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
983 | - 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
984 | - 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
985 | - 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
986 | - 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
987 | - 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
988 | - 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
989 | - 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
990 | - 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
991 | - 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
992 | - 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
993 | - 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
994 | - 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
995 | - 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
996 | - 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
997 | - 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
998 | - ); |
|
999 | - |
|
1000 | - // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
1001 | - self::$_gm13 = array( |
|
1002 | - 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
1003 | - 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
1004 | - 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
1005 | - 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1006 | - 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1007 | - 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1008 | - 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1009 | - 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1010 | - 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1011 | - 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1012 | - 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1013 | - 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1014 | - 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1015 | - 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1016 | - 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1017 | - 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1018 | - ); |
|
1019 | - |
|
1020 | - // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1021 | - self::$_gm14 = array( |
|
1022 | - 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1023 | - 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1024 | - 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1025 | - 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1026 | - 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1027 | - 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1028 | - 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1029 | - 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1030 | - 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1031 | - 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1032 | - 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1033 | - 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1034 | - 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1035 | - 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1036 | - 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1037 | - 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1038 | - ); |
|
1039 | - } |
|
920 | + // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
921 | + self::$_gm2 = array( |
|
922 | + 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
923 | + 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
924 | + 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
925 | + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
926 | + 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
927 | + 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
928 | + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
929 | + 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
930 | + 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
931 | + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
932 | + 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
933 | + 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
934 | + 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
935 | + 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
936 | + 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
937 | + 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
938 | + ); |
|
939 | + |
|
940 | + // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
941 | + self::$_gm3 = array( |
|
942 | + 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
943 | + 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
944 | + 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
945 | + 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
946 | + 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
947 | + 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
948 | + 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
949 | + 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
950 | + 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
951 | + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
952 | + 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
953 | + 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
954 | + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
955 | + 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
956 | + 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
957 | + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
958 | + ); |
|
959 | + |
|
960 | + // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
961 | + self::$_gm9 = array( |
|
962 | + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
963 | + 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
964 | + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
965 | + 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
966 | + 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
967 | + 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
968 | + 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
969 | + 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
970 | + 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
971 | + 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
972 | + 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
973 | + 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
974 | + 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
975 | + 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
976 | + 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
977 | + 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
978 | + ); |
|
979 | + |
|
980 | + // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
981 | + self::$_gm11 = array( |
|
982 | + 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
983 | + 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
984 | + 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
985 | + 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
986 | + 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
987 | + 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
988 | + 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
989 | + 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
990 | + 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
991 | + 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
992 | + 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
993 | + 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
994 | + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
995 | + 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
996 | + 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
997 | + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
998 | + ); |
|
999 | + |
|
1000 | + // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
1001 | + self::$_gm13 = array( |
|
1002 | + 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
1003 | + 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
1004 | + 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
1005 | + 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1006 | + 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1007 | + 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1008 | + 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1009 | + 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1010 | + 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1011 | + 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1012 | + 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1013 | + 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1014 | + 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1015 | + 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1016 | + 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1017 | + 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1018 | + ); |
|
1019 | + |
|
1020 | + // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1021 | + self::$_gm14 = array( |
|
1022 | + 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1023 | + 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1024 | + 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1025 | + 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1026 | + 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1027 | + 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1028 | + 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1029 | + 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1030 | + 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1031 | + 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1032 | + 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1033 | + 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1034 | + 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1035 | + 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1036 | + 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1037 | + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1038 | + ); |
|
1039 | + } |
|
1040 | 1040 | } |
1041 | 1041 | ?> |
@@ -139,11 +139,11 @@ discard block |
||
139 | 139 | // AES will pass in a $len, since it has a fixed key size, other |
140 | 140 | // rijndael implementations can use variable key sizes, supported |
141 | 141 | // sizes are stored in self::$_key_sizes |
142 | - if($len == 0) |
|
142 | + if ($len == 0) |
|
143 | 143 | { |
144 | 144 | // the key must be one of the following lengths: 16, 24, 32 bytes |
145 | 145 | $len = strlen($key); |
146 | - if(!in_array($len, self::$_key_sizes)) |
|
146 | + if (!in_array($len, self::$_key_sizes)) |
|
147 | 147 | { |
148 | 148 | $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
149 | 149 | $msg .= "Received $len bytes."; |
@@ -188,23 +188,23 @@ discard block |
||
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) |
|
191 | + if ($key_sz == 16 && $blk_sz == 16) |
|
192 | 192 | $loops = 10; |
193 | - else if(($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
193 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | 194 | $loops = 12; |
195 | - else if($key_sz == 32 || $blk_sz == 32) |
|
195 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | 196 | $loops = 14; |
197 | 197 | |
198 | 198 | // now begin the encryption |
199 | 199 | $this->addRoundKey($text, 0); |
200 | 200 | |
201 | - for($i = 1; $i <= $loops; ++$i) |
|
201 | + for ($i = 1; $i <= $loops; ++$i) |
|
202 | 202 | { |
203 | 203 | $this->byteSub($text); |
204 | 204 | $this->shiftRow($text); |
205 | 205 | |
206 | 206 | // the last iteration does not use mixColumn |
207 | - if($i < $loops) |
|
207 | + if ($i < $loops) |
|
208 | 208 | $this->mixColumn($text); |
209 | 209 | |
210 | 210 | $this->addRoundKey($text, $i); |
@@ -232,24 +232,24 @@ discard block |
||
232 | 232 | // if the key and block size is 16, do 10 rounds |
233 | 233 | // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
234 | 234 | // if either key or block size is 32, do 14 rounds |
235 | - if($key_sz == 16 && $blk_sz == 16) |
|
235 | + if ($key_sz == 16 && $blk_sz == 16) |
|
236 | 236 | $loops = 10; |
237 | - else if(($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
237 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | 238 | $loops = 12; |
239 | - else if($key_sz == 32 || $blk_sz == 32) |
|
239 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | 240 | $loops = 14; |
241 | 241 | |
242 | 242 | // now begin the decryption |
243 | 243 | $this->addRoundKey($text, 0); |
244 | 244 | |
245 | - for($i = 1; $i <= $loops; ++$i) |
|
245 | + for ($i = 1; $i <= $loops; ++$i) |
|
246 | 246 | { |
247 | 247 | $this->shiftRow($text); |
248 | 248 | $this->byteSub($text); |
249 | 249 | $this->addRoundKey($text, $i); |
250 | 250 | |
251 | 251 | // the last iteration does not use mixColumn |
252 | - if($i < $loops) |
|
252 | + if ($i < $loops) |
|
253 | 253 | $this->mixColumn($text); |
254 | 254 | } |
255 | 255 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | protected function mixColumnMultiply($m, $byte) |
281 | 281 | { |
282 | 282 | // if multiplying by 1, then we just return the same number |
283 | - if($m == 0x01) |
|
283 | + if ($m == 0x01) |
|
284 | 284 | return $byte; |
285 | 285 | |
286 | 286 | $hex = parent::dec2Hex($byte); |
@@ -289,27 +289,27 @@ discard block |
||
289 | 289 | $pos = ($row * 16) + $col; |
290 | 290 | |
291 | 291 | // multiply by 2 (comes from self::$_matrix_mult during encryption) |
292 | - if($m == 0x02) |
|
292 | + if ($m == 0x02) |
|
293 | 293 | return self::$_gm2[$pos]; |
294 | 294 | |
295 | 295 | // multiply by 3 (comes from self::$_matrix_mult during encryption) |
296 | - if($m == 0x03) |
|
296 | + if ($m == 0x03) |
|
297 | 297 | return self::$_gm3[$pos]; |
298 | 298 | |
299 | 299 | // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
300 | - if($m == 0x09) |
|
300 | + if ($m == 0x09) |
|
301 | 301 | return self::$_gm9[$pos]; |
302 | 302 | |
303 | 303 | // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
304 | - if($m == 0x0b) |
|
304 | + if ($m == 0x0b) |
|
305 | 305 | return self::$_gm11[$pos]; |
306 | 306 | |
307 | 307 | // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
308 | - if($m == 0x0d) |
|
308 | + if ($m == 0x0d) |
|
309 | 309 | return self::$_gm13[$pos]; |
310 | 310 | |
311 | 311 | // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
312 | - if($m == 0x0e) |
|
312 | + if ($m == 0x0e) |
|
313 | 313 | return self::$_gm14[$pos]; |
314 | 314 | } |
315 | 315 | |
@@ -331,12 +331,12 @@ discard block |
||
331 | 331 | $ek_len = strlen($this->xkey); |
332 | 332 | $len = $this->blockSize(); |
333 | 333 | |
334 | - if($this->operation() == parent::ENCRYPT) |
|
334 | + if ($this->operation() == parent::ENCRYPT) |
|
335 | 335 | $offset = $round * $len; |
336 | 336 | else |
337 | 337 | $offset = ($ek_len - ($round * $len)) - $len; |
338 | 338 | |
339 | - for($i = 0; $i < $len; ++$i) |
|
339 | + for ($i = 0; $i < $len; ++$i) |
|
340 | 340 | $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
341 | 341 | } |
342 | 342 | |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | private function byteSub(&$text) |
352 | 352 | { |
353 | 353 | $max = strlen($text); |
354 | - for($i = 0; $i < $max; ++$i) |
|
354 | + for ($i = 0; $i < $max; ++$i) |
|
355 | 355 | { |
356 | 356 | // the sbox is arrange in a 16 x 16 grid, where each row |
357 | 357 | // and column is numbered in hex (from 0 - f) |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | $pos = ($row * 16) + $col; |
362 | 362 | |
363 | 363 | // return the corresponding value from the sbox |
364 | - if($this->operation() == parent::ENCRYPT) |
|
364 | + if ($this->operation() == parent::ENCRYPT) |
|
365 | 365 | $text[$i] = chr(self::$_s[$pos]); |
366 | 366 | else // parent::DECRYPT uses the inverse sbox |
367 | 367 | $text[$i] = chr(self::$_s_inv[$pos]); |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | $tmp = $t; |
382 | 382 | |
383 | 383 | // the matrix we use depends on if we are encrypting or decrypting |
384 | - if($this->operation() == parent::ENCRYPT) |
|
384 | + if ($this->operation() == parent::ENCRYPT) |
|
385 | 385 | $m = self::$_matrix_mult; |
386 | 386 | else // parent::DECRYPT |
387 | 387 | $m = self::$_matrix_mult_inv; |
@@ -394,30 +394,30 @@ discard block |
||
394 | 394 | $max_col = ($this->blockSize() * 8) / 32; |
395 | 395 | |
396 | 396 | // loop through each column of the matrix |
397 | - for($col = 0; $col < $max_col; ++$col) |
|
397 | + for ($col = 0; $col < $max_col; ++$col) |
|
398 | 398 | { |
399 | 399 | $pos = $col * 4; |
400 | 400 | |
401 | - $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | - $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | - $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
401 | + $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | + $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | + $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | 404 | $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
405 | 405 | $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
406 | 406 | |
407 | - $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | - $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | - $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
407 | + $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | + $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | + $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | 410 | $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
411 | 411 | $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
412 | 412 | |
413 | - $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | - $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
413 | + $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | + $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | 415 | $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
416 | 416 | $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
417 | 417 | $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
418 | 418 | |
419 | - $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | - $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
419 | + $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | + $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | 421 | $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
422 | 422 | $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
423 | 423 | $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
@@ -441,9 +441,9 @@ discard block |
||
441 | 441 | /* |
442 | 442 | * Rijndael-128 / AES |
443 | 443 | */ |
444 | - if($this->blockSize() == 16) |
|
444 | + if ($this->blockSize() == 16) |
|
445 | 445 | { |
446 | - if($this->operation() == parent::ENCRYPT) |
|
446 | + if ($this->operation() == parent::ENCRYPT) |
|
447 | 447 | { |
448 | 448 | // create a 4x4 matrix |
449 | 449 | // row 0 is unchanged, |
@@ -470,9 +470,9 @@ discard block |
||
470 | 470 | /* |
471 | 471 | * Rijndael-192 |
472 | 472 | */ |
473 | - if($this->blockSize() == 24) |
|
473 | + if ($this->blockSize() == 24) |
|
474 | 474 | { |
475 | - if($this->operation() == parent::ENCRYPT) |
|
475 | + if ($this->operation() == parent::ENCRYPT) |
|
476 | 476 | { |
477 | 477 | // create a 6x4 matrix |
478 | 478 | // row 0 is unchanged |
@@ -502,9 +502,9 @@ discard block |
||
502 | 502 | /* |
503 | 503 | * Rijndael-256 |
504 | 504 | */ |
505 | - if($this->blockSize() == 32) |
|
505 | + if ($this->blockSize() == 32) |
|
506 | 506 | { |
507 | - if($this->operation() == parent::ENCRYPT) |
|
507 | + if ($this->operation() == parent::ENCRYPT) |
|
508 | 508 | { |
509 | 509 | // create an 8x4 matrix |
510 | 510 | // row 0 is unchanged |
@@ -546,7 +546,7 @@ discard block |
||
546 | 546 | private function subWord(&$text) |
547 | 547 | { |
548 | 548 | $max = strlen($text); |
549 | - for($i = 0; $i < $max; ++$i) |
|
549 | + for ($i = 0; $i < $max; ++$i) |
|
550 | 550 | { |
551 | 551 | // the sbox is arrange in a 16 x 16 grid, where each row |
552 | 552 | // and column is numbered in hex (from 0 - f) |
@@ -622,11 +622,11 @@ discard block |
||
622 | 622 | */ |
623 | 623 | protected function expandKey() |
624 | 624 | { |
625 | - if($this->keySize() == 16) |
|
625 | + if ($this->keySize() == 16) |
|
626 | 626 | return $this->expandKey128(); |
627 | - else if($this->keySize() == 24) |
|
627 | + else if ($this->keySize() == 24) |
|
628 | 628 | return $this->expandKey192(); |
629 | - else if($this->keySize() == 32) |
|
629 | + else if ($this->keySize() == 32) |
|
630 | 630 | return $this->expandKey256(); |
631 | 631 | } |
632 | 632 | |
@@ -645,19 +645,19 @@ discard block |
||
645 | 645 | |
646 | 646 | // the number of rounds we make depends on the block size of the text |
647 | 647 | // used during encryption/decryption |
648 | - if($this->blockSize() == 16) |
|
648 | + if ($this->blockSize() == 16) |
|
649 | 649 | $max = 44; |
650 | - if($this->blockSize() == 24) |
|
650 | + if ($this->blockSize() == 24) |
|
651 | 651 | $max = 78; |
652 | - if($this->blockSize() == 32) |
|
652 | + if ($this->blockSize() == 32) |
|
653 | 653 | $max = 120; |
654 | 654 | |
655 | 655 | // 16 byte key expands to 176 bytes |
656 | - for($i = 0; $i < $max; ++$i) |
|
656 | + for ($i = 0; $i < $max; ++$i) |
|
657 | 657 | { |
658 | - if($i >= 0 && $i <= 3) |
|
658 | + if ($i >= 0 && $i <= 3) |
|
659 | 659 | $this->xkey .= $this->k($i * 4); |
660 | - else if(($i % 4) == 0) |
|
660 | + else if (($i % 4) == 0) |
|
661 | 661 | { |
662 | 662 | // rotate the 4 bytes |
663 | 663 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -705,19 +705,19 @@ discard block |
||
705 | 705 | |
706 | 706 | // the number of rounds we make depends on the block size of the text |
707 | 707 | // used during encryption/decryption |
708 | - if($this->blockSize() == 16) |
|
708 | + if ($this->blockSize() == 16) |
|
709 | 709 | $max = 52; |
710 | - if($this->blockSize() == 24) |
|
710 | + if ($this->blockSize() == 24) |
|
711 | 711 | $max = 78; |
712 | - if($this->blockSize() == 32) |
|
712 | + if ($this->blockSize() == 32) |
|
713 | 713 | $max = 120; |
714 | 714 | |
715 | 715 | // 24 byte key expands to 208 bytes |
716 | - for($i = 0; $i < $max; ++$i) |
|
716 | + for ($i = 0; $i < $max; ++$i) |
|
717 | 717 | { |
718 | - if($i >= 0 && $i <= 5) |
|
718 | + if ($i >= 0 && $i <= 5) |
|
719 | 719 | $this->xkey .= $this->k($i * 4); |
720 | - else if(($i % 6) == 0) |
|
720 | + else if (($i % 6) == 0) |
|
721 | 721 | { |
722 | 722 | // rotate the 4 bytes |
723 | 723 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -765,19 +765,19 @@ discard block |
||
765 | 765 | |
766 | 766 | // the number of rounds we make depends on the block size of the text |
767 | 767 | // used during encryption/decryption |
768 | - if($this->blockSize() == 16) |
|
768 | + if ($this->blockSize() == 16) |
|
769 | 769 | $max = 60; |
770 | - if($this->blockSize() == 24) |
|
770 | + if ($this->blockSize() == 24) |
|
771 | 771 | $max = 90; |
772 | - if($this->blockSize() == 32) |
|
772 | + if ($this->blockSize() == 32) |
|
773 | 773 | $max = 120; |
774 | 774 | |
775 | 775 | // 32 byte key expands to 240 bytes |
776 | - for($i = 0; $i < $max; ++$i) |
|
776 | + for ($i = 0; $i < $max; ++$i) |
|
777 | 777 | { |
778 | - if($i >= 0 && $i <= 7) |
|
778 | + if ($i >= 0 && $i <= 7) |
|
779 | 779 | $this->xkey .= $this->k($i * 4); |
780 | - else if($i % 8 == 0) |
|
780 | + else if ($i % 8 == 0) |
|
781 | 781 | { |
782 | 782 | // rotate the 4 bytes |
783 | 783 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -797,7 +797,7 @@ discard block |
||
797 | 797 | $res = parent::xorHex($h1, $h2, $h3); |
798 | 798 | $this->xkey .= parent::hex2Str($res); |
799 | 799 | } |
800 | - else if($i % 4 == 0) |
|
800 | + else if ($i % 4 == 0) |
|
801 | 801 | { |
802 | 802 | // get the subsitution from the s-box |
803 | 803 | $subword = $this->ek(($i - 1) * 4); |
@@ -188,12 +188,13 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
@@ -453,8 +470,7 @@ discard block |
||
453 | 470 | $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
454 | 471 | $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
455 | 472 | $text[12].$text[1].$text[6].$text[11]; |
456 | - } |
|
457 | - else // parent::DECRYPT |
|
473 | + } else // parent::DECRYPT |
|
458 | 474 | { |
459 | 475 | // create a 4x4 matrix |
460 | 476 | // row 0 is unchanged, |
@@ -484,8 +500,7 @@ discard block |
||
484 | 500 | $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
485 | 501 | $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
486 | 502 | |
487 | - } |
|
488 | - else // parent::DECRYPT |
|
503 | + } else // parent::DECRYPT |
|
489 | 504 | { |
490 | 505 | // create a 6x4 matrix |
491 | 506 | // row 0 is unchanged |
@@ -516,8 +531,7 @@ discard block |
||
516 | 531 | $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
517 | 532 | $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
518 | 533 | $text[28].$text[1].$text[10].$text[15]; |
519 | - } |
|
520 | - else // parent::DECRYPT |
|
534 | + } else // parent::DECRYPT |
|
521 | 535 | { |
522 | 536 | // create an 8x4 matrix |
523 | 537 | // row 0 is unchanged |
@@ -622,12 +636,13 @@ discard block |
||
622 | 636 | */ |
623 | 637 | protected function expandKey() |
624 | 638 | { |
625 | - if($this->keySize() == 16) |
|
626 | - return $this->expandKey128(); |
|
627 | - else if($this->keySize() == 24) |
|
628 | - return $this->expandKey192(); |
|
629 | - else if($this->keySize() == 32) |
|
630 | - 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 | + } |
|
631 | 646 | } |
632 | 647 | |
633 | 648 | |
@@ -645,19 +660,22 @@ discard block |
||
645 | 660 | |
646 | 661 | // the number of rounds we make depends on the block size of the text |
647 | 662 | // used during encryption/decryption |
648 | - if($this->blockSize() == 16) |
|
649 | - $max = 44; |
|
650 | - if($this->blockSize() == 24) |
|
651 | - $max = 78; |
|
652 | - if($this->blockSize() == 32) |
|
653 | - $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 | + } |
|
654 | 672 | |
655 | 673 | // 16 byte key expands to 176 bytes |
656 | 674 | for($i = 0; $i < $max; ++$i) |
657 | 675 | { |
658 | - if($i >= 0 && $i <= 3) |
|
659 | - $this->xkey .= $this->k($i * 4); |
|
660 | - else if(($i % 4) == 0) |
|
676 | + if($i >= 0 && $i <= 3) { |
|
677 | + $this->xkey .= $this->k($i * 4); |
|
678 | + } else if(($i % 4) == 0) |
|
661 | 679 | { |
662 | 680 | // rotate the 4 bytes |
663 | 681 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -677,8 +695,7 @@ discard block |
||
677 | 695 | $h3 = parent::str2Hex($ek); |
678 | 696 | $res = parent::xorHex($h1, $h2, $h3); |
679 | 697 | $this->xkey .= parent::hex2Str($res); |
680 | - } |
|
681 | - else |
|
698 | + } else |
|
682 | 699 | { |
683 | 700 | $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
684 | 701 | $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
@@ -705,19 +722,22 @@ discard block |
||
705 | 722 | |
706 | 723 | // the number of rounds we make depends on the block size of the text |
707 | 724 | // used during encryption/decryption |
708 | - if($this->blockSize() == 16) |
|
709 | - $max = 52; |
|
710 | - if($this->blockSize() == 24) |
|
711 | - $max = 78; |
|
712 | - if($this->blockSize() == 32) |
|
713 | - $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 | + } |
|
714 | 734 | |
715 | 735 | // 24 byte key expands to 208 bytes |
716 | 736 | for($i = 0; $i < $max; ++$i) |
717 | 737 | { |
718 | - if($i >= 0 && $i <= 5) |
|
719 | - $this->xkey .= $this->k($i * 4); |
|
720 | - else if(($i % 6) == 0) |
|
738 | + if($i >= 0 && $i <= 5) { |
|
739 | + $this->xkey .= $this->k($i * 4); |
|
740 | + } else if(($i % 6) == 0) |
|
721 | 741 | { |
722 | 742 | // rotate the 4 bytes |
723 | 743 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -737,8 +757,7 @@ discard block |
||
737 | 757 | $h3 = parent::str2Hex($ek); |
738 | 758 | $res = parent::xorHex($h1, $h2, $h3); |
739 | 759 | $this->xkey .= parent::hex2Str($res); |
740 | - } |
|
741 | - else |
|
760 | + } else |
|
742 | 761 | { |
743 | 762 | $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
744 | 763 | $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
@@ -765,19 +784,22 @@ discard block |
||
765 | 784 | |
766 | 785 | // the number of rounds we make depends on the block size of the text |
767 | 786 | // used during encryption/decryption |
768 | - if($this->blockSize() == 16) |
|
769 | - $max = 60; |
|
770 | - if($this->blockSize() == 24) |
|
771 | - $max = 90; |
|
772 | - if($this->blockSize() == 32) |
|
773 | - $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 | + } |
|
774 | 796 | |
775 | 797 | // 32 byte key expands to 240 bytes |
776 | 798 | for($i = 0; $i < $max; ++$i) |
777 | 799 | { |
778 | - if($i >= 0 && $i <= 7) |
|
779 | - $this->xkey .= $this->k($i * 4); |
|
780 | - else if($i % 8 == 0) |
|
800 | + if($i >= 0 && $i <= 7) { |
|
801 | + $this->xkey .= $this->k($i * 4); |
|
802 | + } else if($i % 8 == 0) |
|
781 | 803 | { |
782 | 804 | // rotate the 4 bytes |
783 | 805 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -796,8 +818,7 @@ discard block |
||
796 | 818 | $h3 = parent::str2Hex($ek); |
797 | 819 | $res = parent::xorHex($h1, $h2, $h3); |
798 | 820 | $this->xkey .= parent::hex2Str($res); |
799 | - } |
|
800 | - else if($i % 4 == 0) |
|
821 | + } else if($i % 4 == 0) |
|
801 | 822 | { |
802 | 823 | // get the subsitution from the s-box |
803 | 824 | $subword = $this->ek(($i - 1) * 4); |
@@ -811,8 +832,7 @@ discard block |
||
811 | 832 | $h2 = parent::str2Hex($ek); |
812 | 833 | $res = parent::xorHex($h1, $h2); |
813 | 834 | $this->xkey .= parent::hex2Str($res); |
814 | - } |
|
815 | - else |
|
835 | + } else |
|
816 | 836 | { |
817 | 837 | $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
818 | 838 | $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
@@ -108,7 +108,7 @@ |
||
108 | 108 | * this function contains the code to do both. The SimpleXOR::Encrypt() |
109 | 109 | * and SimpleXOR::Decrypt() function above just call this function |
110 | 110 | * |
111 | - * @param string $input |
|
111 | + * @param string $text |
|
112 | 112 | * @return boolean Always returns true |
113 | 113 | */ |
114 | 114 | private function simpleXOR(&$text) |
@@ -45,101 +45,101 @@ |
||
45 | 45 | */ |
46 | 46 | class Cipher_Simple_XOR extends Cipher |
47 | 47 | { |
48 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
49 | - const BYTES_BLOCK = 1; // 8 bits |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor |
|
54 | - * Sets the key used for encryption. Also sets the requied block size |
|
55 | - * |
|
56 | - * @param string $key string containing the user supplied encryption key |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function __construct($key) |
|
60 | - { |
|
61 | - // SimpleXOR does not have a key size requirement |
|
62 | - parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
63 | - |
|
64 | - // required block size in bits |
|
65 | - $this->blockSize(self::BYTES_BLOCK); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Destructor |
|
71 | - * |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function __destruct() |
|
75 | - { |
|
76 | - parent::__destruct(); |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Encrypts data using an XOR encryption scheme |
|
82 | - * |
|
83 | - * @param string $text A string to encrypt |
|
84 | - * @return boolean Always returns true |
|
85 | - */ |
|
86 | - public function encrypt(&$text) |
|
87 | - { |
|
88 | - $this->operation(parent::ENCRYPT); |
|
89 | - return $this->simpleXOR($text); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
95 | - * |
|
96 | - * @param string $text An encrypted string to decrypt |
|
97 | - * @return boolean Always returns true |
|
98 | - */ |
|
99 | - public function decrypt(&$text) |
|
100 | - { |
|
101 | - $this->operation(parent::DECRYPT); |
|
102 | - return $this->simpleXOR($text); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
108 | - * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
109 | - * and SimpleXOR::Decrypt() function above just call this function |
|
110 | - * |
|
111 | - * @param string $input |
|
112 | - * @return boolean Always returns true |
|
113 | - */ |
|
114 | - private function simpleXOR(&$text) |
|
115 | - { |
|
116 | - $pos = 0; |
|
117 | - $max = strlen($text); |
|
118 | - $key = $this->key(); |
|
119 | - |
|
120 | - for($i = 0; $i < $max; ++$i) |
|
121 | - { |
|
122 | - // if the current position in the key reaches the end of the key, |
|
123 | - // start over at position 0 of the key |
|
124 | - if($pos >= $this->keySize()) |
|
125 | - $pos = 0; |
|
126 | - |
|
127 | - $text[$i] = $text[$i] ^ $key[$pos]; |
|
128 | - ++$pos; |
|
129 | - } |
|
130 | - |
|
131 | - return true; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Indicates that this is stream cipher |
|
137 | - * |
|
138 | - * @return integer Returns Cipher::STREAM |
|
139 | - */ |
|
140 | - public function type() |
|
141 | - { |
|
142 | - return parent::STREAM; |
|
143 | - } |
|
48 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
49 | + const BYTES_BLOCK = 1; // 8 bits |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor |
|
54 | + * Sets the key used for encryption. Also sets the requied block size |
|
55 | + * |
|
56 | + * @param string $key string containing the user supplied encryption key |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function __construct($key) |
|
60 | + { |
|
61 | + // SimpleXOR does not have a key size requirement |
|
62 | + parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
63 | + |
|
64 | + // required block size in bits |
|
65 | + $this->blockSize(self::BYTES_BLOCK); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Destructor |
|
71 | + * |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function __destruct() |
|
75 | + { |
|
76 | + parent::__destruct(); |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Encrypts data using an XOR encryption scheme |
|
82 | + * |
|
83 | + * @param string $text A string to encrypt |
|
84 | + * @return boolean Always returns true |
|
85 | + */ |
|
86 | + public function encrypt(&$text) |
|
87 | + { |
|
88 | + $this->operation(parent::ENCRYPT); |
|
89 | + return $this->simpleXOR($text); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
95 | + * |
|
96 | + * @param string $text An encrypted string to decrypt |
|
97 | + * @return boolean Always returns true |
|
98 | + */ |
|
99 | + public function decrypt(&$text) |
|
100 | + { |
|
101 | + $this->operation(parent::DECRYPT); |
|
102 | + return $this->simpleXOR($text); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
108 | + * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
109 | + * and SimpleXOR::Decrypt() function above just call this function |
|
110 | + * |
|
111 | + * @param string $input |
|
112 | + * @return boolean Always returns true |
|
113 | + */ |
|
114 | + private function simpleXOR(&$text) |
|
115 | + { |
|
116 | + $pos = 0; |
|
117 | + $max = strlen($text); |
|
118 | + $key = $this->key(); |
|
119 | + |
|
120 | + for($i = 0; $i < $max; ++$i) |
|
121 | + { |
|
122 | + // if the current position in the key reaches the end of the key, |
|
123 | + // start over at position 0 of the key |
|
124 | + if($pos >= $this->keySize()) |
|
125 | + $pos = 0; |
|
126 | + |
|
127 | + $text[$i] = $text[$i] ^ $key[$pos]; |
|
128 | + ++$pos; |
|
129 | + } |
|
130 | + |
|
131 | + return true; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Indicates that this is stream cipher |
|
137 | + * |
|
138 | + * @return integer Returns Cipher::STREAM |
|
139 | + */ |
|
140 | + public function type() |
|
141 | + { |
|
142 | + return parent::STREAM; |
|
143 | + } |
|
144 | 144 | } |
145 | 145 | ?> |
@@ -117,11 +117,11 @@ |
||
117 | 117 | $max = strlen($text); |
118 | 118 | $key = $this->key(); |
119 | 119 | |
120 | - for($i = 0; $i < $max; ++$i) |
|
120 | + for ($i = 0; $i < $max; ++$i) |
|
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()) |
|
124 | + if ($pos >= $this->keySize()) |
|
125 | 125 | $pos = 0; |
126 | 126 | |
127 | 127 | $text[$i] = $text[$i] ^ $key[$pos]; |
@@ -121,8 +121,9 @@ |
||
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; |
@@ -88,7 +88,6 @@ discard block |
||
88 | 88 | /** |
89 | 89 | * Encrypt plain text data using Skipjack |
90 | 90 | * |
91 | - * @param string $data A plain text string, 8 bytes long |
|
92 | 91 | * @return boolean Returns true |
93 | 92 | */ |
94 | 93 | public function encrypt(&$text) |
@@ -120,7 +119,6 @@ discard block |
||
120 | 119 | /** |
121 | 120 | * Decrypt a Skipjack encrypted string |
122 | 121 | * |
123 | - * @param string $encrypted A Skipjack encrypted string, 8 bytes long |
|
124 | 122 | * @return boolean Returns true |
125 | 123 | */ |
126 | 124 | public function decrypt(&$text) |
@@ -39,205 +39,205 @@ discard block |
||
39 | 39 | */ |
40 | 40 | class Cipher_Skipjack extends Cipher |
41 | 41 | { |
42 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | - const BYTES_BLOCK = 8; // 64 bits |
|
42 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | + const BYTES_BLOCK = 8; // 64 bits |
|
44 | 44 | |
45 | - /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | - const BYTES_KEY = 10; // 80 bits |
|
45 | + /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | + const BYTES_KEY = 10; // 80 bits |
|
47 | 47 | |
48 | - /** @type string $expanded_key The expanded key */ |
|
49 | - private $expanded_key = ""; |
|
48 | + /** @type string $expanded_key The expanded key */ |
|
49 | + private $expanded_key = ""; |
|
50 | 50 | |
51 | - /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | - private static $_f = array(); |
|
51 | + /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | + private static $_f = array(); |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Constructor |
|
57 | - * |
|
58 | - * @param string $key The key used for Encryption/Decryption |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __construct($key) |
|
62 | - { |
|
63 | - // set the Skipjack key |
|
64 | - parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | - |
|
66 | - // initialize variables |
|
67 | - $this->initTables(); |
|
68 | - |
|
69 | - // set the block size used |
|
70 | - $this->blockSize(self::BYTES_BLOCK); |
|
71 | - |
|
72 | - // expand the key from 10 bytes to 128 bytes |
|
73 | - $this->expandKey(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Destructor |
|
79 | - * |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function __destruct() |
|
83 | - { |
|
84 | - parent::__destruct(); |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Encrypt plain text data using Skipjack |
|
90 | - * |
|
91 | - * @param string $data A plain text string, 8 bytes long |
|
92 | - * @return boolean Returns true |
|
93 | - */ |
|
94 | - public function encrypt(&$text) |
|
95 | - { |
|
96 | - $this->operation(parent::ENCRYPT); |
|
97 | - |
|
98 | - for($i = 1; $i <= 32; ++$i) |
|
99 | - { |
|
100 | - $pos = (4 * $i) - 4; |
|
101 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
102 | - |
|
103 | - if($i >= 1 && $i <= 8) |
|
104 | - $this->ruleA($text, $subkey, $i); |
|
105 | - |
|
106 | - if($i >= 9 && $i <= 16) |
|
107 | - $this->ruleB($text, $subkey, $i); |
|
108 | - |
|
109 | - if($i >= 17 && $i <= 24) |
|
110 | - $this->ruleA($text, $subkey, $i); |
|
111 | - |
|
112 | - if($i >= 25 && $i <= 32) |
|
113 | - $this->ruleB($text, $subkey, $i); |
|
114 | - } |
|
115 | - |
|
116 | - return true; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * Decrypt a Skipjack encrypted string |
|
122 | - * |
|
123 | - * @param string $encrypted A Skipjack encrypted string, 8 bytes long |
|
124 | - * @return boolean Returns true |
|
125 | - */ |
|
126 | - public function decrypt(&$text) |
|
127 | - { |
|
128 | - $this->operation(parent::DECRYPT); |
|
129 | - |
|
130 | - for($i = 32; $i >= 1; --$i) |
|
131 | - { |
|
132 | - $pos = ($i - 1) * 4; |
|
133 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
134 | - |
|
135 | - if($i <= 32 && $i >= 25) |
|
136 | - $this->ruleB($text, $subkey, $i); |
|
137 | - |
|
138 | - if($i <= 24 && $i >= 17) |
|
139 | - $this->ruleA($text, $subkey, $i); |
|
140 | - |
|
141 | - if($i <= 16 && $i >= 9) |
|
142 | - $this->ruleB($text, $subkey, $i); |
|
143 | - |
|
144 | - if($i <= 8 && $i >= 1) |
|
145 | - $this->ruleA($text, $subkey, $i); |
|
146 | - } |
|
147 | - |
|
148 | - return true; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * For the G Permutations, the input data is 2 Bytes The first byte is |
|
154 | - * the left side and the second is the right side.The round key is 4 bytes |
|
155 | - * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
156 | - * |
|
157 | - * @param string $bytes A 2 byte string |
|
158 | - * @param string $key 4 bytes of $this->expanded_key |
|
159 | - * @return string A 2 byte string, the G Permutation of $bytes |
|
160 | - */ |
|
161 | - private function gPermutation($bytes, $key) |
|
162 | - { |
|
163 | - $left = ord($bytes[0]); |
|
164 | - $right = ord($bytes[1]); |
|
165 | - |
|
166 | - if($this->operation() == parent::ENCRYPT) |
|
167 | - { |
|
168 | - for($i = 0; $i < 4; ++$i) |
|
169 | - { |
|
170 | - if($i == 0 || $i == 2) |
|
171 | - { |
|
172 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
173 | - $left = $left ^ self::$_f[$pos]; |
|
174 | - } |
|
175 | - else |
|
176 | - { |
|
177 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
178 | - $right = $right ^ self::$_f[$pos]; |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - else // parent::DECRYPT |
|
183 | - { |
|
184 | - // we do the same as in encryption, but apply the key backwards, |
|
185 | - // from key[3] to key[0] |
|
186 | - for($i = 3; $i >= 0; --$i) |
|
187 | - { |
|
188 | - if($i == 0 || $i == 2) |
|
189 | - { |
|
190 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
191 | - $left = $left ^ self::$_f[$pos]; |
|
192 | - } |
|
193 | - else |
|
194 | - { |
|
195 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
196 | - $right = $right ^ self::$_f[$pos]; |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - return $this->dec2Str($left).$this->dec2Str($right); |
|
202 | - } |
|
203 | - |
|
204 | - |
|
205 | - /** |
|
206 | - * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
207 | - * 2 bytes each: W0, W1, W2, W3. |
|
208 | - * |
|
209 | - * @param string $bytes An 8 byte string |
|
210 | - * @param string $key 4 bytes of $this->expanded_key |
|
211 | - * @param integer $i The round number |
|
212 | - * @return void |
|
213 | - */ |
|
214 | - private function ruleA(&$bytes, $key, $i) |
|
215 | - { |
|
216 | - $w = str_split($bytes, 2); |
|
217 | - |
|
218 | - if($this->operation() == parent::ENCRYPT) |
|
219 | - { |
|
220 | - /* |
|
55 | + /** |
|
56 | + * Constructor |
|
57 | + * |
|
58 | + * @param string $key The key used for Encryption/Decryption |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __construct($key) |
|
62 | + { |
|
63 | + // set the Skipjack key |
|
64 | + parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | + |
|
66 | + // initialize variables |
|
67 | + $this->initTables(); |
|
68 | + |
|
69 | + // set the block size used |
|
70 | + $this->blockSize(self::BYTES_BLOCK); |
|
71 | + |
|
72 | + // expand the key from 10 bytes to 128 bytes |
|
73 | + $this->expandKey(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Destructor |
|
79 | + * |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function __destruct() |
|
83 | + { |
|
84 | + parent::__destruct(); |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Encrypt plain text data using Skipjack |
|
90 | + * |
|
91 | + * @param string $data A plain text string, 8 bytes long |
|
92 | + * @return boolean Returns true |
|
93 | + */ |
|
94 | + public function encrypt(&$text) |
|
95 | + { |
|
96 | + $this->operation(parent::ENCRYPT); |
|
97 | + |
|
98 | + for($i = 1; $i <= 32; ++$i) |
|
99 | + { |
|
100 | + $pos = (4 * $i) - 4; |
|
101 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
102 | + |
|
103 | + if($i >= 1 && $i <= 8) |
|
104 | + $this->ruleA($text, $subkey, $i); |
|
105 | + |
|
106 | + if($i >= 9 && $i <= 16) |
|
107 | + $this->ruleB($text, $subkey, $i); |
|
108 | + |
|
109 | + if($i >= 17 && $i <= 24) |
|
110 | + $this->ruleA($text, $subkey, $i); |
|
111 | + |
|
112 | + if($i >= 25 && $i <= 32) |
|
113 | + $this->ruleB($text, $subkey, $i); |
|
114 | + } |
|
115 | + |
|
116 | + return true; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * Decrypt a Skipjack encrypted string |
|
122 | + * |
|
123 | + * @param string $encrypted A Skipjack encrypted string, 8 bytes long |
|
124 | + * @return boolean Returns true |
|
125 | + */ |
|
126 | + public function decrypt(&$text) |
|
127 | + { |
|
128 | + $this->operation(parent::DECRYPT); |
|
129 | + |
|
130 | + for($i = 32; $i >= 1; --$i) |
|
131 | + { |
|
132 | + $pos = ($i - 1) * 4; |
|
133 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
134 | + |
|
135 | + if($i <= 32 && $i >= 25) |
|
136 | + $this->ruleB($text, $subkey, $i); |
|
137 | + |
|
138 | + if($i <= 24 && $i >= 17) |
|
139 | + $this->ruleA($text, $subkey, $i); |
|
140 | + |
|
141 | + if($i <= 16 && $i >= 9) |
|
142 | + $this->ruleB($text, $subkey, $i); |
|
143 | + |
|
144 | + if($i <= 8 && $i >= 1) |
|
145 | + $this->ruleA($text, $subkey, $i); |
|
146 | + } |
|
147 | + |
|
148 | + return true; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * For the G Permutations, the input data is 2 Bytes The first byte is |
|
154 | + * the left side and the second is the right side.The round key is 4 bytes |
|
155 | + * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
156 | + * |
|
157 | + * @param string $bytes A 2 byte string |
|
158 | + * @param string $key 4 bytes of $this->expanded_key |
|
159 | + * @return string A 2 byte string, the G Permutation of $bytes |
|
160 | + */ |
|
161 | + private function gPermutation($bytes, $key) |
|
162 | + { |
|
163 | + $left = ord($bytes[0]); |
|
164 | + $right = ord($bytes[1]); |
|
165 | + |
|
166 | + if($this->operation() == parent::ENCRYPT) |
|
167 | + { |
|
168 | + for($i = 0; $i < 4; ++$i) |
|
169 | + { |
|
170 | + if($i == 0 || $i == 2) |
|
171 | + { |
|
172 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
173 | + $left = $left ^ self::$_f[$pos]; |
|
174 | + } |
|
175 | + else |
|
176 | + { |
|
177 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
178 | + $right = $right ^ self::$_f[$pos]; |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + else // parent::DECRYPT |
|
183 | + { |
|
184 | + // we do the same as in encryption, but apply the key backwards, |
|
185 | + // from key[3] to key[0] |
|
186 | + for($i = 3; $i >= 0; --$i) |
|
187 | + { |
|
188 | + if($i == 0 || $i == 2) |
|
189 | + { |
|
190 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
191 | + $left = $left ^ self::$_f[$pos]; |
|
192 | + } |
|
193 | + else |
|
194 | + { |
|
195 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
196 | + $right = $right ^ self::$_f[$pos]; |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + return $this->dec2Str($left).$this->dec2Str($right); |
|
202 | + } |
|
203 | + |
|
204 | + |
|
205 | + /** |
|
206 | + * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
207 | + * 2 bytes each: W0, W1, W2, W3. |
|
208 | + * |
|
209 | + * @param string $bytes An 8 byte string |
|
210 | + * @param string $key 4 bytes of $this->expanded_key |
|
211 | + * @param integer $i The round number |
|
212 | + * @return void |
|
213 | + */ |
|
214 | + private function ruleA(&$bytes, $key, $i) |
|
215 | + { |
|
216 | + $w = str_split($bytes, 2); |
|
217 | + |
|
218 | + if($this->operation() == parent::ENCRYPT) |
|
219 | + { |
|
220 | + /* |
|
221 | 221 | * Set the W3 as the old W2 |
222 | 222 | * Set the W2 as the old W1 |
223 | 223 | * Set the W1 as the G(W0) |
224 | 224 | * Set the W0 as the W1 xor W4 xor i |
225 | 225 | */ |
226 | 226 | |
227 | - $w[4] = $w[3]; |
|
228 | - $w[3] = $w[2]; |
|
229 | - $w[2] = $w[1]; |
|
230 | - $w[1] = $this->gPermutation($w[0], $key); |
|
231 | - |
|
232 | - $hex1 = $this->str2Hex($w[1]); |
|
233 | - $hex4 = $this->str2Hex($w[4]); |
|
234 | - $hexi = $this->dec2Hex($i); |
|
235 | - $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
236 | - $w[0] = $this->hex2Str($w[0]); |
|
237 | - } |
|
238 | - else // parent::DECRYPT |
|
239 | - { |
|
240 | - /* |
|
227 | + $w[4] = $w[3]; |
|
228 | + $w[3] = $w[2]; |
|
229 | + $w[2] = $w[1]; |
|
230 | + $w[1] = $this->gPermutation($w[0], $key); |
|
231 | + |
|
232 | + $hex1 = $this->str2Hex($w[1]); |
|
233 | + $hex4 = $this->str2Hex($w[4]); |
|
234 | + $hexi = $this->dec2Hex($i); |
|
235 | + $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
236 | + $w[0] = $this->hex2Str($w[0]); |
|
237 | + } |
|
238 | + else // parent::DECRYPT |
|
239 | + { |
|
240 | + /* |
|
241 | 241 | * Set W4 as W0 xor W1 xor i |
242 | 242 | * Set W0 as Inverse G(W1) |
243 | 243 | * Set W1 as the old W2 |
@@ -245,60 +245,60 @@ discard block |
||
245 | 245 | * Set W3 as W4 |
246 | 246 | */ |
247 | 247 | |
248 | - $hex0 = $this->str2Hex($w[0]); |
|
249 | - $hex1 = $this->str2Hex($w[1]); |
|
250 | - $hexi = $this->dec2Hex($i); |
|
251 | - $w[4] = $this->xorHex($hex0, $hex1, $hexi); |
|
252 | - $w[4] = $this->hex2Str($w[4]); |
|
253 | - |
|
254 | - $w[0] = $this->gPermutation($w[1], $key); |
|
255 | - $w[1] = $w[2]; |
|
256 | - $w[2] = $w[3]; |
|
257 | - $w[3] = $w[4]; |
|
258 | - } |
|
259 | - |
|
260 | - // glue all the pieces back together |
|
261 | - $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * Perform SkipJacks RuleB function. Split the data into 4 parts, |
|
267 | - * 2 bytes each: W0, W1, W2, W3. |
|
268 | - * |
|
269 | - * @param string $bytes An 8 bytes string |
|
270 | - * @param string $key 4 bytes of $this->expanded_key |
|
271 | - * @param integer $i The round number |
|
272 | - * @return void |
|
273 | - */ |
|
274 | - private function ruleB(&$bytes, $key, $i) |
|
275 | - { |
|
276 | - $w = str_split($bytes, 2); |
|
277 | - |
|
278 | - if($this->operation() == parent::ENCRYPT) |
|
279 | - { |
|
280 | - /* |
|
248 | + $hex0 = $this->str2Hex($w[0]); |
|
249 | + $hex1 = $this->str2Hex($w[1]); |
|
250 | + $hexi = $this->dec2Hex($i); |
|
251 | + $w[4] = $this->xorHex($hex0, $hex1, $hexi); |
|
252 | + $w[4] = $this->hex2Str($w[4]); |
|
253 | + |
|
254 | + $w[0] = $this->gPermutation($w[1], $key); |
|
255 | + $w[1] = $w[2]; |
|
256 | + $w[2] = $w[3]; |
|
257 | + $w[3] = $w[4]; |
|
258 | + } |
|
259 | + |
|
260 | + // glue all the pieces back together |
|
261 | + $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * Perform SkipJacks RuleB function. Split the data into 4 parts, |
|
267 | + * 2 bytes each: W0, W1, W2, W3. |
|
268 | + * |
|
269 | + * @param string $bytes An 8 bytes string |
|
270 | + * @param string $key 4 bytes of $this->expanded_key |
|
271 | + * @param integer $i The round number |
|
272 | + * @return void |
|
273 | + */ |
|
274 | + private function ruleB(&$bytes, $key, $i) |
|
275 | + { |
|
276 | + $w = str_split($bytes, 2); |
|
277 | + |
|
278 | + if($this->operation() == parent::ENCRYPT) |
|
279 | + { |
|
280 | + /* |
|
281 | 281 | * Set the new W3 as the old W2 |
282 | 282 | * Set the new W2 as the old W0 xor old W1 xor i |
283 | 283 | * Set the new W1 as G(old W0) |
284 | 284 | * Set the new W0 as the old W3 |
285 | 285 | */ |
286 | 286 | |
287 | - $w[4] = $w[3]; |
|
288 | - $w[3] = $w[2]; |
|
289 | - |
|
290 | - $hex0 = $this->str2Hex($w[0]); |
|
291 | - $hex1 = $this->str2Hex($w[1]); |
|
292 | - $hexi = $this->dec2Hex($i); |
|
293 | - $w[2] = $this->xorHex($hex0, $hex1, $hexi); |
|
294 | - $w[2] = $this->hex2Str($w[2]); |
|
295 | - |
|
296 | - $w[1] = $this->gPermutation($w[0], $key); |
|
297 | - $w[0] = $w[4]; |
|
298 | - } |
|
299 | - else // parent::DECRYPT |
|
300 | - { |
|
301 | - /* |
|
287 | + $w[4] = $w[3]; |
|
288 | + $w[3] = $w[2]; |
|
289 | + |
|
290 | + $hex0 = $this->str2Hex($w[0]); |
|
291 | + $hex1 = $this->str2Hex($w[1]); |
|
292 | + $hexi = $this->dec2Hex($i); |
|
293 | + $w[2] = $this->xorHex($hex0, $hex1, $hexi); |
|
294 | + $w[2] = $this->hex2Str($w[2]); |
|
295 | + |
|
296 | + $w[1] = $this->gPermutation($w[0], $key); |
|
297 | + $w[0] = $w[4]; |
|
298 | + } |
|
299 | + else // parent::DECRYPT |
|
300 | + { |
|
301 | + /* |
|
302 | 302 | * Set W4 as the old W0 |
303 | 303 | * Set new W0 as Inverse G(old W1) |
304 | 304 | * Set new W1 as Inverse G(old W1) xor old W2 xor i |
@@ -306,86 +306,86 @@ discard block |
||
306 | 306 | * Set new W0 as the old W4 |
307 | 307 | */ |
308 | 308 | |
309 | - $w[4] = $w[0]; |
|
310 | - $w[0] = $this->gPermutation($w[1], $key); |
|
311 | - |
|
312 | - $hex0 = $this->str2Hex($w[0]); |
|
313 | - $hex2 = $this->str2Hex($w[2]); |
|
314 | - $hexi = $this->dec2Hex($i); |
|
315 | - $w[1] = $this->xorHex($hex0, $hex2, $hexi); |
|
316 | - $w[1] = $this->hex2Str($w[1]); |
|
317 | - |
|
318 | - $w[2] = $w[3]; |
|
319 | - $w[3] = $w[4]; |
|
320 | - } |
|
321 | - |
|
322 | - $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * Expands the key from 10 bytes, to 128 bytes |
|
328 | - * This is done by copying the key 1 byte at a time and |
|
329 | - * appending it to $this->expanded_key, when we reach the |
|
330 | - * end of the key, we start over at position 0 and continue |
|
331 | - * until we reach 128 bytes |
|
332 | - * |
|
333 | - * @return void |
|
334 | - */ |
|
335 | - private function expandKey() |
|
336 | - { |
|
337 | - $this->expanded_key = ""; |
|
338 | - $key_bytes = $this->keySize(); |
|
339 | - $key = $this->key(); |
|
340 | - $pos = 0; |
|
341 | - |
|
342 | - for($i = 0; $i < 128; ++$i) |
|
343 | - { |
|
344 | - if($pos == $key_bytes) |
|
345 | - $pos = 0; |
|
346 | - |
|
347 | - $this->expanded_key .= $key[$pos]; |
|
348 | - ++$pos; |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * Initialize all the tables, this function is called inside the constructor |
|
355 | - * |
|
356 | - * @return void |
|
357 | - */ |
|
358 | - private function initTables() |
|
359 | - { |
|
360 | - self::$_f = array( |
|
361 | - 0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, |
|
362 | - 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, |
|
363 | - 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, |
|
364 | - 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, |
|
365 | - 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, |
|
366 | - 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, |
|
367 | - 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, |
|
368 | - 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, |
|
369 | - 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, |
|
370 | - 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, |
|
371 | - 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, |
|
372 | - 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, |
|
373 | - 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, |
|
374 | - 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, |
|
375 | - 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, |
|
376 | - 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46 |
|
377 | - ); |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * Indicates that this is a block cipher |
|
383 | - * |
|
384 | - * @return integer Returns Cipher::BLOCK |
|
385 | - */ |
|
386 | - public function type() |
|
387 | - { |
|
388 | - return parent::BLOCK; |
|
389 | - } |
|
309 | + $w[4] = $w[0]; |
|
310 | + $w[0] = $this->gPermutation($w[1], $key); |
|
311 | + |
|
312 | + $hex0 = $this->str2Hex($w[0]); |
|
313 | + $hex2 = $this->str2Hex($w[2]); |
|
314 | + $hexi = $this->dec2Hex($i); |
|
315 | + $w[1] = $this->xorHex($hex0, $hex2, $hexi); |
|
316 | + $w[1] = $this->hex2Str($w[1]); |
|
317 | + |
|
318 | + $w[2] = $w[3]; |
|
319 | + $w[3] = $w[4]; |
|
320 | + } |
|
321 | + |
|
322 | + $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * Expands the key from 10 bytes, to 128 bytes |
|
328 | + * This is done by copying the key 1 byte at a time and |
|
329 | + * appending it to $this->expanded_key, when we reach the |
|
330 | + * end of the key, we start over at position 0 and continue |
|
331 | + * until we reach 128 bytes |
|
332 | + * |
|
333 | + * @return void |
|
334 | + */ |
|
335 | + private function expandKey() |
|
336 | + { |
|
337 | + $this->expanded_key = ""; |
|
338 | + $key_bytes = $this->keySize(); |
|
339 | + $key = $this->key(); |
|
340 | + $pos = 0; |
|
341 | + |
|
342 | + for($i = 0; $i < 128; ++$i) |
|
343 | + { |
|
344 | + if($pos == $key_bytes) |
|
345 | + $pos = 0; |
|
346 | + |
|
347 | + $this->expanded_key .= $key[$pos]; |
|
348 | + ++$pos; |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * Initialize all the tables, this function is called inside the constructor |
|
355 | + * |
|
356 | + * @return void |
|
357 | + */ |
|
358 | + private function initTables() |
|
359 | + { |
|
360 | + self::$_f = array( |
|
361 | + 0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, |
|
362 | + 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, |
|
363 | + 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, |
|
364 | + 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, |
|
365 | + 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, |
|
366 | + 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, |
|
367 | + 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, |
|
368 | + 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, |
|
369 | + 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, |
|
370 | + 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, |
|
371 | + 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, |
|
372 | + 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, |
|
373 | + 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, |
|
374 | + 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, |
|
375 | + 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, |
|
376 | + 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46 |
|
377 | + ); |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * Indicates that this is a block cipher |
|
383 | + * |
|
384 | + * @return integer Returns Cipher::BLOCK |
|
385 | + */ |
|
386 | + public function type() |
|
387 | + { |
|
388 | + return parent::BLOCK; |
|
389 | + } |
|
390 | 390 | } |
391 | 391 | ?> |
@@ -95,21 +95,21 @@ discard block |
||
95 | 95 | { |
96 | 96 | $this->operation(parent::ENCRYPT); |
97 | 97 | |
98 | - for($i = 1; $i <= 32; ++$i) |
|
98 | + for ($i = 1; $i <= 32; ++$i) |
|
99 | 99 | { |
100 | 100 | $pos = (4 * $i) - 4; |
101 | 101 | $subkey = substr($this->expanded_key, $pos, 4); |
102 | 102 | |
103 | - if($i >= 1 && $i <= 8) |
|
103 | + if ($i >= 1 && $i <= 8) |
|
104 | 104 | $this->ruleA($text, $subkey, $i); |
105 | 105 | |
106 | - if($i >= 9 && $i <= 16) |
|
106 | + if ($i >= 9 && $i <= 16) |
|
107 | 107 | $this->ruleB($text, $subkey, $i); |
108 | 108 | |
109 | - if($i >= 17 && $i <= 24) |
|
109 | + if ($i >= 17 && $i <= 24) |
|
110 | 110 | $this->ruleA($text, $subkey, $i); |
111 | 111 | |
112 | - if($i >= 25 && $i <= 32) |
|
112 | + if ($i >= 25 && $i <= 32) |
|
113 | 113 | $this->ruleB($text, $subkey, $i); |
114 | 114 | } |
115 | 115 | |
@@ -127,21 +127,21 @@ discard block |
||
127 | 127 | { |
128 | 128 | $this->operation(parent::DECRYPT); |
129 | 129 | |
130 | - for($i = 32; $i >= 1; --$i) |
|
130 | + for ($i = 32; $i >= 1; --$i) |
|
131 | 131 | { |
132 | 132 | $pos = ($i - 1) * 4; |
133 | 133 | $subkey = substr($this->expanded_key, $pos, 4); |
134 | 134 | |
135 | - if($i <= 32 && $i >= 25) |
|
135 | + if ($i <= 32 && $i >= 25) |
|
136 | 136 | $this->ruleB($text, $subkey, $i); |
137 | 137 | |
138 | - if($i <= 24 && $i >= 17) |
|
138 | + if ($i <= 24 && $i >= 17) |
|
139 | 139 | $this->ruleA($text, $subkey, $i); |
140 | 140 | |
141 | - if($i <= 16 && $i >= 9) |
|
141 | + if ($i <= 16 && $i >= 9) |
|
142 | 142 | $this->ruleB($text, $subkey, $i); |
143 | 143 | |
144 | - if($i <= 8 && $i >= 1) |
|
144 | + if ($i <= 8 && $i >= 1) |
|
145 | 145 | $this->ruleA($text, $subkey, $i); |
146 | 146 | } |
147 | 147 | |
@@ -163,11 +163,11 @@ discard block |
||
163 | 163 | $left = ord($bytes[0]); |
164 | 164 | $right = ord($bytes[1]); |
165 | 165 | |
166 | - if($this->operation() == parent::ENCRYPT) |
|
166 | + if ($this->operation() == parent::ENCRYPT) |
|
167 | 167 | { |
168 | - for($i = 0; $i < 4; ++$i) |
|
168 | + for ($i = 0; $i < 4; ++$i) |
|
169 | 169 | { |
170 | - if($i == 0 || $i == 2) |
|
170 | + if ($i == 0 || $i == 2) |
|
171 | 171 | { |
172 | 172 | $pos = $right ^ $this->str2Dec($key[$i]); |
173 | 173 | $left = $left ^ self::$_f[$pos]; |
@@ -183,9 +183,9 @@ discard block |
||
183 | 183 | { |
184 | 184 | // we do the same as in encryption, but apply the key backwards, |
185 | 185 | // from key[3] to key[0] |
186 | - for($i = 3; $i >= 0; --$i) |
|
186 | + for ($i = 3; $i >= 0; --$i) |
|
187 | 187 | { |
188 | - if($i == 0 || $i == 2) |
|
188 | + if ($i == 0 || $i == 2) |
|
189 | 189 | { |
190 | 190 | $pos = $right ^ $this->str2Dec($key[$i]); |
191 | 191 | $left = $left ^ self::$_f[$pos]; |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | { |
216 | 216 | $w = str_split($bytes, 2); |
217 | 217 | |
218 | - if($this->operation() == parent::ENCRYPT) |
|
218 | + if ($this->operation() == parent::ENCRYPT) |
|
219 | 219 | { |
220 | 220 | /* |
221 | 221 | * Set the W3 as the old W2 |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | { |
276 | 276 | $w = str_split($bytes, 2); |
277 | 277 | |
278 | - if($this->operation() == parent::ENCRYPT) |
|
278 | + if ($this->operation() == parent::ENCRYPT) |
|
279 | 279 | { |
280 | 280 | /* |
281 | 281 | * Set the new W3 as the old W2 |
@@ -339,9 +339,9 @@ discard block |
||
339 | 339 | $key = $this->key(); |
340 | 340 | $pos = 0; |
341 | 341 | |
342 | - for($i = 0; $i < 128; ++$i) |
|
342 | + for ($i = 0; $i < 128; ++$i) |
|
343 | 343 | { |
344 | - if($pos == $key_bytes) |
|
344 | + if ($pos == $key_bytes) |
|
345 | 345 | $pos = 0; |
346 | 346 | |
347 | 347 | $this->expanded_key .= $key[$pos]; |
@@ -100,17 +100,21 @@ discard block |
||
100 | 100 | $pos = (4 * $i) - 4; |
101 | 101 | $subkey = substr($this->expanded_key, $pos, 4); |
102 | 102 | |
103 | - if($i >= 1 && $i <= 8) |
|
104 | - $this->ruleA($text, $subkey, $i); |
|
103 | + if($i >= 1 && $i <= 8) { |
|
104 | + $this->ruleA($text, $subkey, $i); |
|
105 | + } |
|
105 | 106 | |
106 | - if($i >= 9 && $i <= 16) |
|
107 | - $this->ruleB($text, $subkey, $i); |
|
107 | + if($i >= 9 && $i <= 16) { |
|
108 | + $this->ruleB($text, $subkey, $i); |
|
109 | + } |
|
108 | 110 | |
109 | - if($i >= 17 && $i <= 24) |
|
110 | - $this->ruleA($text, $subkey, $i); |
|
111 | + if($i >= 17 && $i <= 24) { |
|
112 | + $this->ruleA($text, $subkey, $i); |
|
113 | + } |
|
111 | 114 | |
112 | - if($i >= 25 && $i <= 32) |
|
113 | - $this->ruleB($text, $subkey, $i); |
|
115 | + if($i >= 25 && $i <= 32) { |
|
116 | + $this->ruleB($text, $subkey, $i); |
|
117 | + } |
|
114 | 118 | } |
115 | 119 | |
116 | 120 | return true; |
@@ -132,17 +136,21 @@ discard block |
||
132 | 136 | $pos = ($i - 1) * 4; |
133 | 137 | $subkey = substr($this->expanded_key, $pos, 4); |
134 | 138 | |
135 | - if($i <= 32 && $i >= 25) |
|
136 | - $this->ruleB($text, $subkey, $i); |
|
139 | + if($i <= 32 && $i >= 25) { |
|
140 | + $this->ruleB($text, $subkey, $i); |
|
141 | + } |
|
137 | 142 | |
138 | - if($i <= 24 && $i >= 17) |
|
139 | - $this->ruleA($text, $subkey, $i); |
|
143 | + if($i <= 24 && $i >= 17) { |
|
144 | + $this->ruleA($text, $subkey, $i); |
|
145 | + } |
|
140 | 146 | |
141 | - if($i <= 16 && $i >= 9) |
|
142 | - $this->ruleB($text, $subkey, $i); |
|
147 | + if($i <= 16 && $i >= 9) { |
|
148 | + $this->ruleB($text, $subkey, $i); |
|
149 | + } |
|
143 | 150 | |
144 | - if($i <= 8 && $i >= 1) |
|
145 | - $this->ruleA($text, $subkey, $i); |
|
151 | + if($i <= 8 && $i >= 1) { |
|
152 | + $this->ruleA($text, $subkey, $i); |
|
153 | + } |
|
146 | 154 | } |
147 | 155 | |
148 | 156 | return true; |
@@ -171,15 +179,13 @@ discard block |
||
171 | 179 | { |
172 | 180 | $pos = $right ^ $this->str2Dec($key[$i]); |
173 | 181 | $left = $left ^ self::$_f[$pos]; |
174 | - } |
|
175 | - else |
|
182 | + } else |
|
176 | 183 | { |
177 | 184 | $pos = $left ^ $this->str2Dec($key[$i]); |
178 | 185 | $right = $right ^ self::$_f[$pos]; |
179 | 186 | } |
180 | 187 | } |
181 | - } |
|
182 | - else // parent::DECRYPT |
|
188 | + } else // parent::DECRYPT |
|
183 | 189 | { |
184 | 190 | // we do the same as in encryption, but apply the key backwards, |
185 | 191 | // from key[3] to key[0] |
@@ -189,8 +195,7 @@ discard block |
||
189 | 195 | { |
190 | 196 | $pos = $right ^ $this->str2Dec($key[$i]); |
191 | 197 | $left = $left ^ self::$_f[$pos]; |
192 | - } |
|
193 | - else |
|
198 | + } else |
|
194 | 199 | { |
195 | 200 | $pos = $left ^ $this->str2Dec($key[$i]); |
196 | 201 | $right = $right ^ self::$_f[$pos]; |
@@ -234,8 +239,7 @@ discard block |
||
234 | 239 | $hexi = $this->dec2Hex($i); |
235 | 240 | $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
236 | 241 | $w[0] = $this->hex2Str($w[0]); |
237 | - } |
|
238 | - else // parent::DECRYPT |
|
242 | + } else // parent::DECRYPT |
|
239 | 243 | { |
240 | 244 | /* |
241 | 245 | * Set W4 as W0 xor W1 xor i |
@@ -295,8 +299,7 @@ discard block |
||
295 | 299 | |
296 | 300 | $w[1] = $this->gPermutation($w[0], $key); |
297 | 301 | $w[0] = $w[4]; |
298 | - } |
|
299 | - else // parent::DECRYPT |
|
302 | + } else // parent::DECRYPT |
|
300 | 303 | { |
301 | 304 | /* |
302 | 305 | * Set W4 as the old W0 |
@@ -341,8 +344,9 @@ discard block |
||
341 | 344 | |
342 | 345 | for($i = 0; $i < 128; ++$i) |
343 | 346 | { |
344 | - if($pos == $key_bytes) |
|
345 | - $pos = 0; |
|
347 | + if($pos == $key_bytes) { |
|
348 | + $pos = 0; |
|
349 | + } |
|
346 | 350 | |
347 | 351 | $this->expanded_key .= $key[$pos]; |
348 | 352 | ++$pos; |
@@ -77,7 +77,6 @@ discard block |
||
77 | 77 | /** |
78 | 78 | * Encrypt plain text data using Vigenere cipher |
79 | 79 | * |
80 | - * @param string $data A plain text string |
|
81 | 80 | * @return boolean Returns true |
82 | 81 | */ |
83 | 82 | public function encrypt(&$text) |
@@ -114,7 +113,6 @@ discard block |
||
114 | 113 | /** |
115 | 114 | * Decrypt a Vigenere encrypted string |
116 | 115 | * |
117 | - * @param string $encrypted A Vigenere encrypted string |
|
118 | 116 | * @return boolean Returns true |
119 | 117 | */ |
120 | 118 | public function decrypt(&$text) |
@@ -44,206 +44,206 @@ |
||
44 | 44 | */ |
45 | 45 | class Cipher_Vigenere extends Cipher |
46 | 46 | { |
47 | - /** @type array $_vtable The Vigenere table */ |
|
48 | - private static $_vtable = null; |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Constructor |
|
53 | - * |
|
54 | - * @param string $key The key used for Encryption/Decryption |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function __construct($key) |
|
58 | - { |
|
59 | - // set the key |
|
60 | - parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | - |
|
62 | - $this->initTables(); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Destructor |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function __destruct() |
|
72 | - { |
|
73 | - parent::__destruct(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Encrypt plain text data using Vigenere cipher |
|
79 | - * |
|
80 | - * @param string $data A plain text string |
|
81 | - * @return boolean Returns true |
|
82 | - */ |
|
83 | - public function encrypt(&$text) |
|
84 | - { |
|
85 | - $this->operation(parent::ENCRYPT); |
|
86 | - |
|
87 | - // convert to uppercase, and remove any non alphabetic characters |
|
88 | - $text = strtoupper($text); |
|
89 | - $text = preg_replace("/[^A-Z]/", "", $text); |
|
90 | - $len = strlen($text); |
|
91 | - |
|
92 | - // prepare the key for the cipher |
|
93 | - $this->keyPrep($len); |
|
94 | - |
|
95 | - // loop through each letter of the message |
|
96 | - for($i = 0; $i < $len; ++$i) |
|
97 | - { |
|
98 | - // get the Cipher letter from the Vigenere table, using the |
|
99 | - // current letter from the key as the row, and the current letter |
|
100 | - // from the text, as the column, subtract 65 because ascii upper case |
|
101 | - // letters start at 65 |
|
102 | - $row = ord($this->expanded_key[$i]) - 65; |
|
103 | - $col = ord($text[$i]) - 65; |
|
104 | - $pos = ($row * 26) + $col; |
|
105 | - |
|
106 | - // convert the plain text to cipher text |
|
107 | - $text[$i] = self::$_vtable[$pos]; |
|
108 | - } |
|
109 | - |
|
110 | - return true; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Decrypt a Vigenere encrypted string |
|
116 | - * |
|
117 | - * @param string $encrypted A Vigenere encrypted string |
|
118 | - * @return boolean Returns true |
|
119 | - */ |
|
120 | - public function decrypt(&$text) |
|
121 | - { |
|
122 | - $this->operation(parent::DECRYPT); |
|
123 | - |
|
124 | - // ensure the cipher text is all uppercase |
|
125 | - $text = strtoupper($text); |
|
126 | - $len = strlen($text); |
|
127 | - |
|
128 | - // prepare the key for the cipher |
|
129 | - $this->keyPrep($len); |
|
130 | - |
|
131 | - // go to the row corresponding to the letter from the key |
|
132 | - for($i = 0; $i < $len; ++$i) |
|
133 | - { |
|
134 | - // find the row from the current character of the key, we subtract 65 |
|
135 | - // because uppercase letters start at ASCII 65 |
|
136 | - $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
137 | - |
|
138 | - // loop throw the entire row in the table until we find the letter |
|
139 | - // that matches the letter from the encrypted text |
|
140 | - for($j = 0; $j < 26; ++$j) |
|
141 | - { |
|
142 | - // save the position we are in in the row |
|
143 | - $pos = $row + $j; |
|
144 | - |
|
145 | - // compare the letter from the table to the letter in the cipher text |
|
146 | - if(self::$_vtable[$pos] == $text[$i]) |
|
147 | - { |
|
148 | - // bingo, we found it. The plain text is the letter associated with |
|
149 | - // with the column position, again add 65 because ascii capital |
|
150 | - // letters start at 65 |
|
151 | - $text[$i] = chr($j + 65); |
|
152 | - break; |
|
153 | - } |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - return true; |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Prepare the key. The key can only contain uppercase letters. |
|
163 | - * All other characters are stripped out. The key length must match |
|
164 | - * The length of the message |
|
165 | - * |
|
166 | - * @param integer $len The length of message |
|
167 | - * @return void |
|
168 | - */ |
|
169 | - private function keyPrep($len) |
|
170 | - { |
|
171 | - // we never modify the actual key, so we save it into another variable |
|
172 | - $this->expanded_key = $this->key(); |
|
173 | - $this->expanded_key = strtoupper($this->expanded_key); |
|
174 | - $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
175 | - $keylen = strlen($this->expanded_key); |
|
176 | - |
|
177 | - // The key must be prepared so that it is the same length as the |
|
178 | - // message. If it is longer or shorter we need to modify it |
|
179 | - // to make it the correct length |
|
180 | - if($keylen > $len) |
|
181 | - $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
182 | - else if($len > $keylen) |
|
183 | - { |
|
184 | - // if the key is shorter than the message, then we need pad the key |
|
185 | - // by repeating it until it is the correct length |
|
186 | - $diff = $len - $keylen; |
|
187 | - $pos = 0; |
|
188 | - |
|
189 | - for($i = 0; $i < $diff; ++$i) |
|
190 | - { |
|
191 | - if($pos >= $keylen) |
|
192 | - $pos = 0; |
|
193 | - |
|
194 | - $this->expanded_key .= $this->expanded_key[$pos]; |
|
195 | - ++$pos; |
|
196 | - } |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * Initialize the Vigenere table used for encryption & decryption |
|
203 | - * |
|
204 | - * @return void |
|
205 | - */ |
|
206 | - private function initTables() |
|
207 | - { |
|
208 | - self::$_vtable = array( |
|
209 | - 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
|
210 | - 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A', |
|
211 | - 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B', |
|
212 | - 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C', |
|
213 | - 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D', |
|
214 | - 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E', |
|
215 | - 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F', |
|
216 | - 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G', |
|
217 | - 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H', |
|
218 | - 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I', |
|
219 | - 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J', |
|
220 | - 'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K', |
|
221 | - 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L', |
|
222 | - 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M', |
|
223 | - 'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N', |
|
224 | - 'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', |
|
225 | - 'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', |
|
226 | - 'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q', |
|
227 | - 'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', |
|
228 | - 'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', |
|
229 | - 'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T', |
|
230 | - 'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U', |
|
231 | - 'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V', |
|
232 | - 'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', |
|
233 | - 'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X', |
|
234 | - 'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y' |
|
235 | - ); |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Indicates that this is a stream cipher |
|
241 | - * |
|
242 | - * @return integer Returns Cipher::STREAM |
|
243 | - */ |
|
244 | - public function type() |
|
245 | - { |
|
246 | - return parent::STREAM; |
|
247 | - } |
|
47 | + /** @type array $_vtable The Vigenere table */ |
|
48 | + private static $_vtable = null; |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Constructor |
|
53 | + * |
|
54 | + * @param string $key The key used for Encryption/Decryption |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function __construct($key) |
|
58 | + { |
|
59 | + // set the key |
|
60 | + parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | + |
|
62 | + $this->initTables(); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Destructor |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function __destruct() |
|
72 | + { |
|
73 | + parent::__destruct(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Encrypt plain text data using Vigenere cipher |
|
79 | + * |
|
80 | + * @param string $data A plain text string |
|
81 | + * @return boolean Returns true |
|
82 | + */ |
|
83 | + public function encrypt(&$text) |
|
84 | + { |
|
85 | + $this->operation(parent::ENCRYPT); |
|
86 | + |
|
87 | + // convert to uppercase, and remove any non alphabetic characters |
|
88 | + $text = strtoupper($text); |
|
89 | + $text = preg_replace("/[^A-Z]/", "", $text); |
|
90 | + $len = strlen($text); |
|
91 | + |
|
92 | + // prepare the key for the cipher |
|
93 | + $this->keyPrep($len); |
|
94 | + |
|
95 | + // loop through each letter of the message |
|
96 | + for($i = 0; $i < $len; ++$i) |
|
97 | + { |
|
98 | + // get the Cipher letter from the Vigenere table, using the |
|
99 | + // current letter from the key as the row, and the current letter |
|
100 | + // from the text, as the column, subtract 65 because ascii upper case |
|
101 | + // letters start at 65 |
|
102 | + $row = ord($this->expanded_key[$i]) - 65; |
|
103 | + $col = ord($text[$i]) - 65; |
|
104 | + $pos = ($row * 26) + $col; |
|
105 | + |
|
106 | + // convert the plain text to cipher text |
|
107 | + $text[$i] = self::$_vtable[$pos]; |
|
108 | + } |
|
109 | + |
|
110 | + return true; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Decrypt a Vigenere encrypted string |
|
116 | + * |
|
117 | + * @param string $encrypted A Vigenere encrypted string |
|
118 | + * @return boolean Returns true |
|
119 | + */ |
|
120 | + public function decrypt(&$text) |
|
121 | + { |
|
122 | + $this->operation(parent::DECRYPT); |
|
123 | + |
|
124 | + // ensure the cipher text is all uppercase |
|
125 | + $text = strtoupper($text); |
|
126 | + $len = strlen($text); |
|
127 | + |
|
128 | + // prepare the key for the cipher |
|
129 | + $this->keyPrep($len); |
|
130 | + |
|
131 | + // go to the row corresponding to the letter from the key |
|
132 | + for($i = 0; $i < $len; ++$i) |
|
133 | + { |
|
134 | + // find the row from the current character of the key, we subtract 65 |
|
135 | + // because uppercase letters start at ASCII 65 |
|
136 | + $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
137 | + |
|
138 | + // loop throw the entire row in the table until we find the letter |
|
139 | + // that matches the letter from the encrypted text |
|
140 | + for($j = 0; $j < 26; ++$j) |
|
141 | + { |
|
142 | + // save the position we are in in the row |
|
143 | + $pos = $row + $j; |
|
144 | + |
|
145 | + // compare the letter from the table to the letter in the cipher text |
|
146 | + if(self::$_vtable[$pos] == $text[$i]) |
|
147 | + { |
|
148 | + // bingo, we found it. The plain text is the letter associated with |
|
149 | + // with the column position, again add 65 because ascii capital |
|
150 | + // letters start at 65 |
|
151 | + $text[$i] = chr($j + 65); |
|
152 | + break; |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + return true; |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Prepare the key. The key can only contain uppercase letters. |
|
163 | + * All other characters are stripped out. The key length must match |
|
164 | + * The length of the message |
|
165 | + * |
|
166 | + * @param integer $len The length of message |
|
167 | + * @return void |
|
168 | + */ |
|
169 | + private function keyPrep($len) |
|
170 | + { |
|
171 | + // we never modify the actual key, so we save it into another variable |
|
172 | + $this->expanded_key = $this->key(); |
|
173 | + $this->expanded_key = strtoupper($this->expanded_key); |
|
174 | + $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
175 | + $keylen = strlen($this->expanded_key); |
|
176 | + |
|
177 | + // The key must be prepared so that it is the same length as the |
|
178 | + // message. If it is longer or shorter we need to modify it |
|
179 | + // to make it the correct length |
|
180 | + if($keylen > $len) |
|
181 | + $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
182 | + else if($len > $keylen) |
|
183 | + { |
|
184 | + // if the key is shorter than the message, then we need pad the key |
|
185 | + // by repeating it until it is the correct length |
|
186 | + $diff = $len - $keylen; |
|
187 | + $pos = 0; |
|
188 | + |
|
189 | + for($i = 0; $i < $diff; ++$i) |
|
190 | + { |
|
191 | + if($pos >= $keylen) |
|
192 | + $pos = 0; |
|
193 | + |
|
194 | + $this->expanded_key .= $this->expanded_key[$pos]; |
|
195 | + ++$pos; |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * Initialize the Vigenere table used for encryption & decryption |
|
203 | + * |
|
204 | + * @return void |
|
205 | + */ |
|
206 | + private function initTables() |
|
207 | + { |
|
208 | + self::$_vtable = array( |
|
209 | + 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
|
210 | + 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A', |
|
211 | + 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B', |
|
212 | + 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C', |
|
213 | + 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D', |
|
214 | + 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E', |
|
215 | + 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F', |
|
216 | + 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G', |
|
217 | + 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H', |
|
218 | + 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I', |
|
219 | + 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J', |
|
220 | + 'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K', |
|
221 | + 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L', |
|
222 | + 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M', |
|
223 | + 'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N', |
|
224 | + 'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', |
|
225 | + 'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', |
|
226 | + 'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q', |
|
227 | + 'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', |
|
228 | + 'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', |
|
229 | + 'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T', |
|
230 | + 'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U', |
|
231 | + 'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V', |
|
232 | + 'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', |
|
233 | + 'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X', |
|
234 | + 'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y' |
|
235 | + ); |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Indicates that this is a stream cipher |
|
241 | + * |
|
242 | + * @return integer Returns Cipher::STREAM |
|
243 | + */ |
|
244 | + public function type() |
|
245 | + { |
|
246 | + return parent::STREAM; |
|
247 | + } |
|
248 | 248 | } |
249 | 249 | ?> |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | $this->keyPrep($len); |
94 | 94 | |
95 | 95 | // loop through each letter of the message |
96 | - for($i = 0; $i < $len; ++$i) |
|
96 | + for ($i = 0; $i < $len; ++$i) |
|
97 | 97 | { |
98 | 98 | // get the Cipher letter from the Vigenere table, using the |
99 | 99 | // current letter from the key as the row, and the current letter |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $this->keyPrep($len); |
130 | 130 | |
131 | 131 | // go to the row corresponding to the letter from the key |
132 | - for($i = 0; $i < $len; ++$i) |
|
132 | + for ($i = 0; $i < $len; ++$i) |
|
133 | 133 | { |
134 | 134 | // find the row from the current character of the key, we subtract 65 |
135 | 135 | // because uppercase letters start at ASCII 65 |
@@ -137,13 +137,13 @@ discard block |
||
137 | 137 | |
138 | 138 | // loop throw the entire row in the table until we find the letter |
139 | 139 | // that matches the letter from the encrypted text |
140 | - for($j = 0; $j < 26; ++$j) |
|
140 | + for ($j = 0; $j < 26; ++$j) |
|
141 | 141 | { |
142 | 142 | // save the position we are in in the row |
143 | 143 | $pos = $row + $j; |
144 | 144 | |
145 | 145 | // compare the letter from the table to the letter in the cipher text |
146 | - if(self::$_vtable[$pos] == $text[$i]) |
|
146 | + if (self::$_vtable[$pos] == $text[$i]) |
|
147 | 147 | { |
148 | 148 | // bingo, we found it. The plain text is the letter associated with |
149 | 149 | // with the column position, again add 65 because ascii capital |
@@ -177,18 +177,18 @@ discard block |
||
177 | 177 | // The key must be prepared so that it is the same length as the |
178 | 178 | // message. If it is longer or shorter we need to modify it |
179 | 179 | // to make it the correct length |
180 | - if($keylen > $len) |
|
180 | + if ($keylen > $len) |
|
181 | 181 | $this->expanded_key = substr($this->expanded_key, 0, $len); |
182 | - else if($len > $keylen) |
|
182 | + else if ($len > $keylen) |
|
183 | 183 | { |
184 | 184 | // if the key is shorter than the message, then we need pad the key |
185 | 185 | // by repeating it until it is the correct length |
186 | 186 | $diff = $len - $keylen; |
187 | 187 | $pos = 0; |
188 | 188 | |
189 | - for($i = 0; $i < $diff; ++$i) |
|
189 | + for ($i = 0; $i < $diff; ++$i) |
|
190 | 190 | { |
191 | - if($pos >= $keylen) |
|
191 | + if ($pos >= $keylen) |
|
192 | 192 | $pos = 0; |
193 | 193 | |
194 | 194 | $this->expanded_key .= $this->expanded_key[$pos]; |
@@ -206,32 +206,32 @@ discard block |
||
206 | 206 | private function initTables() |
207 | 207 | { |
208 | 208 | self::$_vtable = array( |
209 | - 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
|
210 | - 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A', |
|
211 | - 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B', |
|
212 | - 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C', |
|
213 | - 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D', |
|
214 | - 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E', |
|
215 | - 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F', |
|
216 | - 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G', |
|
217 | - 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H', |
|
218 | - 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I', |
|
219 | - 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J', |
|
220 | - 'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K', |
|
221 | - 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L', |
|
222 | - 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M', |
|
223 | - 'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N', |
|
224 | - 'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', |
|
225 | - 'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', |
|
226 | - 'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q', |
|
227 | - 'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', |
|
228 | - 'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', |
|
229 | - 'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T', |
|
230 | - 'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U', |
|
231 | - 'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V', |
|
232 | - 'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W', |
|
233 | - 'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X', |
|
234 | - 'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y' |
|
209 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
210 | + 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', |
|
211 | + 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', |
|
212 | + 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', |
|
213 | + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', |
|
214 | + 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', |
|
215 | + 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', |
|
216 | + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
217 | + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
218 | + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
|
219 | + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
220 | + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
|
221 | + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|
222 | + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
223 | + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
|
224 | + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
225 | + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
226 | + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', |
|
227 | + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
228 | + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
229 | + 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
|
230 | + 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
|
231 | + 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
232 | + 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
|
233 | + 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
234 | + 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' |
|
235 | 235 | ); |
236 | 236 | } |
237 | 237 |
@@ -177,9 +177,9 @@ discard block |
||
177 | 177 | // The key must be prepared so that it is the same length as the |
178 | 178 | // message. If it is longer or shorter we need to modify it |
179 | 179 | // to make it the correct length |
180 | - if($keylen > $len) |
|
181 | - $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
182 | - else if($len > $keylen) |
|
180 | + if($keylen > $len) { |
|
181 | + $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
182 | + } else if($len > $keylen) |
|
183 | 183 | { |
184 | 184 | // if the key is shorter than the message, then we need pad the key |
185 | 185 | // by repeating it until it is the correct length |
@@ -188,8 +188,9 @@ discard block |
||
188 | 188 | |
189 | 189 | for($i = 0; $i < $diff; ++$i) |
190 | 190 | { |
191 | - if($pos >= $keylen) |
|
192 | - $pos = 0; |
|
191 | + if($pos >= $keylen) { |
|
192 | + $pos = 0; |
|
193 | + } |
|
193 | 194 | |
194 | 195 | $this->expanded_key .= $this->expanded_key[$pos]; |
195 | 196 | ++$pos; |
@@ -155,6 +155,7 @@ discard block |
||
155 | 155 | * Convert a binary string (ie: 01101011) to a decimal number |
156 | 156 | * |
157 | 157 | * @param string A string representation of a binary number |
158 | + * @param string $bin |
|
158 | 159 | * @return integer The number converted from the binary string |
159 | 160 | */ |
160 | 161 | public static function bin2Dec($bin) |
@@ -478,7 +479,6 @@ discard block |
||
478 | 479 | /** |
479 | 480 | * Rotates bits Left, appending the bits pushed off the left onto the right |
480 | 481 | * |
481 | - * @param integer $n The integer to rotate bits to the left |
|
482 | 482 | * @param integer $shifts The number of shifts left to make |
483 | 483 | * @return integer The resulting value from the rotation |
484 | 484 | */ |
@@ -504,7 +504,6 @@ discard block |
||
504 | 504 | /** |
505 | 505 | * Rotates bits right, appending the bits pushed off the right onto the left |
506 | 506 | * |
507 | - * @param integer $n The integer to rotate bits to the right |
|
508 | 507 | * @param integer $shifts The number of shifts right to make |
509 | 508 | * @return integer The resulting value from the rotation |
510 | 509 | */ |
@@ -33,590 +33,590 @@ |
||
33 | 33 | */ |
34 | 34 | class Core |
35 | 35 | { |
36 | - /** @type integer HASH_LEN The length of md5() hash string */ |
|
37 | - const HASH_LEN = 16; |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Constructor |
|
42 | - * |
|
43 | - */ |
|
44 | - protected function __construct() |
|
45 | - { |
|
46 | - |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Destructor |
|
52 | - * |
|
53 | - */ |
|
54 | - protected function __destruct() |
|
55 | - { |
|
56 | - |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Convert hexidecimal to a binary string (ex: "00110110") |
|
62 | - * |
|
63 | - * @param string $hex A string containing a hexidecimal number |
|
64 | - * @return string A string representation of a binary |
|
65 | - */ |
|
66 | - public static function hex2Bin($hex) |
|
67 | - { |
|
68 | - // if we do not have an even number of hex characters |
|
69 | - // append a 0 to the beginning to make it even |
|
70 | - if(strlen($hex) % 2) |
|
71 | - $hex = "0$hex"; |
|
72 | - |
|
73 | - $parts = str_split($hex, 2); |
|
74 | - $parts = array_map(function($v) { |
|
75 | - $v = base_convert($v, 16, 2); |
|
76 | - return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
77 | - }, $parts); |
|
78 | - |
|
79 | - return implode("", $parts); |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * Convert hex to a string |
|
85 | - * |
|
86 | - * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
87 | - * @return string a string |
|
88 | - */ |
|
89 | - public static function hex2Str($hex) |
|
90 | - { |
|
91 | - // php version >= 5.4 have a hex2bin function, use it |
|
92 | - // if it exists |
|
93 | - if(function_exists("hex2bin")) |
|
94 | - return hex2bin($hex); |
|
95 | - |
|
96 | - $parts = str_split($hex, 2); |
|
97 | - $parts = array_map(function($v) { |
|
98 | - return chr(Core::hex2Dec($v)); |
|
99 | - }, $parts); |
|
100 | - |
|
101 | - return implode("", $parts); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Converts Hex to Decimal |
|
107 | - * This function just calls php's hexdec() function, but I |
|
108 | - * encapsulated it in this function to keep things uniform |
|
109 | - * and have all possible conversion function available in |
|
110 | - * the Cipher class |
|
111 | - * |
|
112 | - * @param string $hex A hex number to convert to decimal |
|
113 | - * @return integer A decimal number |
|
114 | - */ |
|
115 | - public static function hex2Dec($hex) |
|
116 | - { |
|
117 | - return hexdec($hex); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Convert binary string (ie 00110110) to hex |
|
123 | - * |
|
124 | - * @param string $bin A binary string |
|
125 | - * @return string A string representation of hexidecimal number |
|
126 | - */ |
|
127 | - public static function bin2Hex($bin) |
|
128 | - { |
|
129 | - $parts = str_split($bin, 8); |
|
130 | - |
|
131 | - $parts = array_map(function($v) { |
|
132 | - $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
133 | - $v = dechex(bindec($v)); |
|
134 | - return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
135 | - }, $parts); |
|
136 | - |
|
137 | - return implode("", $parts); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * Converts a binary representation (ie 01101011) back to a string |
|
143 | - * |
|
144 | - * @param string $bin a binary representation string |
|
145 | - * @return string A string of characters representing the binary |
|
146 | - */ |
|
147 | - public static function bin2Str($bin) |
|
148 | - { |
|
149 | - $hex = self::bin2Hex($bin); |
|
150 | - return self::hex2Str($hex); |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * Convert a binary string (ie: 01101011) to a decimal number |
|
156 | - * |
|
157 | - * @param string A string representation of a binary number |
|
158 | - * @return integer The number converted from the binary string |
|
159 | - */ |
|
160 | - public static function bin2Dec($bin) |
|
161 | - { |
|
162 | - return bindec($bin); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Convert a string to hex |
|
168 | - * This function calls the PHP bin2hex(), and is here |
|
169 | - * for consistency with the other string functions |
|
170 | - * |
|
171 | - * @param string $str A string |
|
172 | - * @return string A string representation of hexidecimal number |
|
173 | - */ |
|
174 | - public static function str2Hex($str) |
|
175 | - { |
|
176 | - return bin2hex($str); |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Convert a string of characters to a decimal number |
|
182 | - * |
|
183 | - * @param string $str The string to convert to decimal |
|
184 | - * @return integer The integer converted from the string |
|
185 | - */ |
|
186 | - public static function str2Dec($str) |
|
187 | - { |
|
188 | - $hex = self::str2Hex($str); |
|
189 | - return self::hex2Dec($hex); |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * Converts a string to binary representation (ie 01101011) |
|
195 | - * |
|
196 | - * @param string $str A string |
|
197 | - * @return string A binary representation of the the string |
|
198 | - */ |
|
199 | - public static function str2Bin($str) |
|
200 | - { |
|
201 | - $hex = self::str2Hex($str); |
|
202 | - $parts = str_split($hex, 2); |
|
203 | - |
|
204 | - $parts = array_map(function($v) { |
|
205 | - return Core::hex2Bin($v); |
|
206 | - }, $parts); |
|
207 | - |
|
208 | - return implode("", $parts); |
|
209 | - } |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Converts Decimal to Hex |
|
214 | - * This function just calls php's dechex() function, but I |
|
215 | - * encapsulated it in this function to keep things uniform |
|
216 | - * and have all possible conversion function available in |
|
217 | - * the Cipher class |
|
218 | - * |
|
219 | - * The parameter $req_bytes will pad the return hex with NULL (00) |
|
220 | - * until the hex represents the number of bytes given to $req_bytes |
|
221 | - * This is because dechex() drops null bytes from the Hex, which may |
|
222 | - * be needed in some cases |
|
223 | - * |
|
224 | - * @param integer $dec A decimal number to convert |
|
225 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
226 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
227 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
228 | - * size than the initial integer. |
|
229 | - * @return string A hexidecimal representation of the decimal number |
|
230 | - */ |
|
231 | - public static function dec2Hex($dec, $req_bytes = 0) |
|
232 | - { |
|
233 | - $hex = dechex($dec); |
|
234 | - |
|
235 | - // if we do not have an even number of hex characters |
|
236 | - // append a 0 to the beginning. dechex() drops leading 0's |
|
237 | - if(strlen($hex) % 2) |
|
238 | - $hex = "0$hex"; |
|
239 | - |
|
240 | - // if the number of bytes in the hex is less than |
|
241 | - // what we need it to be, add null bytes to the |
|
242 | - // front of the hex to padd it to the required size |
|
243 | - if(($req_bytes * 2) > strlen($hex)) |
|
244 | - $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
245 | - |
|
246 | - return $hex; |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * Converts Decimal to Binary |
|
252 | - * This function just calls php's decbin() function, but I |
|
253 | - * encapsulated it in this function to keep things uniform |
|
254 | - * and have all possible conversion function available in |
|
255 | - * the Cipher class |
|
256 | - * |
|
257 | - * @param integer $dec A decimal number to convert |
|
258 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
259 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
260 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
261 | - * size than the initial integer. |
|
262 | - * @return string A binary representation of the decimal number |
|
263 | - */ |
|
264 | - public static function dec2Bin($dec, $req_bytes = 0) |
|
265 | - { |
|
266 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
267 | - return self::hex2Bin($hex); |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * Convert a decimal to a string of bytes |
|
273 | - * |
|
274 | - * @param integer $dec A decimal number |
|
275 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
276 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
277 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
278 | - * size than the initial integer. |
|
279 | - * @return string A string with the number of bytes equal to $dec |
|
280 | - */ |
|
281 | - public static function dec2Str($dec, $req_bytes = 0) |
|
282 | - { |
|
283 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
284 | - return self::hex2Str($hex); |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * XORs two binary strings (representation of binary, ie 01101011), |
|
290 | - * assumed to be equal length |
|
291 | - * |
|
292 | - * @param string $a A string that represents binary |
|
293 | - * @param string $b A string that represents binary |
|
294 | - * @return string A representation of binary |
|
295 | - */ |
|
296 | - public static function xorBin($a, $b) |
|
297 | - { |
|
298 | - $len_a = strlen($a); |
|
299 | - $len_b = strlen($b); |
|
300 | - $width = $len_a; |
|
301 | - |
|
302 | - // first determine if the two binary strings are the same length, |
|
303 | - // and if not get them to the same length |
|
304 | - if($len_a > $len_b) |
|
305 | - { |
|
306 | - $width = $len_a; |
|
307 | - $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
308 | - } |
|
309 | - else if($len_a < $len_b) |
|
310 | - { |
|
311 | - $width = $len_b; |
|
312 | - $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
313 | - } |
|
314 | - |
|
315 | - // fortunately PHP knows how to XOR each byte in a string |
|
316 | - // so we don't have to loop to do it |
|
317 | - $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
318 | - return self::str2Bin($bin); |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
324 | - * The values are string representations of hex values |
|
325 | - * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
326 | - * |
|
327 | - * @param string Unlimited number parameters, each a string representation of hex |
|
328 | - * @return string A string representation of the result in Hex |
|
329 | - */ |
|
330 | - public static function xorHex() |
|
331 | - { |
|
332 | - $hex = func_get_args(); |
|
333 | - $count = func_num_args(); |
|
334 | - |
|
335 | - // we need a minimum of 2 values |
|
336 | - if($count < 2) |
|
337 | - return false; |
|
338 | - |
|
339 | - // first get all hex values to an even number |
|
340 | - array_walk($hex, function(&$val, $i){ |
|
341 | - if(strlen($val) % 2) |
|
342 | - $val = "0".$val; |
|
343 | - }); |
|
344 | - |
|
345 | - $res = 0; |
|
346 | - for($i = 0; $i < $count; ++$i) |
|
347 | - { |
|
348 | - // if this is the first loop, set the 'result' to the first |
|
349 | - // hex value |
|
350 | - if($i == 0) |
|
351 | - $res = $hex[0]; |
|
352 | - else |
|
353 | - { |
|
354 | - // to make the code easier to follow |
|
355 | - $h1 = $res; |
|
356 | - $h2 = $hex[$i]; |
|
357 | - |
|
358 | - // get lengths |
|
359 | - $len1 = strlen($h1); |
|
360 | - $len2 = strlen($h2); |
|
361 | - |
|
362 | - // now check that both hex values are the same length, |
|
363 | - // if not pad them with 0's until they are |
|
364 | - if($len1 > $len2) |
|
365 | - $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
366 | - else if($len1 < $len2) |
|
367 | - $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
368 | - |
|
369 | - // PHP knows how to XOR each byte in a string, so convert the |
|
370 | - // hex to a string, XOR, and convert back |
|
371 | - $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
372 | - $res = self::str2Hex($res); |
|
373 | - } |
|
374 | - } |
|
375 | - |
|
376 | - return $res; |
|
377 | - } |
|
378 | - |
|
379 | - |
|
380 | - /** |
|
381 | - * Forces an integer to be signed |
|
382 | - * |
|
383 | - * @param integer $int An integer |
|
384 | - * @return integer An signed integer |
|
385 | - */ |
|
386 | - public static function sInt($int) |
|
387 | - { |
|
388 | - $arr = unpack("i", pack("i", $int)); |
|
389 | - return $arr[1]; |
|
390 | - } |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Forces an integer to be unsigned |
|
395 | - * |
|
396 | - * @param integer $int A signed integer |
|
397 | - * @return integer An unsigned integer |
|
398 | - */ |
|
399 | - public static function uInt($int) |
|
400 | - { |
|
401 | - $arr = unpack("I", pack("I", $int)); |
|
402 | - $ret = $arr[1]; |
|
403 | - |
|
404 | - // On 32 bit platforms unpack() and pack() do not convert |
|
405 | - // from signed to unsigned properly all the time, it will return |
|
406 | - // the same negative number given to it, the work around is |
|
407 | - // to use sprintf(). |
|
408 | - // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
409 | - if($ret < 0) |
|
410 | - $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
411 | - |
|
412 | - return $ret; |
|
413 | - } |
|
414 | - |
|
415 | - |
|
416 | - /** |
|
417 | - * Forces an integer to be a 32 bit signed integer |
|
418 | - * |
|
419 | - * @param integer $int An integer |
|
420 | - * @return integer An signed 32 bit integer |
|
421 | - */ |
|
422 | - public static function sInt32($int) |
|
423 | - { |
|
424 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
425 | - return self::sInt($int); |
|
426 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
427 | - { |
|
428 | - $arr = unpack("l", pack("l", $int)); |
|
429 | - return $arr[1]; |
|
430 | - } |
|
431 | - } |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * Force an integer to be a 32 bit unsigned integer |
|
436 | - * |
|
437 | - * @param integer $int An integer |
|
438 | - * @return integer An unsigned 32 bit integer |
|
439 | - */ |
|
440 | - public static function uInt32($int) |
|
441 | - { |
|
442 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
443 | - return self::uInt($int); |
|
444 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
445 | - { |
|
446 | - $arr = unpack("L", pack("L", $int)); |
|
447 | - return $arr[1]; |
|
448 | - } |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - /** |
|
453 | - * Converts an integer to the value for an signed char |
|
454 | - * |
|
455 | - * @param integer $int The integer to convert to a signed char |
|
456 | - * @return integer A signed integer, representing a signed char |
|
457 | - */ |
|
458 | - public static function sChar($int) |
|
459 | - { |
|
460 | - $arr = unpack("c", pack("c", $int)); |
|
461 | - return $arr[1]; |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - /** |
|
466 | - * Converts an integer to the value for an unsigned char |
|
467 | - * |
|
468 | - * @param integer $int The integer to convert to a unsigned char |
|
469 | - * @return integer An unsigned integer, representing a unsigned char |
|
470 | - */ |
|
471 | - public static function uChar($int) |
|
472 | - { |
|
473 | - $arr = unpack("C", pack("C", $int)); |
|
474 | - return $arr[1]; |
|
475 | - } |
|
476 | - |
|
477 | - |
|
478 | - /** |
|
479 | - * Rotates bits Left, appending the bits pushed off the left onto the right |
|
480 | - * |
|
481 | - * @param integer $n The integer to rotate bits to the left |
|
482 | - * @param integer $shifts The number of shifts left to make |
|
483 | - * @return integer The resulting value from the rotation |
|
484 | - */ |
|
485 | - public static function rotBitsLeft32($i, $shifts) |
|
486 | - { |
|
487 | - if($shifts <= 0) |
|
488 | - return $i; |
|
489 | - |
|
490 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
491 | - |
|
492 | - // this is causing problems on 32 bit platform |
|
493 | - //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
494 | - |
|
495 | - // so lets cheat: convert to binary string, rotate left, and |
|
496 | - // convert back to decimal |
|
497 | - $i = self::dec2Bin(self::uInt32($i), 4); |
|
498 | - $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
499 | - return self::bin2Dec($i); |
|
500 | - |
|
501 | - } |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * Rotates bits right, appending the bits pushed off the right onto the left |
|
506 | - * |
|
507 | - * @param integer $n The integer to rotate bits to the right |
|
508 | - * @param integer $shifts The number of shifts right to make |
|
509 | - * @return integer The resulting value from the rotation |
|
510 | - */ |
|
511 | - public static function rotBitsRight32($i, $shifts) |
|
512 | - { |
|
513 | - if($shifts <= 0) |
|
514 | - return $i; |
|
515 | - |
|
516 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
517 | - |
|
518 | - // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
519 | - // having a problem with some bit shifts on 32 bits |
|
520 | - // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
521 | - |
|
522 | - // so lets cheat: convert to binary string, rotate right, |
|
523 | - // and convert back to decimal |
|
524 | - $i = self::dec2Bin($i, 4); |
|
525 | - $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
526 | - return self::bin2Dec($i); |
|
527 | - } |
|
528 | - |
|
529 | - |
|
530 | - /** |
|
531 | - * Create a string of random bytes, used for creating an IV |
|
532 | - * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
533 | - * There are 4 ways to auto generate random bytes by setting $src parameter |
|
534 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
535 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
536 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
537 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
538 | - * |
|
539 | - * @param string $src Optional, Use the $src to create the random bytes |
|
540 | - * by default PHP_Crypt::RAND is used when $src is not specified |
|
541 | - * @param integer $byte_len The length of the byte string to create |
|
542 | - * @return string A random string of bytes |
|
543 | - */ |
|
544 | - public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
545 | - { |
|
546 | - $bytes = ""; |
|
547 | - $err_msg = ""; |
|
548 | - |
|
549 | - if($src == PHP_Crypt::RAND_DEV_RAND) |
|
550 | - { |
|
551 | - if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
552 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
553 | - else |
|
554 | - $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
555 | - } |
|
556 | - else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
557 | - { |
|
558 | - if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
559 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
560 | - else |
|
561 | - $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
562 | - } |
|
563 | - else if($src == PHP_Crypt::RAND_WIN_COM) |
|
564 | - { |
|
565 | - if(extension_loaded('com_dotnet')) |
|
566 | - { |
|
567 | - // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
568 | - try |
|
569 | - { |
|
570 | - // request a random number in $byte_len bytes, returned |
|
571 | - // as base_64 encoded string. This is because PHP munges the |
|
572 | - // binary data on Windows |
|
573 | - $com = @new \COM("CAPICOM.Utilities.1"); |
|
574 | - $bytes = $com->GetRandom($byte_len, 0); |
|
575 | - } |
|
576 | - catch(Exception $e) |
|
577 | - { |
|
578 | - $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
579 | - } |
|
580 | - |
|
581 | - if(!$bytes) |
|
582 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
583 | - } |
|
584 | - else |
|
585 | - $err_msg = "The COM_DOTNET extension is not loaded"; |
|
586 | - } |
|
587 | - |
|
588 | - // trigger a warning if something went wrong |
|
589 | - if($err_msg != "") |
|
590 | - trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
591 | - |
|
592 | - // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
593 | - // passed as the $src param, create the bytes using mt_rand(). It's not |
|
594 | - // the most secure option but we have no other choice |
|
595 | - if(strlen($bytes) < $byte_len) |
|
596 | - { |
|
597 | - $bytes = ""; |
|
598 | - |
|
599 | - // md5() hash a random number to get a 16 byte string, keep looping |
|
600 | - // until we have a string as long or longer than the ciphers block size |
|
601 | - for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
602 | - $bytes .= md5(mt_rand(), true); |
|
603 | - } |
|
604 | - |
|
605 | - // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
606 | - // cryptographically secure, lets add another layer of 'randomness' before |
|
607 | - // the final md5() below |
|
608 | - $bytes = str_shuffle($bytes); |
|
609 | - |
|
610 | - // md5() the $bytes to add extra randomness. Since md5() only returns |
|
611 | - // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
612 | - // some ciphers which have a block size larger than 16 bytes |
|
613 | - $tmp = ""; |
|
614 | - $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
615 | - for($i = 0; $i < $loop; ++$i) |
|
616 | - $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
617 | - |
|
618 | - // grab the number of bytes equal to the requested $byte_len |
|
619 | - return substr($tmp, 0, $byte_len); |
|
620 | - } |
|
36 | + /** @type integer HASH_LEN The length of md5() hash string */ |
|
37 | + const HASH_LEN = 16; |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Constructor |
|
42 | + * |
|
43 | + */ |
|
44 | + protected function __construct() |
|
45 | + { |
|
46 | + |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Destructor |
|
52 | + * |
|
53 | + */ |
|
54 | + protected function __destruct() |
|
55 | + { |
|
56 | + |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Convert hexidecimal to a binary string (ex: "00110110") |
|
62 | + * |
|
63 | + * @param string $hex A string containing a hexidecimal number |
|
64 | + * @return string A string representation of a binary |
|
65 | + */ |
|
66 | + public static function hex2Bin($hex) |
|
67 | + { |
|
68 | + // if we do not have an even number of hex characters |
|
69 | + // append a 0 to the beginning to make it even |
|
70 | + if(strlen($hex) % 2) |
|
71 | + $hex = "0$hex"; |
|
72 | + |
|
73 | + $parts = str_split($hex, 2); |
|
74 | + $parts = array_map(function($v) { |
|
75 | + $v = base_convert($v, 16, 2); |
|
76 | + return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
77 | + }, $parts); |
|
78 | + |
|
79 | + return implode("", $parts); |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * Convert hex to a string |
|
85 | + * |
|
86 | + * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
87 | + * @return string a string |
|
88 | + */ |
|
89 | + public static function hex2Str($hex) |
|
90 | + { |
|
91 | + // php version >= 5.4 have a hex2bin function, use it |
|
92 | + // if it exists |
|
93 | + if(function_exists("hex2bin")) |
|
94 | + return hex2bin($hex); |
|
95 | + |
|
96 | + $parts = str_split($hex, 2); |
|
97 | + $parts = array_map(function($v) { |
|
98 | + return chr(Core::hex2Dec($v)); |
|
99 | + }, $parts); |
|
100 | + |
|
101 | + return implode("", $parts); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Converts Hex to Decimal |
|
107 | + * This function just calls php's hexdec() function, but I |
|
108 | + * encapsulated it in this function to keep things uniform |
|
109 | + * and have all possible conversion function available in |
|
110 | + * the Cipher class |
|
111 | + * |
|
112 | + * @param string $hex A hex number to convert to decimal |
|
113 | + * @return integer A decimal number |
|
114 | + */ |
|
115 | + public static function hex2Dec($hex) |
|
116 | + { |
|
117 | + return hexdec($hex); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Convert binary string (ie 00110110) to hex |
|
123 | + * |
|
124 | + * @param string $bin A binary string |
|
125 | + * @return string A string representation of hexidecimal number |
|
126 | + */ |
|
127 | + public static function bin2Hex($bin) |
|
128 | + { |
|
129 | + $parts = str_split($bin, 8); |
|
130 | + |
|
131 | + $parts = array_map(function($v) { |
|
132 | + $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
133 | + $v = dechex(bindec($v)); |
|
134 | + return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
135 | + }, $parts); |
|
136 | + |
|
137 | + return implode("", $parts); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * Converts a binary representation (ie 01101011) back to a string |
|
143 | + * |
|
144 | + * @param string $bin a binary representation string |
|
145 | + * @return string A string of characters representing the binary |
|
146 | + */ |
|
147 | + public static function bin2Str($bin) |
|
148 | + { |
|
149 | + $hex = self::bin2Hex($bin); |
|
150 | + return self::hex2Str($hex); |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * Convert a binary string (ie: 01101011) to a decimal number |
|
156 | + * |
|
157 | + * @param string A string representation of a binary number |
|
158 | + * @return integer The number converted from the binary string |
|
159 | + */ |
|
160 | + public static function bin2Dec($bin) |
|
161 | + { |
|
162 | + return bindec($bin); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Convert a string to hex |
|
168 | + * This function calls the PHP bin2hex(), and is here |
|
169 | + * for consistency with the other string functions |
|
170 | + * |
|
171 | + * @param string $str A string |
|
172 | + * @return string A string representation of hexidecimal number |
|
173 | + */ |
|
174 | + public static function str2Hex($str) |
|
175 | + { |
|
176 | + return bin2hex($str); |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Convert a string of characters to a decimal number |
|
182 | + * |
|
183 | + * @param string $str The string to convert to decimal |
|
184 | + * @return integer The integer converted from the string |
|
185 | + */ |
|
186 | + public static function str2Dec($str) |
|
187 | + { |
|
188 | + $hex = self::str2Hex($str); |
|
189 | + return self::hex2Dec($hex); |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * Converts a string to binary representation (ie 01101011) |
|
195 | + * |
|
196 | + * @param string $str A string |
|
197 | + * @return string A binary representation of the the string |
|
198 | + */ |
|
199 | + public static function str2Bin($str) |
|
200 | + { |
|
201 | + $hex = self::str2Hex($str); |
|
202 | + $parts = str_split($hex, 2); |
|
203 | + |
|
204 | + $parts = array_map(function($v) { |
|
205 | + return Core::hex2Bin($v); |
|
206 | + }, $parts); |
|
207 | + |
|
208 | + return implode("", $parts); |
|
209 | + } |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Converts Decimal to Hex |
|
214 | + * This function just calls php's dechex() function, but I |
|
215 | + * encapsulated it in this function to keep things uniform |
|
216 | + * and have all possible conversion function available in |
|
217 | + * the Cipher class |
|
218 | + * |
|
219 | + * The parameter $req_bytes will pad the return hex with NULL (00) |
|
220 | + * until the hex represents the number of bytes given to $req_bytes |
|
221 | + * This is because dechex() drops null bytes from the Hex, which may |
|
222 | + * be needed in some cases |
|
223 | + * |
|
224 | + * @param integer $dec A decimal number to convert |
|
225 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
226 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
227 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
228 | + * size than the initial integer. |
|
229 | + * @return string A hexidecimal representation of the decimal number |
|
230 | + */ |
|
231 | + public static function dec2Hex($dec, $req_bytes = 0) |
|
232 | + { |
|
233 | + $hex = dechex($dec); |
|
234 | + |
|
235 | + // if we do not have an even number of hex characters |
|
236 | + // append a 0 to the beginning. dechex() drops leading 0's |
|
237 | + if(strlen($hex) % 2) |
|
238 | + $hex = "0$hex"; |
|
239 | + |
|
240 | + // if the number of bytes in the hex is less than |
|
241 | + // what we need it to be, add null bytes to the |
|
242 | + // front of the hex to padd it to the required size |
|
243 | + if(($req_bytes * 2) > strlen($hex)) |
|
244 | + $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
245 | + |
|
246 | + return $hex; |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * Converts Decimal to Binary |
|
252 | + * This function just calls php's decbin() function, but I |
|
253 | + * encapsulated it in this function to keep things uniform |
|
254 | + * and have all possible conversion function available in |
|
255 | + * the Cipher class |
|
256 | + * |
|
257 | + * @param integer $dec A decimal number to convert |
|
258 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
259 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
260 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
261 | + * size than the initial integer. |
|
262 | + * @return string A binary representation of the decimal number |
|
263 | + */ |
|
264 | + public static function dec2Bin($dec, $req_bytes = 0) |
|
265 | + { |
|
266 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
267 | + return self::hex2Bin($hex); |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * Convert a decimal to a string of bytes |
|
273 | + * |
|
274 | + * @param integer $dec A decimal number |
|
275 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
276 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
277 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
278 | + * size than the initial integer. |
|
279 | + * @return string A string with the number of bytes equal to $dec |
|
280 | + */ |
|
281 | + public static function dec2Str($dec, $req_bytes = 0) |
|
282 | + { |
|
283 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
284 | + return self::hex2Str($hex); |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * XORs two binary strings (representation of binary, ie 01101011), |
|
290 | + * assumed to be equal length |
|
291 | + * |
|
292 | + * @param string $a A string that represents binary |
|
293 | + * @param string $b A string that represents binary |
|
294 | + * @return string A representation of binary |
|
295 | + */ |
|
296 | + public static function xorBin($a, $b) |
|
297 | + { |
|
298 | + $len_a = strlen($a); |
|
299 | + $len_b = strlen($b); |
|
300 | + $width = $len_a; |
|
301 | + |
|
302 | + // first determine if the two binary strings are the same length, |
|
303 | + // and if not get them to the same length |
|
304 | + if($len_a > $len_b) |
|
305 | + { |
|
306 | + $width = $len_a; |
|
307 | + $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
308 | + } |
|
309 | + else if($len_a < $len_b) |
|
310 | + { |
|
311 | + $width = $len_b; |
|
312 | + $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
313 | + } |
|
314 | + |
|
315 | + // fortunately PHP knows how to XOR each byte in a string |
|
316 | + // so we don't have to loop to do it |
|
317 | + $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
318 | + return self::str2Bin($bin); |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
324 | + * The values are string representations of hex values |
|
325 | + * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
326 | + * |
|
327 | + * @param string Unlimited number parameters, each a string representation of hex |
|
328 | + * @return string A string representation of the result in Hex |
|
329 | + */ |
|
330 | + public static function xorHex() |
|
331 | + { |
|
332 | + $hex = func_get_args(); |
|
333 | + $count = func_num_args(); |
|
334 | + |
|
335 | + // we need a minimum of 2 values |
|
336 | + if($count < 2) |
|
337 | + return false; |
|
338 | + |
|
339 | + // first get all hex values to an even number |
|
340 | + array_walk($hex, function(&$val, $i){ |
|
341 | + if(strlen($val) % 2) |
|
342 | + $val = "0".$val; |
|
343 | + }); |
|
344 | + |
|
345 | + $res = 0; |
|
346 | + for($i = 0; $i < $count; ++$i) |
|
347 | + { |
|
348 | + // if this is the first loop, set the 'result' to the first |
|
349 | + // hex value |
|
350 | + if($i == 0) |
|
351 | + $res = $hex[0]; |
|
352 | + else |
|
353 | + { |
|
354 | + // to make the code easier to follow |
|
355 | + $h1 = $res; |
|
356 | + $h2 = $hex[$i]; |
|
357 | + |
|
358 | + // get lengths |
|
359 | + $len1 = strlen($h1); |
|
360 | + $len2 = strlen($h2); |
|
361 | + |
|
362 | + // now check that both hex values are the same length, |
|
363 | + // if not pad them with 0's until they are |
|
364 | + if($len1 > $len2) |
|
365 | + $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
366 | + else if($len1 < $len2) |
|
367 | + $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
368 | + |
|
369 | + // PHP knows how to XOR each byte in a string, so convert the |
|
370 | + // hex to a string, XOR, and convert back |
|
371 | + $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
372 | + $res = self::str2Hex($res); |
|
373 | + } |
|
374 | + } |
|
375 | + |
|
376 | + return $res; |
|
377 | + } |
|
378 | + |
|
379 | + |
|
380 | + /** |
|
381 | + * Forces an integer to be signed |
|
382 | + * |
|
383 | + * @param integer $int An integer |
|
384 | + * @return integer An signed integer |
|
385 | + */ |
|
386 | + public static function sInt($int) |
|
387 | + { |
|
388 | + $arr = unpack("i", pack("i", $int)); |
|
389 | + return $arr[1]; |
|
390 | + } |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Forces an integer to be unsigned |
|
395 | + * |
|
396 | + * @param integer $int A signed integer |
|
397 | + * @return integer An unsigned integer |
|
398 | + */ |
|
399 | + public static function uInt($int) |
|
400 | + { |
|
401 | + $arr = unpack("I", pack("I", $int)); |
|
402 | + $ret = $arr[1]; |
|
403 | + |
|
404 | + // On 32 bit platforms unpack() and pack() do not convert |
|
405 | + // from signed to unsigned properly all the time, it will return |
|
406 | + // the same negative number given to it, the work around is |
|
407 | + // to use sprintf(). |
|
408 | + // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
409 | + if($ret < 0) |
|
410 | + $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
411 | + |
|
412 | + return $ret; |
|
413 | + } |
|
414 | + |
|
415 | + |
|
416 | + /** |
|
417 | + * Forces an integer to be a 32 bit signed integer |
|
418 | + * |
|
419 | + * @param integer $int An integer |
|
420 | + * @return integer An signed 32 bit integer |
|
421 | + */ |
|
422 | + public static function sInt32($int) |
|
423 | + { |
|
424 | + if(PHP_INT_SIZE === 4) // 32 bit |
|
425 | + return self::sInt($int); |
|
426 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
427 | + { |
|
428 | + $arr = unpack("l", pack("l", $int)); |
|
429 | + return $arr[1]; |
|
430 | + } |
|
431 | + } |
|
432 | + |
|
433 | + |
|
434 | + /** |
|
435 | + * Force an integer to be a 32 bit unsigned integer |
|
436 | + * |
|
437 | + * @param integer $int An integer |
|
438 | + * @return integer An unsigned 32 bit integer |
|
439 | + */ |
|
440 | + public static function uInt32($int) |
|
441 | + { |
|
442 | + if(PHP_INT_SIZE === 4) // 32 bit |
|
443 | + return self::uInt($int); |
|
444 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
445 | + { |
|
446 | + $arr = unpack("L", pack("L", $int)); |
|
447 | + return $arr[1]; |
|
448 | + } |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + /** |
|
453 | + * Converts an integer to the value for an signed char |
|
454 | + * |
|
455 | + * @param integer $int The integer to convert to a signed char |
|
456 | + * @return integer A signed integer, representing a signed char |
|
457 | + */ |
|
458 | + public static function sChar($int) |
|
459 | + { |
|
460 | + $arr = unpack("c", pack("c", $int)); |
|
461 | + return $arr[1]; |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + /** |
|
466 | + * Converts an integer to the value for an unsigned char |
|
467 | + * |
|
468 | + * @param integer $int The integer to convert to a unsigned char |
|
469 | + * @return integer An unsigned integer, representing a unsigned char |
|
470 | + */ |
|
471 | + public static function uChar($int) |
|
472 | + { |
|
473 | + $arr = unpack("C", pack("C", $int)); |
|
474 | + return $arr[1]; |
|
475 | + } |
|
476 | + |
|
477 | + |
|
478 | + /** |
|
479 | + * Rotates bits Left, appending the bits pushed off the left onto the right |
|
480 | + * |
|
481 | + * @param integer $n The integer to rotate bits to the left |
|
482 | + * @param integer $shifts The number of shifts left to make |
|
483 | + * @return integer The resulting value from the rotation |
|
484 | + */ |
|
485 | + public static function rotBitsLeft32($i, $shifts) |
|
486 | + { |
|
487 | + if($shifts <= 0) |
|
488 | + return $i; |
|
489 | + |
|
490 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
491 | + |
|
492 | + // this is causing problems on 32 bit platform |
|
493 | + //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
494 | + |
|
495 | + // so lets cheat: convert to binary string, rotate left, and |
|
496 | + // convert back to decimal |
|
497 | + $i = self::dec2Bin(self::uInt32($i), 4); |
|
498 | + $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
499 | + return self::bin2Dec($i); |
|
500 | + |
|
501 | + } |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * Rotates bits right, appending the bits pushed off the right onto the left |
|
506 | + * |
|
507 | + * @param integer $n The integer to rotate bits to the right |
|
508 | + * @param integer $shifts The number of shifts right to make |
|
509 | + * @return integer The resulting value from the rotation |
|
510 | + */ |
|
511 | + public static function rotBitsRight32($i, $shifts) |
|
512 | + { |
|
513 | + if($shifts <= 0) |
|
514 | + return $i; |
|
515 | + |
|
516 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
517 | + |
|
518 | + // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
519 | + // having a problem with some bit shifts on 32 bits |
|
520 | + // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
521 | + |
|
522 | + // so lets cheat: convert to binary string, rotate right, |
|
523 | + // and convert back to decimal |
|
524 | + $i = self::dec2Bin($i, 4); |
|
525 | + $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
526 | + return self::bin2Dec($i); |
|
527 | + } |
|
528 | + |
|
529 | + |
|
530 | + /** |
|
531 | + * Create a string of random bytes, used for creating an IV |
|
532 | + * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
533 | + * There are 4 ways to auto generate random bytes by setting $src parameter |
|
534 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
535 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
536 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
537 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
538 | + * |
|
539 | + * @param string $src Optional, Use the $src to create the random bytes |
|
540 | + * by default PHP_Crypt::RAND is used when $src is not specified |
|
541 | + * @param integer $byte_len The length of the byte string to create |
|
542 | + * @return string A random string of bytes |
|
543 | + */ |
|
544 | + public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
545 | + { |
|
546 | + $bytes = ""; |
|
547 | + $err_msg = ""; |
|
548 | + |
|
549 | + if($src == PHP_Crypt::RAND_DEV_RAND) |
|
550 | + { |
|
551 | + if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
552 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
553 | + else |
|
554 | + $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
555 | + } |
|
556 | + else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
557 | + { |
|
558 | + if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
559 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
560 | + else |
|
561 | + $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
562 | + } |
|
563 | + else if($src == PHP_Crypt::RAND_WIN_COM) |
|
564 | + { |
|
565 | + if(extension_loaded('com_dotnet')) |
|
566 | + { |
|
567 | + // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
568 | + try |
|
569 | + { |
|
570 | + // request a random number in $byte_len bytes, returned |
|
571 | + // as base_64 encoded string. This is because PHP munges the |
|
572 | + // binary data on Windows |
|
573 | + $com = @new \COM("CAPICOM.Utilities.1"); |
|
574 | + $bytes = $com->GetRandom($byte_len, 0); |
|
575 | + } |
|
576 | + catch(Exception $e) |
|
577 | + { |
|
578 | + $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
579 | + } |
|
580 | + |
|
581 | + if(!$bytes) |
|
582 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
583 | + } |
|
584 | + else |
|
585 | + $err_msg = "The COM_DOTNET extension is not loaded"; |
|
586 | + } |
|
587 | + |
|
588 | + // trigger a warning if something went wrong |
|
589 | + if($err_msg != "") |
|
590 | + trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
591 | + |
|
592 | + // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
593 | + // passed as the $src param, create the bytes using mt_rand(). It's not |
|
594 | + // the most secure option but we have no other choice |
|
595 | + if(strlen($bytes) < $byte_len) |
|
596 | + { |
|
597 | + $bytes = ""; |
|
598 | + |
|
599 | + // md5() hash a random number to get a 16 byte string, keep looping |
|
600 | + // until we have a string as long or longer than the ciphers block size |
|
601 | + for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
602 | + $bytes .= md5(mt_rand(), true); |
|
603 | + } |
|
604 | + |
|
605 | + // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
606 | + // cryptographically secure, lets add another layer of 'randomness' before |
|
607 | + // the final md5() below |
|
608 | + $bytes = str_shuffle($bytes); |
|
609 | + |
|
610 | + // md5() the $bytes to add extra randomness. Since md5() only returns |
|
611 | + // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
612 | + // some ciphers which have a block size larger than 16 bytes |
|
613 | + $tmp = ""; |
|
614 | + $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
615 | + for($i = 0; $i < $loop; ++$i) |
|
616 | + $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
617 | + |
|
618 | + // grab the number of bytes equal to the requested $byte_len |
|
619 | + return substr($tmp, 0, $byte_len); |
|
620 | + } |
|
621 | 621 | } |
622 | 622 | ?> |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if we do not have an even number of hex characters |
69 | 69 | // append a 0 to the beginning to make it even |
70 | - if(strlen($hex) % 2) |
|
70 | + if (strlen($hex) % 2) |
|
71 | 71 | $hex = "0$hex"; |
72 | 72 | |
73 | 73 | $parts = str_split($hex, 2); |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | { |
91 | 91 | // php version >= 5.4 have a hex2bin function, use it |
92 | 92 | // if it exists |
93 | - if(function_exists("hex2bin")) |
|
93 | + if (function_exists("hex2bin")) |
|
94 | 94 | return hex2bin($hex); |
95 | 95 | |
96 | 96 | $parts = str_split($hex, 2); |
@@ -234,13 +234,13 @@ discard block |
||
234 | 234 | |
235 | 235 | // if we do not have an even number of hex characters |
236 | 236 | // append a 0 to the beginning. dechex() drops leading 0's |
237 | - if(strlen($hex) % 2) |
|
237 | + if (strlen($hex) % 2) |
|
238 | 238 | $hex = "0$hex"; |
239 | 239 | |
240 | 240 | // if the number of bytes in the hex is less than |
241 | 241 | // what we need it to be, add null bytes to the |
242 | 242 | // front of the hex to padd it to the required size |
243 | - if(($req_bytes * 2) > strlen($hex)) |
|
243 | + if (($req_bytes * 2) > strlen($hex)) |
|
244 | 244 | $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
245 | 245 | |
246 | 246 | return $hex; |
@@ -301,12 +301,12 @@ discard block |
||
301 | 301 | |
302 | 302 | // first determine if the two binary strings are the same length, |
303 | 303 | // and if not get them to the same length |
304 | - if($len_a > $len_b) |
|
304 | + if ($len_a > $len_b) |
|
305 | 305 | { |
306 | 306 | $width = $len_a; |
307 | 307 | $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
308 | 308 | } |
309 | - else if($len_a < $len_b) |
|
309 | + else if ($len_a < $len_b) |
|
310 | 310 | { |
311 | 311 | $width = $len_b; |
312 | 312 | $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
@@ -333,21 +333,21 @@ discard block |
||
333 | 333 | $count = func_num_args(); |
334 | 334 | |
335 | 335 | // we need a minimum of 2 values |
336 | - if($count < 2) |
|
336 | + if ($count < 2) |
|
337 | 337 | return false; |
338 | 338 | |
339 | 339 | // first get all hex values to an even number |
340 | - array_walk($hex, function(&$val, $i){ |
|
341 | - if(strlen($val) % 2) |
|
340 | + array_walk($hex, function(&$val, $i) { |
|
341 | + if (strlen($val) % 2) |
|
342 | 342 | $val = "0".$val; |
343 | 343 | }); |
344 | 344 | |
345 | 345 | $res = 0; |
346 | - for($i = 0; $i < $count; ++$i) |
|
346 | + for ($i = 0; $i < $count; ++$i) |
|
347 | 347 | { |
348 | 348 | // if this is the first loop, set the 'result' to the first |
349 | 349 | // hex value |
350 | - if($i == 0) |
|
350 | + if ($i == 0) |
|
351 | 351 | $res = $hex[0]; |
352 | 352 | else |
353 | 353 | { |
@@ -361,9 +361,9 @@ discard block |
||
361 | 361 | |
362 | 362 | // now check that both hex values are the same length, |
363 | 363 | // if not pad them with 0's until they are |
364 | - if($len1 > $len2) |
|
364 | + if ($len1 > $len2) |
|
365 | 365 | $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
366 | - else if($len1 < $len2) |
|
366 | + else if ($len1 < $len2) |
|
367 | 367 | $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
368 | 368 | |
369 | 369 | // PHP knows how to XOR each byte in a string, so convert the |
@@ -406,7 +406,7 @@ discard block |
||
406 | 406 | // the same negative number given to it, the work around is |
407 | 407 | // to use sprintf(). |
408 | 408 | // Tested with php 5.3.x on Windows XP & Linux 32bit |
409 | - if($ret < 0) |
|
409 | + if ($ret < 0) |
|
410 | 410 | $ret = sprintf("%u", $ret) + 0; // convert from string to int |
411 | 411 | |
412 | 412 | return $ret; |
@@ -421,7 +421,7 @@ discard block |
||
421 | 421 | */ |
422 | 422 | public static function sInt32($int) |
423 | 423 | { |
424 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
424 | + if (PHP_INT_SIZE === 4) // 32 bit |
|
425 | 425 | return self::sInt($int); |
426 | 426 | else // PHP_INT_SIZE === 8 // 64 bit |
427 | 427 | { |
@@ -439,7 +439,7 @@ discard block |
||
439 | 439 | */ |
440 | 440 | public static function uInt32($int) |
441 | 441 | { |
442 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
442 | + if (PHP_INT_SIZE === 4) // 32 bit |
|
443 | 443 | return self::uInt($int); |
444 | 444 | else // PHP_INT_SIZE === 8 // 64 bit |
445 | 445 | { |
@@ -484,7 +484,7 @@ discard block |
||
484 | 484 | */ |
485 | 485 | public static function rotBitsLeft32($i, $shifts) |
486 | 486 | { |
487 | - if($shifts <= 0) |
|
487 | + if ($shifts <= 0) |
|
488 | 488 | return $i; |
489 | 489 | |
490 | 490 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | */ |
511 | 511 | public static function rotBitsRight32($i, $shifts) |
512 | 512 | { |
513 | - if($shifts <= 0) |
|
513 | + if ($shifts <= 0) |
|
514 | 514 | return $i; |
515 | 515 | |
516 | 516 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
@@ -546,23 +546,23 @@ discard block |
||
546 | 546 | $bytes = ""; |
547 | 547 | $err_msg = ""; |
548 | 548 | |
549 | - if($src == PHP_Crypt::RAND_DEV_RAND) |
|
549 | + if ($src == PHP_Crypt::RAND_DEV_RAND) |
|
550 | 550 | { |
551 | - if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
551 | + if (file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
552 | 552 | $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
553 | 553 | else |
554 | 554 | $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
555 | 555 | } |
556 | - else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
556 | + else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
557 | 557 | { |
558 | - if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
558 | + if (file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
559 | 559 | $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
560 | 560 | else |
561 | 561 | $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
562 | 562 | } |
563 | - else if($src == PHP_Crypt::RAND_WIN_COM) |
|
563 | + else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
564 | 564 | { |
565 | - if(extension_loaded('com_dotnet')) |
|
565 | + if (extension_loaded('com_dotnet')) |
|
566 | 566 | { |
567 | 567 | // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
568 | 568 | try |
@@ -573,32 +573,32 @@ discard block |
||
573 | 573 | $com = @new \COM("CAPICOM.Utilities.1"); |
574 | 574 | $bytes = $com->GetRandom($byte_len, 0); |
575 | 575 | } |
576 | - catch(Exception $e) |
|
576 | + catch (Exception $e) |
|
577 | 577 | { |
578 | - $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
578 | + $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
579 | 579 | } |
580 | 580 | |
581 | - if(!$bytes) |
|
582 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
581 | + if (!$bytes) |
|
582 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
583 | 583 | } |
584 | 584 | else |
585 | 585 | $err_msg = "The COM_DOTNET extension is not loaded"; |
586 | 586 | } |
587 | 587 | |
588 | 588 | // trigger a warning if something went wrong |
589 | - if($err_msg != "") |
|
589 | + if ($err_msg != "") |
|
590 | 590 | trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
591 | 591 | |
592 | 592 | // if the random bytes where not created properly or PHP_Crypt::RAND was |
593 | 593 | // passed as the $src param, create the bytes using mt_rand(). It's not |
594 | 594 | // the most secure option but we have no other choice |
595 | - if(strlen($bytes) < $byte_len) |
|
595 | + if (strlen($bytes) < $byte_len) |
|
596 | 596 | { |
597 | 597 | $bytes = ""; |
598 | 598 | |
599 | 599 | // md5() hash a random number to get a 16 byte string, keep looping |
600 | 600 | // until we have a string as long or longer than the ciphers block size |
601 | - for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
601 | + for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
602 | 602 | $bytes .= md5(mt_rand(), true); |
603 | 603 | } |
604 | 604 | |
@@ -612,7 +612,7 @@ discard block |
||
612 | 612 | // some ciphers which have a block size larger than 16 bytes |
613 | 613 | $tmp = ""; |
614 | 614 | $loop = ceil(strlen($bytes) / self::HASH_LEN); |
615 | - for($i = 0; $i < $loop; ++$i) |
|
615 | + for ($i = 0; $i < $loop; ++$i) |
|
616 | 616 | $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
617 | 617 | |
618 | 618 | // grab the number of bytes equal to the requested $byte_len |
@@ -67,8 +67,9 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if we do not have an even number of hex characters |
69 | 69 | // append a 0 to the beginning to make it even |
70 | - if(strlen($hex) % 2) |
|
71 | - $hex = "0$hex"; |
|
70 | + if(strlen($hex) % 2) { |
|
71 | + $hex = "0$hex"; |
|
72 | + } |
|
72 | 73 | |
73 | 74 | $parts = str_split($hex, 2); |
74 | 75 | $parts = array_map(function($v) { |
@@ -90,8 +91,9 @@ discard block |
||
90 | 91 | { |
91 | 92 | // php version >= 5.4 have a hex2bin function, use it |
92 | 93 | // if it exists |
93 | - if(function_exists("hex2bin")) |
|
94 | - return hex2bin($hex); |
|
94 | + if(function_exists("hex2bin")) { |
|
95 | + return hex2bin($hex); |
|
96 | + } |
|
95 | 97 | |
96 | 98 | $parts = str_split($hex, 2); |
97 | 99 | $parts = array_map(function($v) { |
@@ -234,14 +236,16 @@ discard block |
||
234 | 236 | |
235 | 237 | // if we do not have an even number of hex characters |
236 | 238 | // append a 0 to the beginning. dechex() drops leading 0's |
237 | - if(strlen($hex) % 2) |
|
238 | - $hex = "0$hex"; |
|
239 | + if(strlen($hex) % 2) { |
|
240 | + $hex = "0$hex"; |
|
241 | + } |
|
239 | 242 | |
240 | 243 | // if the number of bytes in the hex is less than |
241 | 244 | // what we need it to be, add null bytes to the |
242 | 245 | // front of the hex to padd it to the required size |
243 | - if(($req_bytes * 2) > strlen($hex)) |
|
244 | - $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
246 | + if(($req_bytes * 2) > strlen($hex)) { |
|
247 | + $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
248 | + } |
|
245 | 249 | |
246 | 250 | return $hex; |
247 | 251 | } |
@@ -305,8 +309,7 @@ discard block |
||
305 | 309 | { |
306 | 310 | $width = $len_a; |
307 | 311 | $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
308 | - } |
|
309 | - else if($len_a < $len_b) |
|
312 | + } else if($len_a < $len_b) |
|
310 | 313 | { |
311 | 314 | $width = $len_b; |
312 | 315 | $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
@@ -333,13 +336,15 @@ discard block |
||
333 | 336 | $count = func_num_args(); |
334 | 337 | |
335 | 338 | // we need a minimum of 2 values |
336 | - if($count < 2) |
|
337 | - return false; |
|
339 | + if($count < 2) { |
|
340 | + return false; |
|
341 | + } |
|
338 | 342 | |
339 | 343 | // first get all hex values to an even number |
340 | 344 | array_walk($hex, function(&$val, $i){ |
341 | - if(strlen($val) % 2) |
|
342 | - $val = "0".$val; |
|
345 | + if(strlen($val) % 2) { |
|
346 | + $val = "0".$val; |
|
347 | + } |
|
343 | 348 | }); |
344 | 349 | |
345 | 350 | $res = 0; |
@@ -347,9 +352,9 @@ discard block |
||
347 | 352 | { |
348 | 353 | // if this is the first loop, set the 'result' to the first |
349 | 354 | // hex value |
350 | - if($i == 0) |
|
351 | - $res = $hex[0]; |
|
352 | - else |
|
355 | + if($i == 0) { |
|
356 | + $res = $hex[0]; |
|
357 | + } else |
|
353 | 358 | { |
354 | 359 | // to make the code easier to follow |
355 | 360 | $h1 = $res; |
@@ -361,10 +366,11 @@ discard block |
||
361 | 366 | |
362 | 367 | // now check that both hex values are the same length, |
363 | 368 | // if not pad them with 0's until they are |
364 | - if($len1 > $len2) |
|
365 | - $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
366 | - else if($len1 < $len2) |
|
367 | - $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
369 | + if($len1 > $len2) { |
|
370 | + $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
371 | + } else if($len1 < $len2) { |
|
372 | + $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
373 | + } |
|
368 | 374 | |
369 | 375 | // PHP knows how to XOR each byte in a string, so convert the |
370 | 376 | // hex to a string, XOR, and convert back |
@@ -406,8 +412,10 @@ discard block |
||
406 | 412 | // the same negative number given to it, the work around is |
407 | 413 | // to use sprintf(). |
408 | 414 | // Tested with php 5.3.x on Windows XP & Linux 32bit |
409 | - if($ret < 0) |
|
410 | - $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
415 | + if($ret < 0) { |
|
416 | + $ret = sprintf("%u", $ret) + 0; |
|
417 | + } |
|
418 | + // convert from string to int |
|
411 | 419 | |
412 | 420 | return $ret; |
413 | 421 | } |
@@ -421,9 +429,10 @@ discard block |
||
421 | 429 | */ |
422 | 430 | public static function sInt32($int) |
423 | 431 | { |
424 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
432 | + if(PHP_INT_SIZE === 4) { |
|
433 | + // 32 bit |
|
425 | 434 | return self::sInt($int); |
426 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
435 | + } else // PHP_INT_SIZE === 8 // 64 bit |
|
427 | 436 | { |
428 | 437 | $arr = unpack("l", pack("l", $int)); |
429 | 438 | return $arr[1]; |
@@ -439,9 +448,10 @@ discard block |
||
439 | 448 | */ |
440 | 449 | public static function uInt32($int) |
441 | 450 | { |
442 | - if(PHP_INT_SIZE === 4) // 32 bit |
|
451 | + if(PHP_INT_SIZE === 4) { |
|
452 | + // 32 bit |
|
443 | 453 | return self::uInt($int); |
444 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
454 | + } else // PHP_INT_SIZE === 8 // 64 bit |
|
445 | 455 | { |
446 | 456 | $arr = unpack("L", pack("L", $int)); |
447 | 457 | return $arr[1]; |
@@ -484,8 +494,9 @@ discard block |
||
484 | 494 | */ |
485 | 495 | public static function rotBitsLeft32($i, $shifts) |
486 | 496 | { |
487 | - if($shifts <= 0) |
|
488 | - return $i; |
|
497 | + if($shifts <= 0) { |
|
498 | + return $i; |
|
499 | + } |
|
489 | 500 | |
490 | 501 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
491 | 502 | |
@@ -510,8 +521,9 @@ discard block |
||
510 | 521 | */ |
511 | 522 | public static function rotBitsRight32($i, $shifts) |
512 | 523 | { |
513 | - if($shifts <= 0) |
|
514 | - return $i; |
|
524 | + if($shifts <= 0) { |
|
525 | + return $i; |
|
526 | + } |
|
515 | 527 | |
516 | 528 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
517 | 529 | |
@@ -548,19 +560,19 @@ discard block |
||
548 | 560 | |
549 | 561 | if($src == PHP_Crypt::RAND_DEV_RAND) |
550 | 562 | { |
551 | - if(file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
552 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
553 | - else |
|
554 | - $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
555 | - } |
|
556 | - else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
563 | + if(file_exists(PHP_Crypt::RAND_DEV_RAND)) { |
|
564 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
565 | + } else { |
|
566 | + $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
567 | + } |
|
568 | + } else if($src == PHP_Crypt::RAND_DEV_URAND) |
|
557 | 569 | { |
558 | - if(file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
559 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
560 | - else |
|
561 | - $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
562 | - } |
|
563 | - else if($src == PHP_Crypt::RAND_WIN_COM) |
|
570 | + if(file_exists(PHP_Crypt::RAND_DEV_URAND)) { |
|
571 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
572 | + } else { |
|
573 | + $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
574 | + } |
|
575 | + } else if($src == PHP_Crypt::RAND_WIN_COM) |
|
564 | 576 | { |
565 | 577 | if(extension_loaded('com_dotnet')) |
566 | 578 | { |
@@ -572,22 +584,23 @@ discard block |
||
572 | 584 | // binary data on Windows |
573 | 585 | $com = @new \COM("CAPICOM.Utilities.1"); |
574 | 586 | $bytes = $com->GetRandom($byte_len, 0); |
575 | - } |
|
576 | - catch(Exception $e) |
|
587 | + } catch(Exception $e) |
|
577 | 588 | { |
578 | 589 | $err_msg = "Windows COM exception: ".$e->getMessage(); |
579 | 590 | } |
580 | 591 | |
581 | - if(!$bytes) |
|
582 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
592 | + if(!$bytes) { |
|
593 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
594 | + } |
|
595 | + } else { |
|
596 | + $err_msg = "The COM_DOTNET extension is not loaded"; |
|
583 | 597 | } |
584 | - else |
|
585 | - $err_msg = "The COM_DOTNET extension is not loaded"; |
|
586 | 598 | } |
587 | 599 | |
588 | 600 | // trigger a warning if something went wrong |
589 | - if($err_msg != "") |
|
590 | - trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
601 | + if($err_msg != "") { |
|
602 | + trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
603 | + } |
|
591 | 604 | |
592 | 605 | // if the random bytes where not created properly or PHP_Crypt::RAND was |
593 | 606 | // passed as the $src param, create the bytes using mt_rand(). It's not |
@@ -598,8 +611,9 @@ discard block |
||
598 | 611 | |
599 | 612 | // md5() hash a random number to get a 16 byte string, keep looping |
600 | 613 | // until we have a string as long or longer than the ciphers block size |
601 | - for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
602 | - $bytes .= md5(mt_rand(), true); |
|
614 | + for($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) { |
|
615 | + $bytes .= md5(mt_rand(), true); |
|
616 | + } |
|
603 | 617 | } |
604 | 618 | |
605 | 619 | // because $bytes may have come from mt_rand() or /dev/urandom which are not |
@@ -612,8 +626,9 @@ discard block |
||
612 | 626 | // some ciphers which have a block size larger than 16 bytes |
613 | 627 | $tmp = ""; |
614 | 628 | $loop = ceil(strlen($bytes) / self::HASH_LEN); |
615 | - for($i = 0; $i < $loop; ++$i) |
|
616 | - $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
629 | + for($i = 0; $i < $loop; ++$i) { |
|
630 | + $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
631 | + } |
|
617 | 632 | |
618 | 633 | // grab the number of bytes equal to the requested $byte_len |
619 | 634 | return substr($tmp, 0, $byte_len); |
@@ -240,7 +240,7 @@ |
||
240 | 240 | * returns the the padding type only. |
241 | 241 | * |
242 | 242 | * @param string $type One of the predefined padding types |
243 | - * @return void |
|
243 | + * @return string |
|
244 | 244 | */ |
245 | 245 | public function padding($type = "") |
246 | 246 | { |
@@ -36,299 +36,299 @@ |
||
36 | 36 | */ |
37 | 37 | abstract class Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * @type object $cipher The cipher object used within the mode |
|
41 | - */ |
|
42 | - protected $cipher = null; |
|
43 | - |
|
44 | - /** |
|
45 | - * @type string $iv The IV used for the mode, not all Modes |
|
46 | - * use an IV so this may be empty |
|
47 | - */ |
|
48 | - protected $iv = ""; |
|
49 | - |
|
50 | - /** |
|
51 | - * @type string $register For modes that use a register to do |
|
52 | - * encryption/decryption. This stores the unencrypted register. |
|
53 | - */ |
|
54 | - protected $register = ""; |
|
55 | - |
|
56 | - /** |
|
57 | - * @type string $enc_register For modes that use a register to do |
|
58 | - * encryption/decryption. This stores the encrypted register |
|
59 | - */ |
|
60 | - protected $enc_register = ""; |
|
61 | - |
|
62 | - /** |
|
63 | - * @type integer $block_size The byte size of the block to |
|
64 | - * encrypt/decrypt for the Mode |
|
65 | - */ |
|
66 | - private $block_size = 0; |
|
67 | - |
|
68 | - /** @type string $mode_name The name of mode currently used */ |
|
69 | - private $mode_name = ""; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type string $padding The type of padding to use when required. |
|
73 | - * Padding types are defined in phpCrypt class. Defaults to |
|
74 | - * PHP_Crypt::PAD_ZERO |
|
75 | - */ |
|
76 | - private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * Constructor |
|
81 | - * Sets the cipher object that will be used for encryption |
|
82 | - * |
|
83 | - * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | - * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function __construct($mode_name, $cipher) |
|
88 | - { |
|
89 | - $this->name($mode_name); |
|
90 | - $this->cipher($cipher); |
|
91 | - $this->block_size = $this->cipher->blockSize(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Destructor |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - protected function __destruct() |
|
101 | - { |
|
102 | - |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /********************************************************************** |
|
39 | + /** |
|
40 | + * @type object $cipher The cipher object used within the mode |
|
41 | + */ |
|
42 | + protected $cipher = null; |
|
43 | + |
|
44 | + /** |
|
45 | + * @type string $iv The IV used for the mode, not all Modes |
|
46 | + * use an IV so this may be empty |
|
47 | + */ |
|
48 | + protected $iv = ""; |
|
49 | + |
|
50 | + /** |
|
51 | + * @type string $register For modes that use a register to do |
|
52 | + * encryption/decryption. This stores the unencrypted register. |
|
53 | + */ |
|
54 | + protected $register = ""; |
|
55 | + |
|
56 | + /** |
|
57 | + * @type string $enc_register For modes that use a register to do |
|
58 | + * encryption/decryption. This stores the encrypted register |
|
59 | + */ |
|
60 | + protected $enc_register = ""; |
|
61 | + |
|
62 | + /** |
|
63 | + * @type integer $block_size The byte size of the block to |
|
64 | + * encrypt/decrypt for the Mode |
|
65 | + */ |
|
66 | + private $block_size = 0; |
|
67 | + |
|
68 | + /** @type string $mode_name The name of mode currently used */ |
|
69 | + private $mode_name = ""; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type string $padding The type of padding to use when required. |
|
73 | + * Padding types are defined in phpCrypt class. Defaults to |
|
74 | + * PHP_Crypt::PAD_ZERO |
|
75 | + */ |
|
76 | + private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * Constructor |
|
81 | + * Sets the cipher object that will be used for encryption |
|
82 | + * |
|
83 | + * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | + * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function __construct($mode_name, $cipher) |
|
88 | + { |
|
89 | + $this->name($mode_name); |
|
90 | + $this->cipher($cipher); |
|
91 | + $this->block_size = $this->cipher->blockSize(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Destructor |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + protected function __destruct() |
|
101 | + { |
|
102 | + |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /********************************************************************** |
|
108 | 108 | * ABSTRACT METHODS |
109 | 109 | * |
110 | 110 | * The abstract methods required by inheriting classes to implement |
111 | 111 | **********************************************************************/ |
112 | 112 | |
113 | - /** |
|
114 | - * All modes must have encrypt(), which implements |
|
115 | - * the mode using the cipher's encrypiton algorithm |
|
116 | - * |
|
117 | - * @param string $text A String to encrypt |
|
118 | - * @return boolean Always returns true |
|
119 | - */ |
|
120 | - abstract public function encrypt(&$text); |
|
113 | + /** |
|
114 | + * All modes must have encrypt(), which implements |
|
115 | + * the mode using the cipher's encrypiton algorithm |
|
116 | + * |
|
117 | + * @param string $text A String to encrypt |
|
118 | + * @return boolean Always returns true |
|
119 | + */ |
|
120 | + abstract public function encrypt(&$text); |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * All modes must have decrypt(), which implements |
|
125 | - * the mode using the cipher's encryption algorithm |
|
126 | - * |
|
127 | - * @param string $text A String to decrypt |
|
128 | - * @return boolean Always returns true |
|
129 | - */ |
|
130 | - abstract public function decrypt(&$text); |
|
123 | + /** |
|
124 | + * All modes must have decrypt(), which implements |
|
125 | + * the mode using the cipher's encryption algorithm |
|
126 | + * |
|
127 | + * @param string $text A String to decrypt |
|
128 | + * @return boolean Always returns true |
|
129 | + */ |
|
130 | + abstract public function decrypt(&$text); |
|
131 | 131 | |
132 | 132 | |
133 | - /** |
|
134 | - * Indicates whether or not a mode requires an IV |
|
135 | - * |
|
136 | - * @return boolean Always returns true or false |
|
137 | - */ |
|
138 | - abstract public function requiresIV(); |
|
133 | + /** |
|
134 | + * Indicates whether or not a mode requires an IV |
|
135 | + * |
|
136 | + * @return boolean Always returns true or false |
|
137 | + */ |
|
138 | + abstract public function requiresIV(); |
|
139 | 139 | |
140 | 140 | |
141 | 141 | |
142 | 142 | |
143 | - /********************************************************************** |
|
143 | + /********************************************************************** |
|
144 | 144 | * PUBLIC METHODS |
145 | 145 | * |
146 | 146 | **********************************************************************/ |
147 | 147 | |
148 | - /** |
|
149 | - * Create an IV if the Mode used requires an IV. |
|
150 | - * The IV should be saved and used for Encryption/Decryption |
|
151 | - * of the same blocks of data. |
|
152 | - * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | - * |
|
158 | - * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | - * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | - * PHP_Crypt::RAND if none is given. |
|
161 | - * @return string The IV that is being used by the mode |
|
162 | - */ |
|
163 | - public function createIV($src = PHP_Crypt::RAND) |
|
164 | - { |
|
165 | - // if the mode does not use an IV, lets not waste time |
|
166 | - if(!$this->requiresIV()) |
|
167 | - return false; |
|
168 | - |
|
169 | - $iv = Core::randBytes($src, $this->block_size); |
|
170 | - return $this->IV($iv); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | - * is not given, this function only returns the current IV in use. |
|
177 | - * |
|
178 | - * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | - * @return string The current IV being used |
|
180 | - */ |
|
181 | - public function IV($iv = null) |
|
182 | - { |
|
183 | - if($iv != null) |
|
184 | - { |
|
185 | - // check that the iv is the correct length, |
|
186 | - $len = strlen($iv); |
|
187 | - if($len != $this->block_size) |
|
188 | - { |
|
189 | - $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | - trigger_error($msg, E_USER_WARNING); |
|
191 | - } |
|
192 | - |
|
193 | - $this->clearRegisters(); |
|
194 | - $this->register = $iv; |
|
195 | - $this->iv = $iv; |
|
196 | - } |
|
197 | - |
|
198 | - return $this->iv; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Checks to see if the current mode requires an IV and that it is set |
|
204 | - * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | - * |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - public function checkIV() |
|
209 | - { |
|
210 | - if($this->requiresIV() && strlen($this->register) == 0) |
|
211 | - { |
|
212 | - $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | - trigger_error($msg, E_USER_WARNING); |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * Sets and returns the name of the mode being used |
|
220 | - * If $name parameter is set, sets the mode. If |
|
221 | - * $name is not set, returns the current mode in use |
|
222 | - * |
|
223 | - * @param string $name Optional, One of the predefined |
|
224 | - * phpCrypt mode constant names |
|
225 | - * @return string One of the predefined phpCrypt mode |
|
226 | - * constant mode names |
|
227 | - */ |
|
228 | - public function name($name = "") |
|
229 | - { |
|
230 | - if($name != "") |
|
231 | - $this->mode_name = $name; |
|
232 | - |
|
233 | - return $this->mode_name; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Sets or Returns the padding type used with the mode |
|
239 | - * If the $type parameter is not given, this function |
|
240 | - * returns the the padding type only. |
|
241 | - * |
|
242 | - * @param string $type One of the predefined padding types |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - public function padding($type = "") |
|
246 | - { |
|
247 | - if($type != "") |
|
248 | - $this->padding = $type; |
|
249 | - |
|
250 | - return $this->padding; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns or Sets the cipher object being used |
|
256 | - * If the $cipher parameter is set with a cipher object, |
|
257 | - * the cipher current cipher will be set to this cipher. |
|
258 | - * If the $cipher parameter is not set, returns the |
|
259 | - * current cipher object being used. |
|
260 | - * |
|
261 | - * @param object $cipher Optional, An object of type Cipher |
|
262 | - * @return object An object of type Cipher |
|
263 | - */ |
|
264 | - public function cipher($cipher = null) |
|
265 | - { |
|
266 | - if(is_object($cipher)) |
|
267 | - $this->cipher = $cipher; |
|
268 | - |
|
269 | - return $this->cipher; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - |
|
274 | - |
|
275 | - /********************************************************************** |
|
148 | + /** |
|
149 | + * Create an IV if the Mode used requires an IV. |
|
150 | + * The IV should be saved and used for Encryption/Decryption |
|
151 | + * of the same blocks of data. |
|
152 | + * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | + * |
|
158 | + * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | + * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | + * PHP_Crypt::RAND if none is given. |
|
161 | + * @return string The IV that is being used by the mode |
|
162 | + */ |
|
163 | + public function createIV($src = PHP_Crypt::RAND) |
|
164 | + { |
|
165 | + // if the mode does not use an IV, lets not waste time |
|
166 | + if(!$this->requiresIV()) |
|
167 | + return false; |
|
168 | + |
|
169 | + $iv = Core::randBytes($src, $this->block_size); |
|
170 | + return $this->IV($iv); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | + * is not given, this function only returns the current IV in use. |
|
177 | + * |
|
178 | + * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | + * @return string The current IV being used |
|
180 | + */ |
|
181 | + public function IV($iv = null) |
|
182 | + { |
|
183 | + if($iv != null) |
|
184 | + { |
|
185 | + // check that the iv is the correct length, |
|
186 | + $len = strlen($iv); |
|
187 | + if($len != $this->block_size) |
|
188 | + { |
|
189 | + $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | + trigger_error($msg, E_USER_WARNING); |
|
191 | + } |
|
192 | + |
|
193 | + $this->clearRegisters(); |
|
194 | + $this->register = $iv; |
|
195 | + $this->iv = $iv; |
|
196 | + } |
|
197 | + |
|
198 | + return $this->iv; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Checks to see if the current mode requires an IV and that it is set |
|
204 | + * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | + * |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + public function checkIV() |
|
209 | + { |
|
210 | + if($this->requiresIV() && strlen($this->register) == 0) |
|
211 | + { |
|
212 | + $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | + trigger_error($msg, E_USER_WARNING); |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * Sets and returns the name of the mode being used |
|
220 | + * If $name parameter is set, sets the mode. If |
|
221 | + * $name is not set, returns the current mode in use |
|
222 | + * |
|
223 | + * @param string $name Optional, One of the predefined |
|
224 | + * phpCrypt mode constant names |
|
225 | + * @return string One of the predefined phpCrypt mode |
|
226 | + * constant mode names |
|
227 | + */ |
|
228 | + public function name($name = "") |
|
229 | + { |
|
230 | + if($name != "") |
|
231 | + $this->mode_name = $name; |
|
232 | + |
|
233 | + return $this->mode_name; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Sets or Returns the padding type used with the mode |
|
239 | + * If the $type parameter is not given, this function |
|
240 | + * returns the the padding type only. |
|
241 | + * |
|
242 | + * @param string $type One of the predefined padding types |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + public function padding($type = "") |
|
246 | + { |
|
247 | + if($type != "") |
|
248 | + $this->padding = $type; |
|
249 | + |
|
250 | + return $this->padding; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns or Sets the cipher object being used |
|
256 | + * If the $cipher parameter is set with a cipher object, |
|
257 | + * the cipher current cipher will be set to this cipher. |
|
258 | + * If the $cipher parameter is not set, returns the |
|
259 | + * current cipher object being used. |
|
260 | + * |
|
261 | + * @param object $cipher Optional, An object of type Cipher |
|
262 | + * @return object An object of type Cipher |
|
263 | + */ |
|
264 | + public function cipher($cipher = null) |
|
265 | + { |
|
266 | + if(is_object($cipher)) |
|
267 | + $this->cipher = $cipher; |
|
268 | + |
|
269 | + return $this->cipher; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + |
|
274 | + |
|
275 | + /********************************************************************** |
|
276 | 276 | * PROTECTED METHODS |
277 | 277 | * |
278 | 278 | **********************************************************************/ |
279 | 279 | |
280 | - /** |
|
281 | - * Pads str so that final block is $block_bits in size, if the final block |
|
282 | - * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | - * The padding should be set by phpCrypt::setPadding() |
|
284 | - * |
|
285 | - * @param string $str the string to be padded |
|
286 | - * @return boolean Returns true |
|
287 | - */ |
|
288 | - protected function pad(&$str) |
|
289 | - { |
|
290 | - $len = strlen($str); |
|
291 | - $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | - |
|
293 | - // now determine the next multiple of blockSize(), then find |
|
294 | - // the difference between that and the length of $str, |
|
295 | - // this is how many padding bytes we will need |
|
296 | - $num = ceil($len / $bytes) * $bytes; |
|
297 | - $num = $num - $len; |
|
298 | - |
|
299 | - Padding::pad($str, $num, $this->padding); |
|
300 | - return true; |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Strip out the padded blocks created from Pad(). |
|
306 | - * Padding type should be set by phpCrypt::setPadding() |
|
307 | - * |
|
308 | - * @param string $str the string to strip padding from |
|
309 | - * @return boolean Returns True |
|
310 | - */ |
|
311 | - protected function strip(&$str) |
|
312 | - { |
|
313 | - Padding::strip($str, $this->padding); |
|
314 | - return true; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /********************************************************************** |
|
280 | + /** |
|
281 | + * Pads str so that final block is $block_bits in size, if the final block |
|
282 | + * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | + * The padding should be set by phpCrypt::setPadding() |
|
284 | + * |
|
285 | + * @param string $str the string to be padded |
|
286 | + * @return boolean Returns true |
|
287 | + */ |
|
288 | + protected function pad(&$str) |
|
289 | + { |
|
290 | + $len = strlen($str); |
|
291 | + $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | + |
|
293 | + // now determine the next multiple of blockSize(), then find |
|
294 | + // the difference between that and the length of $str, |
|
295 | + // this is how many padding bytes we will need |
|
296 | + $num = ceil($len / $bytes) * $bytes; |
|
297 | + $num = $num - $len; |
|
298 | + |
|
299 | + Padding::pad($str, $num, $this->padding); |
|
300 | + return true; |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Strip out the padded blocks created from Pad(). |
|
306 | + * Padding type should be set by phpCrypt::setPadding() |
|
307 | + * |
|
308 | + * @param string $str the string to strip padding from |
|
309 | + * @return boolean Returns True |
|
310 | + */ |
|
311 | + protected function strip(&$str) |
|
312 | + { |
|
313 | + Padding::strip($str, $this->padding); |
|
314 | + return true; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /********************************************************************** |
|
319 | 319 | * PRIVATE METHODS |
320 | 320 | * |
321 | 321 | **********************************************************************/ |
322 | 322 | |
323 | - /** |
|
324 | - * Clears the registers used for some modes |
|
325 | - * |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function clearRegisters() |
|
329 | - { |
|
330 | - $this->register = ""; |
|
331 | - $this->enc_register = ""; |
|
332 | - } |
|
323 | + /** |
|
324 | + * Clears the registers used for some modes |
|
325 | + * |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function clearRegisters() |
|
329 | + { |
|
330 | + $this->register = ""; |
|
331 | + $this->enc_register = ""; |
|
332 | + } |
|
333 | 333 | } |
334 | 334 | ?> |
@@ -163,7 +163,7 @@ discard block |
||
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()) |
|
166 | + if (!$this->requiresIV()) |
|
167 | 167 | return false; |
168 | 168 | |
169 | 169 | $iv = Core::randBytes($src, $this->block_size); |
@@ -180,11 +180,11 @@ discard block |
||
180 | 180 | */ |
181 | 181 | public function IV($iv = null) |
182 | 182 | { |
183 | - if($iv != null) |
|
183 | + if ($iv != null) |
|
184 | 184 | { |
185 | 185 | // check that the iv is the correct length, |
186 | 186 | $len = strlen($iv); |
187 | - if($len != $this->block_size) |
|
187 | + if ($len != $this->block_size) |
|
188 | 188 | { |
189 | 189 | $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
190 | 190 | trigger_error($msg, E_USER_WARNING); |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | */ |
208 | 208 | public function checkIV() |
209 | 209 | { |
210 | - if($this->requiresIV() && strlen($this->register) == 0) |
|
210 | + if ($this->requiresIV() && strlen($this->register) == 0) |
|
211 | 211 | { |
212 | 212 | $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
213 | 213 | trigger_error($msg, E_USER_WARNING); |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | */ |
228 | 228 | public function name($name = "") |
229 | 229 | { |
230 | - if($name != "") |
|
230 | + if ($name != "") |
|
231 | 231 | $this->mode_name = $name; |
232 | 232 | |
233 | 233 | return $this->mode_name; |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | */ |
245 | 245 | public function padding($type = "") |
246 | 246 | { |
247 | - if($type != "") |
|
247 | + if ($type != "") |
|
248 | 248 | $this->padding = $type; |
249 | 249 | |
250 | 250 | return $this->padding; |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public function cipher($cipher = null) |
265 | 265 | { |
266 | - if(is_object($cipher)) |
|
266 | + if (is_object($cipher)) |
|
267 | 267 | $this->cipher = $cipher; |
268 | 268 | |
269 | 269 | return $this->cipher; |
@@ -163,8 +163,9 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 | } |
@@ -41,7 +41,6 @@ |
||
41 | 41 | * Sets the cipher object that will be used for encryption |
42 | 42 | * |
43 | 43 | * @param object $cipher one of the phpCrypt encryption cipher objects |
44 | - * @param int $block_size The size of blocks (in bits) used for encryption |
|
45 | 44 | * @return void |
46 | 45 | */ |
47 | 46 | public function __construct($cipher) |
@@ -36,107 +36,107 @@ |
||
36 | 36 | */ |
37 | 37 | class Mode_ECB extends Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * Sets the cipher object that will be used for encryption |
|
42 | - * |
|
43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | - * @param int $block_size The size of blocks (in bits) used for encryption |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function __construct($cipher) |
|
48 | - { |
|
49 | - parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
50 | - |
|
51 | - // this works with only block Ciphers |
|
52 | - if($cipher->type() != Cipher::BLOCK) |
|
53 | - trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Destructor |
|
59 | - * |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public function __destruct() |
|
63 | - { |
|
64 | - parent::__destruct(); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
70 | - * to the constructor in ECB mode |
|
71 | - * |
|
72 | - * @param string $text The string to be encrypted in ECB mode |
|
73 | - * @return boolean Returns true |
|
74 | - */ |
|
75 | - public function encrypt(&$text) |
|
76 | - { |
|
77 | - $this->pad($text); |
|
78 | - $blocksz = $this->cipher->blockSize(); |
|
79 | - |
|
80 | - $max = strlen($text) / $blocksz; |
|
81 | - for($i = 0; $i < $max; ++$i) |
|
82 | - { |
|
83 | - // get the current position within $text |
|
84 | - $pos = $i * $blocksz; |
|
85 | - |
|
86 | - // grab a block of text |
|
87 | - $block = substr($text, $pos, $blocksz); |
|
88 | - |
|
89 | - // encrypt the block |
|
90 | - $this->cipher->encrypt($block); |
|
91 | - |
|
92 | - // replace the plain text with the cipher text |
|
93 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
94 | - } |
|
95 | - |
|
96 | - return true; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
102 | - * to the constructor in ECB mode |
|
103 | - * |
|
104 | - * @param string $text The string to be decrypted in ECB mode |
|
105 | - * @return boolean Returns true |
|
106 | - */ |
|
107 | - public function decrypt(&$text) |
|
108 | - { |
|
109 | - $blocksz = $this->cipher->blockSize(); |
|
110 | - |
|
111 | - $max = strlen($text) / $blocksz; |
|
112 | - for($i = 0; $i < $max; ++$i) |
|
113 | - { |
|
114 | - // get the current position within $text |
|
115 | - $pos = $i * $blocksz; |
|
116 | - |
|
117 | - // get a block of cipher text |
|
118 | - $block = substr($text, $pos, $blocksz); |
|
119 | - |
|
120 | - // decrypt the block |
|
121 | - $this->cipher->decrypt($block); |
|
122 | - |
|
123 | - // replace the block of cipher text with plain text |
|
124 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
125 | - } |
|
126 | - |
|
127 | - $this->strip($text); |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * This mode does not require an IV |
|
134 | - * |
|
135 | - * @return boolean Returns false |
|
136 | - */ |
|
137 | - public function requiresIV() |
|
138 | - { |
|
139 | - return false; |
|
140 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * Sets the cipher object that will be used for encryption |
|
42 | + * |
|
43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | + * @param int $block_size The size of blocks (in bits) used for encryption |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function __construct($cipher) |
|
48 | + { |
|
49 | + parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
50 | + |
|
51 | + // this works with only block Ciphers |
|
52 | + if($cipher->type() != Cipher::BLOCK) |
|
53 | + trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Destructor |
|
59 | + * |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public function __destruct() |
|
63 | + { |
|
64 | + parent::__destruct(); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
70 | + * to the constructor in ECB mode |
|
71 | + * |
|
72 | + * @param string $text The string to be encrypted in ECB mode |
|
73 | + * @return boolean Returns true |
|
74 | + */ |
|
75 | + public function encrypt(&$text) |
|
76 | + { |
|
77 | + $this->pad($text); |
|
78 | + $blocksz = $this->cipher->blockSize(); |
|
79 | + |
|
80 | + $max = strlen($text) / $blocksz; |
|
81 | + for($i = 0; $i < $max; ++$i) |
|
82 | + { |
|
83 | + // get the current position within $text |
|
84 | + $pos = $i * $blocksz; |
|
85 | + |
|
86 | + // grab a block of text |
|
87 | + $block = substr($text, $pos, $blocksz); |
|
88 | + |
|
89 | + // encrypt the block |
|
90 | + $this->cipher->encrypt($block); |
|
91 | + |
|
92 | + // replace the plain text with the cipher text |
|
93 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
94 | + } |
|
95 | + |
|
96 | + return true; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
102 | + * to the constructor in ECB mode |
|
103 | + * |
|
104 | + * @param string $text The string to be decrypted in ECB mode |
|
105 | + * @return boolean Returns true |
|
106 | + */ |
|
107 | + public function decrypt(&$text) |
|
108 | + { |
|
109 | + $blocksz = $this->cipher->blockSize(); |
|
110 | + |
|
111 | + $max = strlen($text) / $blocksz; |
|
112 | + for($i = 0; $i < $max; ++$i) |
|
113 | + { |
|
114 | + // get the current position within $text |
|
115 | + $pos = $i * $blocksz; |
|
116 | + |
|
117 | + // get a block of cipher text |
|
118 | + $block = substr($text, $pos, $blocksz); |
|
119 | + |
|
120 | + // decrypt the block |
|
121 | + $this->cipher->decrypt($block); |
|
122 | + |
|
123 | + // replace the block of cipher text with plain text |
|
124 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
125 | + } |
|
126 | + |
|
127 | + $this->strip($text); |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * This mode does not require an IV |
|
134 | + * |
|
135 | + * @return boolean Returns false |
|
136 | + */ |
|
137 | + public function requiresIV() |
|
138 | + { |
|
139 | + return false; |
|
140 | + } |
|
141 | 141 | } |
142 | 142 | ?> |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
50 | 50 | |
51 | 51 | // this works with only block Ciphers |
52 | - if($cipher->type() != Cipher::BLOCK) |
|
52 | + if ($cipher->type() != Cipher::BLOCK) |
|
53 | 53 | trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
54 | 54 | } |
55 | 55 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | $blocksz = $this->cipher->blockSize(); |
79 | 79 | |
80 | 80 | $max = strlen($text) / $blocksz; |
81 | - for($i = 0; $i < $max; ++$i) |
|
81 | + for ($i = 0; $i < $max; ++$i) |
|
82 | 82 | { |
83 | 83 | // get the current position within $text |
84 | 84 | $pos = $i * $blocksz; |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | $blocksz = $this->cipher->blockSize(); |
110 | 110 | |
111 | 111 | $max = strlen($text) / $blocksz; |
112 | - for($i = 0; $i < $max; ++$i) |
|
112 | + for ($i = 0; $i < $max; ++$i) |
|
113 | 113 | { |
114 | 114 | // get the current position within $text |
115 | 115 | $pos = $i * $blocksz; |
@@ -49,8 +49,9 @@ |
||
49 | 49 | parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
50 | 50 | |
51 | 51 | // this works with only block Ciphers |
52 | - if($cipher->type() != Cipher::BLOCK) |
|
53 | - trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
52 | + if($cipher->type() != Cipher::BLOCK) { |
|
53 | + trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
54 | + } |
|
54 | 55 | } |
55 | 56 | |
56 | 57 |