author | Dan |
Sun, 16 Mar 2008 21:08:37 -0400 | |
changeset 290 | 72ecb951b313 |
parent 287 | 3370d63928cf |
child 300 | 614f6ff1a855 |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
||
3 |
/** |
|
4 |
* Phijndael - an implementation of the AES encryption standard in PHP |
|
5 |
* Originally written by Fritz Schneider <fritz AT cd DOT ucsd DOT edu> |
|
6 |
* Ported to PHP by Dan Fuhry <dan AT enano DOT homelinux DOT org> |
|
7 |
* @package phijndael |
|
8 |
* @author Fritz Schneider |
|
9 |
* @author Dan Fuhry |
|
10 |
* @license BSD-style license |
|
11 |
*/ |
|
12 |
||
13 |
define ('ENC_HEX', 201); |
|
14 |
define ('ENC_BASE64', 202); |
|
15 |
define ('ENC_BINARY', 203); |
|
16 |
||
229
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
17 |
$_aes_objcache = array(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
18 |
|
1 | 19 |
class AESCrypt { |
20 |
||
21 |
var $debug = false; |
|
22 |
var $mcrypt = false; |
|
229
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
23 |
var $decrypt_cache = array(); |
1 | 24 |
|
25 |
// Rijndael parameters -- Valid values are 128, 192, or 256 |
|
26 |
||
27 |
var $keySizeInBits = 128; |
|
28 |
var $blockSizeInBits = 128; |
|
29 |
||
30 |
/////// You shouldn't have to modify anything below this line except for |
|
31 |
/////// the function getRandomBytes(). |
|
32 |
// |
|
33 |
// Note: in the following code the two dimensional arrays are indexed as |
|
34 |
// you would probably expect, as array[row][column]. The state arrays |
|
35 |
// are 2d arrays of the form state[4][Nb]. |
|
36 |
||
37 |
||
38 |
// The number of rounds for the cipher, indexed by [Nk][Nb] |
|
39 |
var $roundsArray = Array(0,0,0,0,Array(0,0,0,0,10,0, 12,0, 14),0, |
|
40 |
Array(0,0,0,0,12,0, 12,0, 14),0, |
|
41 |
Array(0,0,0,0,14,0, 14,0, 14) ); |
|
42 |
||
43 |
// The number of bytes to shift by in shiftRow, indexed by [Nb][row] |
|
44 |
var $shiftOffsets = Array(0,0,0,0,Array(0,1, 2, 3),0,Array(0,1, 2, 3),0,Array(0,1, 3, 4) ); |
|
45 |
||
46 |
// The round constants used in subkey expansion |
|
47 |
var $Rcon = Array( |
|
48 |
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
49 |
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
50 |
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, |
|
51 |
0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, |
|
52 |
0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 ); |
|
53 |
||
54 |
// Precomputed lookup table for the SBox |
|
55 |
var $SBox = Array( |
|
56 |
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, |
|
57 |
118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, |
|
58 |
114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, |
|
59 |
216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, |
|
60 |
235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, |
|
61 |
179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, |
|
62 |
190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, |
|
63 |
249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, |
|
64 |
188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, |
|
65 |
23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, |
|
66 |
144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, |
|
67 |
6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, |
|
68 |
141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, |
|
69 |
46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, |
|
70 |
181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, |
|
71 |
248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, |
|
72 |
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, |
|
73 |
22 ); |
|
74 |
||
75 |
// Precomputed lookup table for the inverse SBox |
|
76 |
var $SBoxInverse = Array( |
|
77 |
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, |
|
78 |
251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, |
|
79 |
233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, |
|
80 |
250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, |
|
81 |
109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, |
|
82 |
204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, |
|
83 |
70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247, |
|
84 |
228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2, |
|
85 |
193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220, |
|
86 |
234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173, |
|
87 |
53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29, |
|
88 |
41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75, |
|
89 |
198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168, |
|
90 |
51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, |
|
91 |
127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, |
|
92 |
224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, |
|
93 |
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, |
|
94 |
125 ); |
|
95 |
||
96 |
function AESCrypt($ks = 128, $bs = 128, $debug = false) |
|
97 |
{ |
|
98 |
$this->__construct($ks, $bs, $debug); |
|
99 |
} |
|
100 |
||
101 |
function __construct($ks = 128, $bs = 128, $debug = false) |
|
102 |
{ |
|
103 |
$this->keySizeInBits = $ks; |
|
104 |
$this->blockSizeInBits = $bs; |
|
105 |
||
106 |
// Use the Mcrypt library? This speeds things up dramatically. |
|
107 |
if(defined('MCRYPT_RIJNDAEL_' . $ks) && defined('MCRYPT_ACCEL')) |
|
108 |
{ |
|
109 |
eval('$mcb = MCRYPT_RIJNDAEL_' . $ks.';'); |
|
110 |
$bks = mcrypt_module_get_algo_block_size($mcb); |
|
111 |
$bks = $bks * 8; |
|
112 |
if ( $bks != $bs ) |
|
113 |
{ |
|
114 |
$mcb = false; |
|
115 |
echo (string)$bks; |
|
116 |
} |
|
117 |
} |
|
118 |
else |
|
119 |
{ |
|
120 |
$mcb = false; |
|
121 |
} |
|
122 |
||
123 |
$this->mcrypt = $mcb; |
|
124 |
||
125 |
// Cipher parameters ... do not change these |
|
126 |
$this->Nk = $this->keySizeInBits / 32; |
|
127 |
$this->Nb = $this->blockSizeInBits / 32; |
|
128 |
$this->Nr = $this->roundsArray[$this->Nk][$this->Nb]; |
|
129 |
$this->debug = $debug; |
|
130 |
} |
|
131 |
||
229
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
132 |
function singleton($key_size, $block_size) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
133 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
134 |
global $_aes_objcache; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
135 |
if ( isset($_aes_objcache["$key_size,$block_size"]) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
136 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
137 |
return $_aes_objcache["$key_size,$block_size"]; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
138 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
139 |
|
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
140 |
$_aes_objcache["$key_size,$block_size"] = new AESCrypt($key_size, $block_size); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
141 |
return $_aes_objcache["$key_size,$block_size"]; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
142 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
143 |
|
1 | 144 |
// Error handler |
145 |
||
146 |
function trigger_error($text, $level = E_USER_NOTICE) |
|
147 |
{ |
|
148 |
$bt = debug_backtrace(); |
|
149 |
$lastfunc =& $bt[1]; |
|
150 |
switch($level) |
|
151 |
{ |
|
152 |
case E_USER_NOTICE: |
|
153 |
default: |
|
154 |
$desc = 'Notice'; |
|
155 |
break; |
|
156 |
case E_USER_WARNING: |
|
157 |
$desc = 'Warning'; |
|
158 |
break; |
|
159 |
case E_USER_ERROR: |
|
160 |
$desc = 'Fatal'; |
|
161 |
break; |
|
162 |
} |
|
163 |
ob_start(); |
|
164 |
if($this->debug || $level == E_USER_ERROR) echo "AES encryption: <b>{$desc}:</b> $text in {$lastfunc['file']} on line {$lastfunc['line']} in function {$lastfunc['function']}<br />"; |
|
165 |
if($this->debug) |
|
166 |
{ |
|
167 |
//echo '<pre>'.enano_debug_print_backtrace(true).'</pre>'; |
|
168 |
} |
|
169 |
ob_end_flush(); |
|
170 |
if($level == E_USER_ERROR) |
|
171 |
{ |
|
172 |
echo '<p><b>This can sometimes happen if you are upgrading Enano to a new version and did not log out first.</b> <a href="'.$_SERVER['PHP_SELF'].'?do=diag&sub=cookie_destroy">Click here</a> to force cookies to clear and try again. You will be logged out.</p>'; |
|
173 |
exit; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
function array_slice_js_compat($array, $start, $finish = 0) |
|
178 |
{ |
|
179 |
$len = $finish - $start; |
|
180 |
if($len < 0) $len = 0 - $len; |
|
181 |
//if($this->debug) echo (string)$len . ' '; |
|
182 |
//if(count($array) < $start + $len) |
|
183 |
// $this->trigger_error('Index out of range', E_USER_WARNING); |
|
184 |
return array_slice($array, $start, $len); |
|
185 |
} |
|
186 |
||
187 |
function concat($s1, $s2) |
|
188 |
{ |
|
189 |
if(is_array($s1) && is_array($s2)) |
|
190 |
return array_merge($s1, $s2); |
|
191 |
elseif( ( is_array($s1) && !is_array($s2) ) || ( !is_array($s1) && is_array($s2) ) ) |
|
192 |
{ |
|
193 |
$this->trigger_error('incompatible types - you can\'t combine a non-array with an array', E_USER_WARNING); |
|
194 |
return false; |
|
195 |
} |
|
196 |
else |
|
197 |
return $s1 . $s2; |
|
198 |
} |
|
199 |
||
200 |
// This method circularly shifts the array left by the number of elements |
|
201 |
// given in its parameter. It returns the resulting array and is used for |
|
202 |
// the ShiftRow step. Note that shift() and push() could be used for a more |
|
203 |
// elegant solution, but they require IE5.5+, so I chose to do it manually. |
|
204 |
||
205 |
function cyclicShiftLeft($theArray, $positions) { |
|
206 |
if(!is_int($positions)) |
|
207 |
{ |
|
208 |
$this->trigger_error('$positions is not an integer! Backtrace:<br /><pre>'.print_r(debug_backtrace(), true).'</pre>', E_USER_WARNING); |
|
209 |
return false; |
|
210 |
} |
|
211 |
$second = array_slice($theArray, 0, $positions); |
|
212 |
$first = array_slice($theArray, $positions); |
|
213 |
$theArray = array_merge($first, $second); |
|
214 |
return $theArray; |
|
215 |
} |
|
216 |
||
217 |
// Multiplies the element "poly" of GF(2^8) by x. See the Rijndael spec. |
|
218 |
||
219 |
function xtime($poly) { |
|
220 |
$poly <<= 1; |
|
221 |
return (($poly & 0x100) ? ($poly ^ 0x11B) : ($poly)); |
|
222 |
} |
|
223 |
||
224 |
// Multiplies the two elements of GF(2^8) together and returns the result. |
|
225 |
// See the Rijndael spec, but should be straightforward: for each power of |
|
226 |
// the indeterminant that has a 1 coefficient in x, add y times that power |
|
227 |
// to the result. x and y should be bytes representing elements of GF(2^8) |
|
228 |
||
229 |
function mult_GF256($x, $y) { |
|
230 |
$result = 0; |
|
231 |
||
232 |
for ($bit = 1; $bit < 256; $bit *= 2, $y = $this->xtime($y)) { |
|
233 |
if ($x & $bit) |
|
234 |
$result ^= $y; |
|
235 |
} |
|
236 |
return $result; |
|
237 |
} |
|
238 |
||
239 |
// Performs the substitution step of the cipher. State is the 2d array of |
|
240 |
// state information (see spec) and direction is string indicating whether |
|
241 |
// we are performing the forward substitution ("encrypt") or inverse |
|
242 |
// substitution (anything else) |
|
243 |
||
244 |
function byteSub(&$state, $direction) { |
|
245 |
//global $this->SBox, $this->SBoxInverse, $this->Nb; |
|
246 |
if ($direction == "encrypt") // Point S to the SBox we're using |
|
247 |
$S =& $this->SBox; |
|
248 |
else |
|
249 |
$S =& $this->SBoxInverse; |
|
250 |
for ($i = 0; $i < 4; $i++) // Substitute for every byte in state |
|
251 |
for ($j = 0; $j < $this->Nb; $j++) |
|
252 |
$state[$i][$j] = $S[$state[$i][$j]]; |
|
253 |
} |
|
254 |
||
255 |
// Performs the row shifting step of the cipher. |
|
256 |
||
257 |
function shiftRow(&$state, $direction) { |
|
258 |
//global $this->Nb, $this->shiftOffsets; |
|
259 |
for ($i=1; $i<4; $i++) // Row 0 never shifts |
|
260 |
if ($direction == "encrypt") |
|
261 |
$state[$i] = $this->cyclicShiftLeft($state[$i], $this->shiftOffsets[$this->Nb][$i]); |
|
262 |
else |
|
263 |
$state[$i] = $this->cyclicShiftLeft($state[$i], $this->Nb - $this->shiftOffsets[$this->Nb][$i]); |
|
264 |
||
265 |
} |
|
266 |
||
267 |
// Performs the column mixing step of the cipher. Most of these steps can |
|
268 |
// be combined into table lookups on 32bit values (at least for encryption) |
|
269 |
// to greatly increase the speed. |
|
270 |
||
271 |
function mixColumn(&$state, $direction) { |
|
272 |
//global $this->Nb; |
|
273 |
$b = Array(); // Result of matrix multiplications |
|
274 |
for ($j = 0; $j < $this->Nb; $j++) { // Go through each column... |
|
275 |
for ($i = 0; $i < 4; $i++) { // and for each row in the column... |
|
276 |
if ($direction == "encrypt") |
|
277 |
$b[$i] = $this->mult_GF256($state[$i][$j], 2) ^ // perform mixing |
|
278 |
$this->mult_GF256($state[($i+1)%4][$j], 3) ^ |
|
279 |
$state[($i+2)%4][$j] ^ |
|
280 |
$state[($i+3)%4][$j]; |
|
281 |
else |
|
282 |
$b[$i] = $this->mult_GF256($state[$i][$j], 0xE) ^ |
|
283 |
$this->mult_GF256($state[($i+1)%4][$j], 0xB) ^ |
|
284 |
$this->mult_GF256($state[($i+2)%4][$j], 0xD) ^ |
|
285 |
$this->mult_GF256($state[($i+3)%4][$j], 9); |
|
286 |
} |
|
287 |
for ($i = 0; $i < 4; $i++) // Place result back into column |
|
288 |
$state[$i][$j] = $b[$i]; |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
// Adds the current round key to the state information. Straightforward. |
|
293 |
||
294 |
function addRoundKey(&$state, $roundKey) { |
|
295 |
//global $this->Nb; |
|
296 |
for ($j = 0; $j < $this->Nb; $j++) { // Step through columns... |
|
297 |
$state[0][$j] ^= ( $roundKey[$j] & 0xFF); // and XOR |
|
298 |
$state[1][$j] ^= (($roundKey[$j]>>8) & 0xFF); |
|
299 |
$state[2][$j] ^= (($roundKey[$j]>>16) & 0xFF); |
|
300 |
$state[3][$j] ^= (($roundKey[$j]>>24) & 0xFF); |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
// This function creates the expanded key from the input (128/192/256-bit) |
|
305 |
// key. The parameter key is an array of bytes holding the value of the key. |
|
306 |
// The returned value is an array whose elements are the 32-bit words that |
|
307 |
// make up the expanded key. |
|
308 |
||
309 |
function keyExpansion($key) { |
|
310 |
//global $this->keySizeInBits, $this->blockSizeInBits, $this->roundsArray, $this->Nk, $this->Nb, $this->Nr, $this->Nk, $this->SBox, $this->Rcon; |
|
311 |
$expandedKey = Array(); |
|
312 |
||
313 |
// in case the key size or parameters were changed... |
|
314 |
$this->Nk = $this->keySizeInBits / 32; |
|
315 |
$this->Nb = $this->blockSizeInBits / 32; |
|
316 |
$this->Nr = $this->roundsArray[$this->Nk][$this->Nb]; |
|
317 |
||
318 |
for ($j=0; $j < $this->Nk; $j++) // Fill in input key first |
|
319 |
$expandedKey[$j] = |
|
320 |
($key[4*$j]) | ($key[4*$j+1]<<8) | ($key[4*$j+2]<<16) | ($key[4*$j+3]<<24); |
|
321 |
||
322 |
// Now walk down the rest of the array filling in expanded key bytes as |
|
323 |
// per Rijndael's spec |
|
324 |
for ($j = $this->Nk; $j < $this->Nb * ($this->Nr + 1); $j++) { // For each word of expanded key |
|
325 |
$temp = $expandedKey[$j - 1]; |
|
326 |
if ($j % $this->Nk == 0) |
|
327 |
$temp = ( ($this->SBox[($temp>>8) & 0xFF]) | |
|
328 |
($this->SBox[($temp>>16) & 0xFF]<<8) | |
|
329 |
($this->SBox[($temp>>24) & 0xFF]<<16) | |
|
330 |
($this->SBox[$temp & 0xFF]<<24) ) ^ $this->Rcon[floor($j / $this->Nk) - 1]; |
|
331 |
elseif ($this->Nk > 6 && $j % $this->Nk == 4) |
|
332 |
$temp = ($this->SBox[($temp>>24) & 0xFF]<<24) | |
|
333 |
($this->SBox[($temp>>16) & 0xFF]<<16) | |
|
334 |
($this->SBox[($temp>>8) & 0xFF]<<8) | |
|
335 |
($this->SBox[ $temp & 0xFF]); |
|
336 |
$expandedKey[$j] = $expandedKey[$j-$this->Nk] ^ $temp; |
|
337 |
} |
|
338 |
return $expandedKey; |
|
339 |
} |
|
340 |
||
341 |
// Rijndael's round functions... |
|
342 |
||
343 |
function RijndaelRound(&$state, $roundKey) { |
|
344 |
$this->byteSub($state, "encrypt"); |
|
345 |
$this->shiftRow($state, "encrypt"); |
|
346 |
$this->mixColumn($state, "encrypt"); |
|
347 |
$this->addRoundKey($state, $roundKey); |
|
348 |
} |
|
349 |
||
350 |
function InverseRijndaelRound(&$state, $roundKey) { |
|
351 |
$this->addRoundKey($state, $roundKey); |
|
352 |
$this->mixColumn($state, "decrypt"); |
|
353 |
$this->shiftRow($state, "decrypt"); |
|
354 |
$this->byteSub($state, "decrypt"); |
|
355 |
} |
|
356 |
||
357 |
function FinalRijndaelRound(&$state, $roundKey) { |
|
358 |
$this->byteSub($state, "encrypt"); |
|
359 |
$this->shiftRow($state, "encrypt"); |
|
360 |
$this->addRoundKey($state, $roundKey); |
|
361 |
} |
|
362 |
||
363 |
function InverseFinalRijndaelRound(&$state, $roundKey){ |
|
364 |
$this->addRoundKey($state, $roundKey); |
|
365 |
$this->shiftRow($state, "decrypt"); |
|
366 |
$this->byteSub($state, "decrypt"); |
|
367 |
} |
|
368 |
||
369 |
// encrypt is the basic encryption function. It takes parameters |
|
370 |
// block, an array of bytes representing a plaintext block, and expandedKey, |
|
371 |
// an array of words representing the expanded key previously returned by |
|
372 |
// keyExpansion(). The ciphertext block is returned as an array of bytes. |
|
373 |
||
374 |
function cryptBlock($block, $expandedKey) { |
|
375 |
//global $this->blockSizeInBits, $this->Nb, $this->Nr; |
|
376 |
$t=count($block)*8; |
|
377 |
if (!is_array($block) || count($block)*8 != $this->blockSizeInBits) |
|
378 |
{ |
|
379 |
$this->trigger_error('block is bad or block size is wrong<pre>'.print_r($block, true).'</pre><p>Aiming for size '.$this->blockSizeInBits.', got '.$t.'.', E_USER_WARNING); |
|
380 |
return false; |
|
381 |
} |
|
382 |
if (!$expandedKey) |
|
383 |
return; |
|
384 |
||
385 |
$block = $this->packBytes($block); |
|
386 |
$this->addRoundKey($block, $expandedKey); |
|
387 |
for ($i=1; $i<$this->Nr; $i++) |
|
388 |
$this->RijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1))); |
|
389 |
$this->FinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr)); |
|
390 |
$ret = $this->unpackBytes($block); |
|
391 |
return $ret; |
|
392 |
} |
|
393 |
||
394 |
// decrypt is the basic decryption function. It takes parameters |
|
395 |
// block, an array of bytes representing a ciphertext block, and expandedKey, |
|
396 |
// an array of words representing the expanded key previously returned by |
|
397 |
// keyExpansion(). The decrypted block is returned as an array of bytes. |
|
398 |
||
399 |
function unCryptBlock($block, $expandedKey) { |
|
400 |
$t = count($block)*8; |
|
401 |
if (!is_array($block) || count($block)*8 != $this->blockSizeInBits) |
|
402 |
{ |
|
403 |
$this->trigger_error('$block is not a valid rijndael-block array: '.$this->byteArrayToHex($block).'<pre>'.print_r($block, true).'</pre><p>Block size is '.$t.', should be '.$this->blockSizeInBits.'</p>', E_USER_WARNING); |
|
404 |
return false; |
|
405 |
} |
|
406 |
if (!$expandedKey) |
|
407 |
{ |
|
408 |
$this->trigger_error('$expandedKey is invalid', E_USER_WARNING); |
|
409 |
return false; |
|
410 |
} |
|
411 |
||
412 |
$block = $this->packBytes($block); |
|
413 |
$this->InverseFinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr)); |
|
414 |
for ($i = $this->Nr - 1; $i>0; $i--) |
|
415 |
{ |
|
416 |
$this->InverseRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1))); |
|
417 |
} |
|
418 |
$this->addRoundKey($block, $expandedKey); |
|
419 |
$ret = $this->unpackBytes($block); |
|
420 |
if(!is_array($ret)) |
|
421 |
{ |
|
422 |
$this->trigger_error('$ret is not an array', E_USER_WARNING); |
|
423 |
} |
|
424 |
return $ret; |
|
425 |
} |
|
426 |
||
427 |
// This method takes a byte array (byteArray) and converts it to a string by |
|
428 |
// applying String.fromCharCode() to each value and concatenating the result. |
|
429 |
// The resulting string is returned. Note that this function SKIPS zero bytes |
|
430 |
// under the assumption that they are padding added in formatPlaintext(). |
|
431 |
// Obviously, do not invoke this method on raw data that can contain zero |
|
432 |
// bytes. It is really only appropriate for printable ASCII/Latin-1 |
|
433 |
// values. Roll your own function for more robust functionality :) |
|
434 |
||
435 |
function byteArrayToString($byteArray) { |
|
436 |
$result = ""; |
|
437 |
for($i=0; $i<count($byteArray); $i++) |
|
438 |
if ($byteArray[$i] != 0) |
|
439 |
$result .= chr($byteArray[$i]); |
|
440 |
return $result; |
|
441 |
} |
|
442 |
||
443 |
// This function takes an array of bytes (byteArray) and converts them |
|
444 |
// to a hexadecimal string. Array element 0 is found at the beginning of |
|
445 |
// the resulting string, high nibble first. Consecutive elements follow |
|
446 |
// similarly, for example [16, 255] --> "10ff". The function returns a |
|
447 |
// string. |
|
448 |
||
449 |
/* |
|
450 |
function byteArrayToHex($byteArray) { |
|
451 |
$result = ""; |
|
452 |
if (!$byteArray) |
|
453 |
return; |
|
454 |
for ($i=0; $i<count($byteArray); $i++) |
|
455 |
$result .= (($byteArray[$i]<16) ? "0" : "") + toString($byteArray[$i]); // magic number here is 16, not sure how to handle this... |
|
456 |
||
457 |
return $result; |
|
458 |
} |
|
459 |
*/ |
|
460 |
function byteArrayToHex($arr) |
|
461 |
{ |
|
462 |
$ret = ''; |
|
463 |
foreach($arr as $a) |
|
464 |
{ |
|
465 |
$nibble = (string)dechex(intval($a)); |
|
466 |
if(strlen($nibble) == 1) $nibble = '0' . $nibble; |
|
467 |
$ret .= $nibble; |
|
468 |
} |
|
469 |
return $ret; |
|
470 |
} |
|
471 |
||
472 |
// PHP equivalent of Javascript's toString() |
|
473 |
function toString($bool) |
|
474 |
{ |
|
475 |
if(is_bool($bool)) |
|
476 |
return ($bool) ? 'true' : 'false'; |
|
477 |
elseif(is_array($bool)) |
|
478 |
return implode(',', $bool); |
|
479 |
else |
|
480 |
return (string)$bool; |
|
481 |
} |
|
482 |
||
483 |
// This function converts a string containing hexadecimal digits to an |
|
484 |
// array of bytes. The resulting byte array is filled in the order the |
|
485 |
// values occur in the string, for example "10FF" --> [16, 255]. This |
|
486 |
// function returns an array. |
|
487 |
||
488 |
/* |
|
489 |
function hexToByteArray($hexString) { |
|
490 |
$byteArray = Array(); |
|
491 |
if (strlen($hexString) % 2) // must have even length |
|
492 |
return; |
|
493 |
if (strstr($hexString, "0x") == $hexString || strstr($hexString, "0X") == $hexString) |
|
494 |
$hexString = substr($hexString, 2); |
|
495 |
for ($i = 0; $i<strlen($hexString); $i++,$i++) |
|
496 |
$byteArray[floor($i/2)] = intval(substr($hexString, $i, 2)); // again, that strange magic number: 16 |
|
497 |
return $byteArray; |
|
498 |
} |
|
499 |
*/ |
|
500 |
function hexToByteArray($str) |
|
501 |
{ |
|
502 |
if(substr($str, 0, 2) == '0x' || substr($str, 0, 2) == '0X') |
|
503 |
$str = substr($str, 2); |
|
504 |
$arr = Array(); |
|
505 |
$str = $this->enano_str_split($str, 2); |
|
506 |
foreach($str as $s) |
|
507 |
{ |
|
508 |
$arr[] = intval(hexdec($s)); |
|
509 |
} |
|
510 |
return $arr; |
|
511 |
} |
|
512 |
||
513 |
// This function packs an array of bytes into the four row form defined by |
|
514 |
// Rijndael. It assumes the length of the array of bytes is divisible by |
|
515 |
// four. Bytes are filled in according to the Rijndael spec (starting with |
|
516 |
// column 0, row 0 to 3). This function returns a 2d array. |
|
517 |
||
518 |
function packBytes($octets) { |
|
519 |
$state = Array(); |
|
520 |
if (!$octets || count($octets) % 4) |
|
521 |
return; |
|
522 |
||
523 |
$state[0] = Array(); $state[1] = Array(); |
|
524 |
$state[2] = Array(); $state[3] = Array(); |
|
525 |
for ($j=0; $j<count($octets); $j = $j+4) { |
|
526 |
$state[0][$j/4] = $octets[$j]; |
|
527 |
$state[1][$j/4] = $octets[$j+1]; |
|
528 |
$state[2][$j/4] = $octets[$j+2]; |
|
529 |
$state[3][$j/4] = $octets[$j+3]; |
|
530 |
} |
|
531 |
return $state; |
|
532 |
} |
|
533 |
||
534 |
// This function unpacks an array of bytes from the four row format preferred |
|
535 |
// by Rijndael into a single 1d array of bytes. It assumes the input "packed" |
|
536 |
// is a packed array. Bytes are filled in according to the Rijndael spec. |
|
537 |
// This function returns a 1d array of bytes. |
|
538 |
||
539 |
function unpackBytes($packed) { |
|
540 |
$result = Array(); |
|
541 |
for ($j=0; $j<count($packed[0]); $j++) { |
|
542 |
$result[] = $packed[0][$j]; |
|
543 |
$result[] = $packed[1][$j]; |
|
544 |
$result[] = $packed[2][$j]; |
|
545 |
$result[] = $packed[3][$j]; |
|
546 |
} |
|
547 |
return $result; |
|
548 |
} |
|
549 |
||
550 |
function charCodeAt($str, $i) |
|
551 |
{ |
|
552 |
return ord(substr($str, $i, 1)); |
|
553 |
} |
|
554 |
||
555 |
function fromCharCode($str) |
|
556 |
{ |
|
557 |
return chr($str); |
|
558 |
} |
|
559 |
||
560 |
// This function takes a prospective plaintext (string or array of bytes) |
|
561 |
// and pads it with zero bytes if its length is not a multiple of the block |
|
562 |
// size. If plaintext is a string, it is converted to an array of bytes |
|
563 |
// in the process. The type checking can be made much nicer using the |
|
564 |
// instanceof operator, but this operator is not available until IE5.0 so I |
|
565 |
// chose to use the heuristic below. |
|
566 |
||
567 |
function formatPlaintext($plaintext) { |
|
568 |
//global $this->blockSizeInBits; |
|
569 |
$bpb = $this->blockSizeInBits / 8; // bytes per block |
|
570 |
||
571 |
// if primitive string or String instance |
|
572 |
if (is_string($plaintext)) { |
|
573 |
$plaintext = $this->enano_str_split($plaintext); |
|
574 |
// Unicode issues here (ignoring high byte) |
|
575 |
for ($i=0; $i<sizeof($plaintext); $i++) |
|
576 |
$plaintext[$i] = $this->charCodeAt($plaintext[$i], 0) & 0xFF; |
|
577 |
} |
|
578 |
||
579 |
for ($i = $bpb - (sizeof($plaintext) % $bpb); $i > 0 && $i < $bpb; $i--) |
|
580 |
$plaintext[] = 0; |
|
581 |
||
582 |
return $plaintext; |
|
583 |
} |
|
584 |
||
585 |
// Returns an array containing "howMany" random bytes. YOU SHOULD CHANGE THIS |
|
586 |
// TO RETURN HIGHER QUALITY RANDOM BYTES IF YOU ARE USING THIS FOR A "REAL" |
|
587 |
// APPLICATION. (edit: done, mt_rand() is relatively secure) |
|
588 |
||
589 |
function getRandomBytes($howMany) { |
|
590 |
$bytes = Array(); |
|
591 |
for ($i=0; $i<$howMany; $i++) |
|
592 |
$bytes[$i] = mt_rand(0, 255); |
|
593 |
return $bytes; |
|
594 |
} |
|
595 |
||
596 |
// rijndaelEncrypt(plaintext, key, mode) |
|
597 |
// Encrypts the plaintext using the given key and in the given mode. |
|
598 |
// The parameter "plaintext" can either be a string or an array of bytes. |
|
599 |
// The parameter "key" must be an array of key bytes. If you have a hex |
|
600 |
// string representing the key, invoke hexToByteArray() on it to convert it |
|
601 |
// to an array of bytes. The third parameter "mode" is a string indicating |
|
602 |
// the encryption mode to use, either "ECB" or "CBC". If the parameter is |
|
603 |
// omitted, ECB is assumed. |
|
604 |
// |
|
605 |
// An array of bytes representing the cihpertext is returned. To convert |
|
606 |
// this array to hex, invoke byteArrayToHex() on it. If you are using this |
|
607 |
// "for real" it is a good idea to change the function getRandomBytes() to |
|
608 |
// something that returns truly random bits. |
|
609 |
||
610 |
function rijndaelEncrypt($plaintext, $key, $mode = 'ECB') { |
|
611 |
//global $this->blockSizeInBits, $this->keySizeInBits; |
|
612 |
$bpb = $this->blockSizeInBits / 8; // bytes per block |
|
613 |
// var ct; // ciphertext |
|
614 |
||
615 |
if($mode == 'CBC') |
|
616 |
{ |
|
617 |
if (!is_string($plaintext) || !is_array($key)) |
|
618 |
{ |
|
619 |
$this->trigger_error('In CBC mode the first and second parameters should be strings', E_USER_WARNING); |
|
620 |
return false; |
|
621 |
} |
|
622 |
} else { |
|
623 |
if (!is_array($plaintext) || !is_array($key)) |
|
624 |
{ |
|
625 |
$this->trigger_error('In ECB mode the first and second parameters should be byte arrays', E_USER_WARNING); |
|
626 |
return false; |
|
627 |
} |
|
628 |
} |
|
629 |
if (sizeof($key)*8 != $this->keySizeInBits) |
|
630 |
{ |
|
631 |
$this->trigger_error('The key needs to be '. ( $this->keySizeInBits / 8 ) .' bytes in length', E_USER_WARNING); |
|
632 |
return false; |
|
633 |
} |
|
634 |
if ($mode == "CBC") |
|
635 |
$ct = $this->getRandomBytes($bpb); // get IV |
|
636 |
else { |
|
637 |
$mode = "ECB"; |
|
638 |
$ct = Array(); |
|
639 |
} |
|
640 |
||
641 |
// convert plaintext to byte array and pad with zeros if necessary. |
|
642 |
$plaintext = $this->formatPlaintext($plaintext); |
|
643 |
||
644 |
$expandedKey = $this->keyExpansion($key); |
|
645 |
||
646 |
for ($block=0; $block<sizeof($plaintext) / $bpb; $block++) { |
|
647 |
$aBlock = $this->array_slice_js_compat($plaintext, $block*$bpb, ($block+1)*$bpb); |
|
648 |
if ($mode == "CBC") |
|
649 |
{ |
|
650 |
for ($i=0; $i<$bpb; $i++) |
|
651 |
{ |
|
652 |
$aBlock[$i] ^= $ct[$block*$bpb + $i]; |
|
653 |
} |
|
654 |
} |
|
655 |
$cp = $this->cryptBlock($aBlock, $expandedKey); |
|
656 |
$ct = $this->concat($ct, $cp); |
|
657 |
} |
|
658 |
||
659 |
return $ct; |
|
660 |
} |
|
661 |
||
662 |
// rijndaelDecrypt(ciphertext, key, mode) |
|
663 |
// Decrypts the using the given key and mode. The parameter "ciphertext" |
|
664 |
// must be an array of bytes. The parameter "key" must be an array of key |
|
665 |
// bytes. If you have a hex string representing the ciphertext or key, |
|
666 |
// invoke hexToByteArray() on it to convert it to an array of bytes. The |
|
667 |
// parameter "mode" is a string, either "CBC" or "ECB". |
|
668 |
// |
|
669 |
// An array of bytes representing the plaintext is returned. To convert |
|
670 |
// this array to a hex string, invoke byteArrayToHex() on it. To convert it |
|
671 |
// to a string of characters, you can use byteArrayToString(). |
|
672 |
||
673 |
function rijndaelDecrypt($ciphertext, $key, $mode = 'ECB') { |
|
674 |
//global $this->blockSizeInBits, $this->keySizeInBits; |
|
675 |
$bpb = $this->blockSizeInBits / 8; // bytes per block |
|
676 |
$pt = Array(); // plaintext array |
|
677 |
// $aBlock; // a decrypted block |
|
678 |
// $block; // current block number |
|
679 |
||
680 |
if (!$ciphertext) |
|
681 |
{ |
|
682 |
$this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING); |
|
683 |
return false; |
|
684 |
} |
|
685 |
if( !is_array($key) ) |
|
686 |
{ |
|
687 |
$this->trigger_error('$key should be a byte array', E_USER_WARNING); |
|
688 |
return false; |
|
689 |
} |
|
690 |
if( is_string($ciphertext) ) |
|
691 |
{ |
|
692 |
$this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING); |
|
693 |
return false; |
|
694 |
} |
|
695 |
if (sizeof($key)*8 != $this->keySizeInBits) |
|
696 |
{ |
|
697 |
$this->trigger_error('Encryption key is the wrong length', E_USER_WARNING); |
|
698 |
return false; |
|
699 |
} |
|
700 |
if (!$mode) |
|
701 |
$mode = "ECB"; // assume ECB if mode omitted |
|
702 |
||
703 |
$expandedKey = $this->keyExpansion($key); |
|
704 |
||
705 |
// work backwards to accomodate CBC mode |
|
706 |
for ($block=(sizeof($ciphertext) / $bpb)-1; $block>0; $block--) |
|
707 |
{ |
|
708 |
if( ( $block*$bpb ) + ( ($block+1)*$bpb ) > count($ciphertext) ) |
|
709 |
{ |
|
710 |
//$this->trigger_error('$ciphertext index out of bounds', E_USER_ERROR); |
|
711 |
} |
|
712 |
$current_block = $this->array_slice_js_compat($ciphertext, $block*$bpb, ($block+1)*$bpb); |
|
713 |
if(count($current_block) * 8 != $this->blockSizeInBits) |
|
714 |
{ |
|
715 |
// $c=count($current_block)*8; |
|
716 |
// $this->trigger_error('We got a '.$c.'-bit block, instead of '.$this->blockSizeInBits.'', E_USER_ERROR); |
|
717 |
} |
|
718 |
$aBlock = $this->uncryptBlock($current_block, $expandedKey); |
|
719 |
if(!$aBlock) |
|
720 |
{ |
|
721 |
$this->trigger_error('Shared block decryption routine returned false', E_USER_WARNING); |
|
722 |
return false; |
|
723 |
} |
|
724 |
if ($mode == "CBC") |
|
725 |
for ($i=0; $i<$bpb; $i++) |
|
726 |
$pt[($block-1)*$bpb + $i] = $aBlock[$i] ^ $ciphertext[($block-1)*$bpb + $i]; |
|
727 |
else |
|
728 |
$pt = $this->concat($aBlock, $pt); |
|
729 |
} |
|
730 |
||
731 |
// do last block if ECB (skips the IV in CBC) |
|
732 |
if ($mode == "ECB") |
|
733 |
{ |
|
734 |
$x = $this->uncryptBlock($this->array_slice_js_compat($ciphertext, 0, $bpb), $expandedKey); |
|
735 |
if(!$x) |
|
736 |
{ |
|
737 |
$this->trigger_error('ECB block decryption routine returned false', E_USER_WARNING); |
|
738 |
return false; |
|
739 |
} |
|
740 |
$pt = $this->concat($x, $pt); |
|
741 |
if(!$pt) |
|
742 |
{ |
|
743 |
$this->trigger_error('ECB concatenation routine returned false', E_USER_WARNING); |
|
744 |
return false; |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
return $pt; |
|
749 |
} |
|
750 |
||
751 |
/** |
|
752 |
* Wrapper for encryption. |
|
753 |
* @param string $text the text to encrypt |
|
754 |
* @param string $key the raw binary key to encrypt with |
|
755 |
* @param int $return_encoding optional - can be ENC_BINARY, ENC_HEX or ENC_BASE64 |
|
756 |
*/ |
|
757 |
||
758 |
function encrypt($text, $key, $return_encoding = ENC_HEX) |
|
759 |
{ |
|
54
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
760 |
if ( $text == '' ) |
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
761 |
return ''; |
1 | 762 |
if ( $this->mcrypt && $this->blockSizeInBits == mcrypt_module_get_algo_block_size(eval('return MCRYPT_RIJNDAEL_'.$this->keySizeInBits.';')) ) |
763 |
{ |
|
764 |
$iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB); |
|
765 |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
766 |
$cryptext = mcrypt_encrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv); |
|
767 |
switch($return_encoding) |
|
768 |
{ |
|
769 |
case ENC_HEX: |
|
770 |
default: |
|
771 |
$cryptext = $this->strtohex($cryptext); |
|
772 |
break; |
|
773 |
case ENC_BINARY: |
|
774 |
$cryptext = $cryptext; |
|
775 |
break; |
|
776 |
case ENC_BASE64: |
|
777 |
$cryptext = base64_encode($cryptext); |
|
778 |
break; |
|
779 |
} |
|
780 |
} |
|
781 |
else |
|
782 |
{ |
|
783 |
$key = $this->prepare_string($key); |
|
784 |
$text = $this->prepare_string($text); |
|
785 |
$cryptext = $this->rijndaelEncrypt($text, $key, 'ECB'); |
|
786 |
if(!is_array($cryptext)) |
|
787 |
{ |
|
42
45ebe475ff75
I dunno how many times I'm gonna have to fix the "problem seems to be the hex conversion" bug, but this is at least the fourth try.
Dan
parents:
40
diff
changeset
|
788 |
echo 'Warning: encryption failed for string: '.print_r($text,true).'<br />'; |
1 | 789 |
return false; |
790 |
} |
|
791 |
switch($return_encoding) |
|
792 |
{ |
|
793 |
case ENC_HEX: |
|
794 |
default: |
|
795 |
$cryptext = $this->byteArrayToHex($cryptext); |
|
796 |
break; |
|
797 |
case ENC_BINARY: |
|
798 |
$cryptext = $this->byteArrayToString($cryptext); |
|
799 |
break; |
|
800 |
case ENC_BASE64: |
|
801 |
$cryptext = base64_encode($this->byteArrayToString($cryptext)); |
|
802 |
break; |
|
803 |
} |
|
804 |
} |
|
805 |
return $cryptext; |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* Wrapper for decryption. |
|
810 |
* @param string $text the encrypted text |
|
811 |
* @param string $key the raw binary key used to encrypt the text |
|
812 |
* @param int $input_encoding the encoding used for the encrypted string. Can be ENC_BINARY, ENC_HEX, or ENC_BASE64. |
|
813 |
* @return string |
|
814 |
*/ |
|
815 |
||
816 |
function decrypt($text, $key, $input_encoding = ENC_HEX) |
|
817 |
{ |
|
54
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
818 |
if ( $text == '' ) |
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
819 |
return ''; |
229
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
820 |
$text_orig = $text; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
821 |
if ( isset($this->decrypt_cache[$key]) && is_array($this->decrypt_cache[$key]) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
822 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
823 |
if ( isset($this->decrypt_cache[$key][$text]) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
824 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
825 |
return $this->decrypt_cache[$key][$text]; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
826 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
827 |
} |
1 | 828 |
switch($input_encoding) |
829 |
{ |
|
830 |
case ENC_BINARY: |
|
831 |
default: |
|
832 |
break; |
|
833 |
case ENC_HEX: |
|
834 |
$text = $this->hextostring($text); |
|
835 |
break; |
|
836 |
case ENC_BASE64: |
|
837 |
$text = base64_decode($text); |
|
838 |
break; |
|
839 |
} |
|
840 |
//$mod = strlen($text) % $this->blockSizeInBits; |
|
841 |
//if($mod != 96) |
|
842 |
//die('modulus check failed: '.$mod); |
|
843 |
if ( $this->mcrypt ) |
|
844 |
{ |
|
845 |
$iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB); |
|
846 |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|
847 |
$dypt = mcrypt_decrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv); |
|
848 |
} |
|
849 |
else |
|
850 |
{ |
|
851 |
$etext = $this->prepare_string($text); |
|
852 |
$ekey = $this->prepare_string($key); |
|
853 |
$mod = count($etext) % $this->blockSizeInBits; |
|
854 |
$dypt = $this->rijndaelDecrypt($etext, $ekey, 'ECB'); |
|
855 |
if(!$dypt) |
|
856 |
{ |
|
857 |
echo '<pre>'.print_r($dypt, true).'</pre>'; |
|
858 |
$this->trigger_error('Rijndael main decryption routine failed', E_USER_ERROR); |
|
859 |
} |
|
860 |
$dypt = $this->byteArrayToString($dypt); |
|
861 |
} |
|
229
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
862 |
if ( !isset($this->decrypt_cache[$key]) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
863 |
$this->decrypt_cache[$key] = array(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
864 |
|
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
865 |
$this->decrypt_cache[$key][$text_orig] = $dypt; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
54
diff
changeset
|
866 |
|
1 | 867 |
return $dypt; |
868 |
} |
|
869 |
||
870 |
/** |
|
871 |
* Enano-ese equivalent of str_split() which is only found in PHP5 |
|
872 |
* @param $text string the text to split |
|
873 |
* @param $inc int size of each block |
|
874 |
* @return array |
|
875 |
*/ |
|
876 |
||
877 |
function enano_str_split($text, $inc = 1) |
|
878 |
{ |
|
879 |
if($inc < 1) return false; |
|
880 |
if($inc >= strlen($text)) return Array($text); |
|
881 |
$len = ceil(strlen($text) / $inc); |
|
882 |
$ret = Array(); |
|
883 |
for($i=0;$i<strlen($text);$i=$i+$inc) |
|
884 |
{ |
|
885 |
$ret[] = substr($text, $i, $inc); |
|
886 |
} |
|
887 |
return $ret; |
|
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Generates a random key suitable for encryption |
|
892 |
* @param int $len the length of the key, in bytes |
|
893 |
* @return string a BINARY key |
|
894 |
*/ |
|
895 |
||
896 |
function randkey($len = 32) |
|
897 |
{ |
|
898 |
$key = ''; |
|
899 |
for($i=0;$i<$len;$i++) |
|
900 |
{ |
|
901 |
$key .= chr(mt_rand(0, 255)); |
|
902 |
} |
|
287
3370d63928cf
Fixed PHP warning in Rijndael RNG code when open_basedir restriction in effect
Dan
parents:
267
diff
changeset
|
903 |
if ( @file_exists('/dev/random') && @is_readable('/dev/random') ) |
44
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
904 |
{ |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
905 |
// Let's use something a little more secure |
287
3370d63928cf
Fixed PHP warning in Rijndael RNG code when open_basedir restriction in effect
Dan
parents:
267
diff
changeset
|
906 |
$ur = @fopen('/dev/random', 'r'); |
44
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
907 |
if ( !$ur ) |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
908 |
return $key; |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
909 |
$ukey = @fread($ur, $len); |
45 | 910 |
fclose($ur); |
44
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
911 |
if ( strlen($ukey) != $len ) |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
912 |
return $key; |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
913 |
return $ukey; |
90b557e7f12c
$aes->gen_readymade_key() now uses /dev/urandom when possible
Dan
parents:
42
diff
changeset
|
914 |
} |
1 | 915 |
return $key; |
916 |
} |
|
917 |
||
918 |
/* |
|
919 |
function byteArrayToString($arr) |
|
920 |
{ |
|
921 |
if(!is_array($arr)) |
|
922 |
{ |
|
923 |
$this->trigger_error('First parameter should be an array', E_USER_WARNING); |
|
924 |
return false; |
|
925 |
} |
|
926 |
$ret = ''; |
|
927 |
foreach($arr as $a) |
|
928 |
{ |
|
929 |
if($a != 0) $ret .= chr($a); |
|
930 |
} |
|
931 |
return $ret; |
|
932 |
} |
|
933 |
*/ |
|
934 |
||
935 |
function strtohex($str) |
|
936 |
{ |
|
937 |
$str = $this->enano_str_split($str); |
|
938 |
$ret = ''; |
|
939 |
foreach($str as $s) |
|
940 |
{ |
|
941 |
$chr = dechex(ord($s)); |
|
942 |
if(strlen($chr) < 2) $chr = '0' . $chr; |
|
943 |
$ret .= $chr; |
|
944 |
} |
|
945 |
return $ret; |
|
946 |
} |
|
947 |
||
948 |
function gen_readymade_key() |
|
949 |
{ |
|
950 |
$key = $this->strtohex($this->randkey($this->keySizeInBits / 8)); |
|
951 |
return $key; |
|
952 |
} |
|
953 |
||
954 |
function prepare_string($text) |
|
955 |
{ |
|
956 |
$ret = $this->hexToByteArray($this->strtohex($text)); |
|
957 |
if(count($ret) != strlen($text)) |
|
40
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
958 |
{ |
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
959 |
die('Could not convert string "' . $text . '" to hex byte array for encryption'); |
723bb7acf914
Fixed a lot of bugs with Safari and Konqueror; improved Opera compatibility
Dan
parents:
1
diff
changeset
|
960 |
} |
1 | 961 |
return $ret; |
962 |
} |
|
963 |
||
964 |
/** |
|
965 |
* Decodes a hex string. |
|
966 |
* @param string $hex The hex code to decode |
|
967 |
* @return string |
|
968 |
*/ |
|
969 |
||
970 |
function hextostring($hex) |
|
971 |
{ |
|
972 |
$hex = $this->enano_str_split($hex, 2); |
|
973 |
$bin_key = ''; |
|
974 |
foreach($hex as $nibble) |
|
975 |
{ |
|
976 |
$byte = chr(hexdec($nibble)); |
|
977 |
$bin_key .= $byte; |
|
978 |
} |
|
979 |
return $bin_key; |
|
980 |
} |
|
981 |
} |
|
982 |
||
983 |
/** |
|
984 |
* XXTEA encryption arithmetic library. |
|
985 |
* |
|
986 |
* Copyright (C) 2006 Ma Bingyao <andot@ujn.edu.cn> |
|
987 |
* Version: 1.5 |
|
988 |
* LastModified: Dec 5, 2006 |
|
989 |
* This library is free. You can redistribute it and/or modify it. |
|
990 |
* |
|
991 |
* From dandaman32: I am treating this code as GPL, as implied by the license statement above. |
|
992 |
*/ |
|
993 |
class TEACrypt extends AESCrypt { |
|
994 |
function long2str($v, $w) { |
|
995 |
$len = count($v); |
|
996 |
$n = ($len - 1) << 2; |
|
997 |
if ($w) { |
|
998 |
$m = $v[$len - 1]; |
|
999 |
if (($m < $n - 3) || ($m > $n)) return false; |
|
1000 |
$n = $m; |
|
1001 |
} |
|
1002 |
$s = array(); |
|
1003 |
for ($i = 0; $i < $len; $i++) { |
|
1004 |
$s[$i] = pack("V", $v[$i]); |
|
1005 |
} |
|
1006 |
if ($w) { |
|
1007 |
return substr(join('', $s), 0, $n); |
|
1008 |
} |
|
1009 |
else { |
|
1010 |
return join('', $s); |
|
1011 |
} |
|
1012 |
} |
|
1013 |
||
1014 |
function str2long($s, $w) { |
|
1015 |
$v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3)); |
|
1016 |
$v = array_values($v); |
|
1017 |
if ($w) { |
|
1018 |
$v[count($v)] = strlen($s); |
|
1019 |
} |
|
1020 |
return $v; |
|
1021 |
} |
|
1022 |
||
1023 |
function int32($n) { |
|
1024 |
while ($n >= 2147483648) $n -= 4294967296; |
|
1025 |
while ($n <= -2147483649) $n += 4294967296; |
|
1026 |
return (int)$n; |
|
1027 |
} |
|
1028 |
||
267 | 1029 |
// The third parameter is to ensure compatibility with php 6.0-dev |
1030 |
function encrypt($str, $key, $unused = ENC_HEX) { |
|
54
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
1031 |
if ($str == "") |
84b56303cab5
Bugfixes: Login system properly handles blank password situation (returns ""); fading button now works right with relative URLs
Dan
parents:
45
diff
changeset
|
1032 |
{ |
1 | 1033 |
return ""; |
1034 |
} |
|
1035 |
$v = $this->str2long($str, true); |
|
1036 |
$k = $this->str2long($key, false); |
|
1037 |
if (count($k) < 4) { |
|
1038 |
for ($i = count($k); $i < 4; $i++) { |
|
1039 |
$k[$i] = 0; |
|
1040 |
} |
|
1041 |
} |
|
1042 |
$n = count($v) - 1; |
|
1043 |
||
1044 |
$z = $v[$n]; |
|
1045 |
$y = $v[0]; |
|
1046 |
$delta = 0x9E3779B9; |
|
1047 |
$q = floor(6 + 52 / ($n + 1)); |
|
1048 |
$sum = 0; |
|
1049 |
while (0 < $q--) { |
|
1050 |
$sum = $this->int32($sum + $delta); |
|
1051 |
$e = $sum >> 2 & 3; |
|
1052 |
for ($p = 0; $p < $n; $p++) { |
|
1053 |
$y = $v[$p + 1]; |
|
1054 |
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); |
|
1055 |
$z = $v[$p] = $this->int32($v[$p] + $mx); |
|
1056 |
} |
|
1057 |
$y = $v[0]; |
|
1058 |
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); |
|
1059 |
$z = $v[$n] = $this->int32($v[$n] + $mx); |
|
1060 |
} |
|
1061 |
return $this->long2str($v, false); |
|
1062 |
} |
|
1063 |
||
267 | 1064 |
// The third parameter is to ensure compatibility with php 6.0-dev |
1065 |
function decrypt($str, $key, $unused = ENC_HEX) { |
|
1 | 1066 |
if ($str == "") { |
1067 |
return ""; |
|
1068 |
} |
|
1069 |
$v = $this->str2long($str, false); |
|
1070 |
$k = $this->str2long($key, false); |
|
1071 |
if (count($k) < 4) { |
|
1072 |
for ($i = count($k); $i < 4; $i++) { |
|
1073 |
$k[$i] = 0; |
|
1074 |
} |
|
1075 |
} |
|
1076 |
$n = count($v) - 1; |
|
1077 |
||
1078 |
$z = $v[$n]; |
|
1079 |
$y = $v[0]; |
|
1080 |
$delta = 0x9E3779B9; |
|
1081 |
$q = floor(6 + 52 / ($n + 1)); |
|
1082 |
$sum = $this->int32($q * $delta); |
|
1083 |
while ($sum != 0) { |
|
1084 |
$e = $sum >> 2 & 3; |
|
1085 |
for ($p = $n; $p > 0; $p--) { |
|
1086 |
$z = $v[$p - 1]; |
|
1087 |
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); |
|
1088 |
$y = $v[$p] = $this->int32($v[$p] - $mx); |
|
1089 |
} |
|
1090 |
$z = $v[$n]; |
|
1091 |
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z)); |
|
1092 |
$y = $v[0] = $this->int32($v[0] - $mx); |
|
1093 |
$sum = $this->int32($sum - $delta); |
|
1094 |
} |
|
1095 |
return $this->long2str($v, true); |
|
1096 |
} |
|
1097 |
} |
|
1098 |
||
1099 |
?> |