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