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 $this->CI->load->library('validation');
00145
00146 if ($this->CI->validation->valid_base64($string) === FALSE)
00147 {
00148 return FALSE;
00149 }
00150
00151 $dec = base64_decode($string);
00152
00153 if ($this->_mcrypt_exists === TRUE)
00154 {
00155 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
00156 {
00157 return FALSE;
00158 }
00159 }
00160
00161 return $this->_xor_decode($dec, $key);
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 function _xor_encode($string, $key)
00178 {
00179 $rand = '';
00180 while (strlen($rand) < 32)
00181 {
00182 $rand .= mt_rand(0, mt_getrandmax());
00183 }
00184
00185 $rand = $this->hash($rand);
00186
00187 $enc = '';
00188 for ($i = 0; $i < strlen($string); $i++)
00189 {
00190 $enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
00191 }
00192
00193 return $this->_xor_merge($enc, $key);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 function _xor_decode($string, $key)
00210 {
00211 $string = $this->_xor_merge($string, $key);
00212
00213 $dec = '';
00214 for ($i = 0; $i < strlen($string); $i++)
00215 {
00216 $dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));
00217 }
00218
00219 return $dec;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 function _xor_merge($string, $key)
00235 {
00236 $hash = $this->hash($key);
00237 $str = '';
00238 for ($i = 0; $i < strlen($string); $i++)
00239 {
00240 $str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
00241 }
00242
00243 return $str;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 function mcrypt_encode($data, $key)
00257 {
00258 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
00259 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
00260 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 function mcrypt_decode($data, $key)
00274 {
00275 $data = $this->_remove_cipher_noise($data, $key);
00276 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
00277
00278 if ($init_size > strlen($data))
00279 {
00280 return FALSE;
00281 }
00282
00283 $init_vect = substr($data, 0, $init_size);
00284 $data = substr($data, $init_size);
00285 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 function _add_cipher_noise($data, $key)
00303 {
00304 $keyhash = $this->hash($key);
00305 $keylen = strlen($keyhash);
00306 $str = '';
00307
00308 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
00309 {
00310 if ($j >= $keylen)
00311 {
00312 $j = 0;
00313 }
00314
00315 $str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
00316 }
00317
00318 return $str;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 function _remove_cipher_noise($data, $key)
00334 {
00335 $keyhash = $this->hash($key);
00336 $keylen = strlen($keyhash);
00337 $str = '';
00338
00339 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
00340 {
00341 if ($j >= $keylen)
00342 {
00343 $j = 0;
00344 }
00345
00346 $temp = ord($data[$i]) - ord($keyhash[$j]);
00347
00348 if ($temp < 0)
00349 {
00350 $temp = $temp + 256;
00351 }
00352
00353 $str .= chr($temp);
00354 }
00355
00356 return $str;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 function set_cipher($cipher)
00369 {
00370 $this->_mcrypt_cipher = $cipher;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 function set_mode($mode)
00383 {
00384 $this->_mcrypt_mode = $mode;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 function _get_cipher()
00396 {
00397 if ($this->_mcrypt_cipher == '')
00398 {
00399 $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
00400 }
00401
00402 return $this->_mcrypt_cipher;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 function _get_mode()
00414 {
00415 if ($this->_mcrypt_mode == '')
00416 {
00417 $this->_mcrypt_mode = MCRYPT_MODE_ECB;
00418 }
00419
00420 return $this->_mcrypt_mode;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 function set_hash($type = 'sha1')
00433 {
00434 $this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 function hash($str)
00447 {
00448 return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 function sha1($str)
00461 {
00462 if ( ! function_exists('sha1'))
00463 {
00464 if ( ! function_exists('mhash'))
00465 {
00466 require_once(BASEPATH.'libraries/Sha1'.EXT);
00467 $SH = new CI_SHA;
00468 return $SH->generate($str);
00469 }
00470 else
00471 {
00472 return bin2hex(mhash(MHASH_SHA1, $str));
00473 }
00474 }
00475 else
00476 {
00477 return sha1($str);
00478 }
00479 }
00480
00481 }
00482
00483
00484
00485
00486