00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Encrypt {
00030
00031 var $CI;
00032 var $encryption_key = '';
00033 var $_hash_type = 'sha1';
00034 var $_mcrypt_exists = FALSE;
00035 var $_mcrypt_cipher;
00036 var $_mcrypt_mode;
00037
00038
00039
00040
00041
00042
00043
00044 function CI_Encrypt()
00045 {
00046 $this->CI =& get_instance();
00047 $this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE;
00048 log_message('debug', "Encrypt Class Initialized");
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 function get_key($key = '')
00064 {
00065 if ($key == '')
00066 {
00067 if ($this->encryption_key != '')
00068 {
00069 return $this->encryption_key;
00070 }
00071
00072 $CI =& get_instance();
00073 $key = $CI->config->item('encryption_key');
00074
00075 if ($key === FALSE)
00076 {
00077 show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
00078 }
00079 }
00080
00081 return md5($key);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 function set_key($key = '')
00094 {
00095 $this->encryption_key = $key;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 function encode($string, $key = '')
00117 {
00118 $key = $this->get_key($key);
00119 $enc = $this->_xor_encode($string, $key);
00120
00121 if ($this->_mcrypt_exists === TRUE)
00122 {
00123 $enc = $this->mcrypt_encode($enc, $key);
00124 }
00125 return base64_encode($enc);
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 function decode($string, $key = '')
00141 {
00142 $key = $this->get_key($key);
00143
00144 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
00145 {
00146 return FALSE;
00147 }
00148
00149 $dec = base64_decode($string);
00150
00151 if ($this->_mcrypt_exists === TRUE)
00152 {
00153 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
00154 {
00155 return FALSE;
00156 }
00157 }
00158
00159 return $this->_xor_decode($dec, $key);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 function _xor_encode($string, $key)
00176 {
00177 $rand = '';
00178 while (strlen($rand) < 32)
00179 {
00180 $rand .= mt_rand(0, mt_getrandmax());
00181 }
00182
00183 $rand = $this->hash($rand);
00184
00185 $enc = '';
00186 for ($i = 0; $i < strlen($string); $i++)
00187 {
00188 $enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
00189 }
00190
00191 return $this->_xor_merge($enc, $key);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 function _xor_decode($string, $key)
00208 {
00209 $string = $this->_xor_merge($string, $key);
00210
00211 $dec = '';
00212 for ($i = 0; $i < strlen($string); $i++)
00213 {
00214 $dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));
00215 }
00216
00217 return $dec;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 function _xor_merge($string, $key)
00233 {
00234 $hash = $this->hash($key);
00235 $str = '';
00236 for ($i = 0; $i < strlen($string); $i++)
00237 {
00238 $str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
00239 }
00240
00241 return $str;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 function mcrypt_encode($data, $key)
00255 {
00256 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
00257 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
00258 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 function mcrypt_decode($data, $key)
00272 {
00273 $data = $this->_remove_cipher_noise($data, $key);
00274 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
00275
00276 if ($init_size > strlen($data))
00277 {
00278 return FALSE;
00279 }
00280
00281 $init_vect = substr($data, 0, $init_size);
00282 $data = substr($data, $init_size);
00283 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 function _add_cipher_noise($data, $key)
00301 {
00302 $keyhash = $this->hash($key);
00303 $keylen = strlen($keyhash);
00304 $str = '';
00305
00306 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
00307 {
00308 if ($j >= $keylen)
00309 {
00310 $j = 0;
00311 }
00312
00313 $str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
00314 }
00315
00316 return $str;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 function _remove_cipher_noise($data, $key)
00332 {
00333 $keyhash = $this->hash($key);
00334 $keylen = strlen($keyhash);
00335 $str = '';
00336
00337 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
00338 {
00339 if ($j >= $keylen)
00340 {
00341 $j = 0;
00342 }
00343
00344 $temp = ord($data[$i]) - ord($keyhash[$j]);
00345
00346 if ($temp < 0)
00347 {
00348 $temp = $temp + 256;
00349 }
00350
00351 $str .= chr($temp);
00352 }
00353
00354 return $str;
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 function set_cipher($cipher)
00367 {
00368 $this->_mcrypt_cipher = $cipher;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 function set_mode($mode)
00381 {
00382 $this->_mcrypt_mode = $mode;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 function _get_cipher()
00394 {
00395 if ($this->_mcrypt_cipher == '')
00396 {
00397 $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
00398 }
00399
00400 return $this->_mcrypt_cipher;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 function _get_mode()
00412 {
00413 if ($this->_mcrypt_mode == '')
00414 {
00415 $this->_mcrypt_mode = MCRYPT_MODE_ECB;
00416 }
00417
00418 return $this->_mcrypt_mode;
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 function set_hash($type = 'sha1')
00431 {
00432 $this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 function hash($str)
00445 {
00446 return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 function sha1($str)
00459 {
00460 if ( ! function_exists('sha1'))
00461 {
00462 if ( ! function_exists('mhash'))
00463 {
00464 require_once(BASEPATH.'libraries/Sha1'.EXT);
00465 $SH = new CI_SHA;
00466 return $SH->generate($str);
00467 }
00468 else
00469 {
00470 return bin2hex(mhash(MHASH_SHA1, $str));
00471 }
00472 }
00473 else
00474 {
00475 return sha1($str);
00476 }
00477 }
00478
00479 }
00480
00481
00482
00483
00484