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