CI_Encrypt Class Reference

List of all members.


Public Member Functions

 CI_Encrypt ()
 Constructor.
 get_key ($key= '')
 Fetch the encryption key.
 set_key ($key= '')
 Set the encryption key.
 encode ($string, $key= '')
 Encode.
 decode ($string, $key= '')
 Decode.
 _xor_encode ($string, $key)
 XOR Encode.
 _xor_decode ($string, $key)
 XOR Decode.
 _xor_merge ($string, $key)
 XOR key + string Combiner.
 mcrypt_encode ($data, $key)
 Encrypt using Mcrypt.
 mcrypt_decode ($data, $key)
 Decrypt using Mcrypt.
 _add_cipher_noise ($data, $key)
 Adds permuted noise to the IV + encrypted data to protect against Man-in-the-middle attacks on CBC mode ciphers http://www.ciphersbyritter.com/GLOSSARY.HTM#IV.
 _remove_cipher_noise ($data, $key)
 Removes permuted noise from the IV + encrypted data, reversing _add_cipher_noise().
 set_cipher ($cipher)
 Set the Mcrypt Cipher.
 set_mode ($mode)
 Set the Mcrypt Mode.
 _get_cipher ()
 Get Mcrypt cipher Value.
 _get_mode ()
 Get Mcrypt Mode Value.
 set_hash ($type= 'sha1')
 Set the Hash type.
 hash ($str)
 Hash encode a string.
 sha1 ($str)
 Generate an SHA1 Hash.

Public Attributes

 $CI
 $encryption_key = ''
 $_hash_type = 'sha1'
 $_mcrypt_exists = FALSE
 $_mcrypt_cipher
 $_mcrypt_mode

Detailed Description

Definition at line 29 of file Encrypt.php.


Member Function Documentation

CI_Encrypt::_add_cipher_noise ( data,
key 
)

Adds permuted noise to the IV + encrypted data to protect against Man-in-the-middle attacks on CBC mode ciphers http://www.ciphersbyritter.com/GLOSSARY.HTM#IV.

Function description

private

Parameters:
string 
string 
Returns:
string

Definition at line 300 of file Encrypt.php.

References hash().

Referenced by mcrypt_encode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::_get_cipher (  ) 

Get Mcrypt cipher Value.

private

Returns:
string

Definition at line 393 of file Encrypt.php.

Referenced by mcrypt_decode(), and mcrypt_encode().

00394         {
00395                 if ($this->_mcrypt_cipher == '')
00396                 {
00397                         $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
00398                 }
00399 
00400                 return $this->_mcrypt_cipher;
00401         }

Here is the caller graph for this function:

CI_Encrypt::_get_mode (  ) 

Get Mcrypt Mode Value.

private

Returns:
string

Definition at line 411 of file Encrypt.php.

Referenced by mcrypt_decode(), and mcrypt_encode().

00412         {
00413                 if ($this->_mcrypt_mode == '')
00414                 {
00415                         $this->_mcrypt_mode = MCRYPT_MODE_ECB;
00416                 }
00417                 
00418                 return $this->_mcrypt_mode;
00419         }

Here is the caller graph for this function:

CI_Encrypt::_remove_cipher_noise ( data,
key 
)

Removes permuted noise from the IV + encrypted data, reversing _add_cipher_noise().

Function description

public

Parameters:
type 
Returns:
type

Definition at line 331 of file Encrypt.php.

References hash().

Referenced by mcrypt_decode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::_xor_decode ( string,
key 
)

XOR Decode.

Takes an encoded string and key as input and generates the plain-text original message

private

Parameters:
string 
string 
Returns:
string

Definition at line 207 of file Encrypt.php.

References _xor_merge().

Referenced by decode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::_xor_encode ( string,
key 
)

XOR Encode.

Takes a plain-text string and key as input and generates an encoded bit-string using XOR

private

Parameters:
string 
string 
Returns:
string

Definition at line 175 of file Encrypt.php.

References _xor_merge(), and hash().

Referenced by encode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::_xor_merge ( string,
key 
)

XOR key + string Combiner.

Takes a string and key as input and computes the difference using XOR

private

Parameters:
string 
string 
Returns:
string

Definition at line 232 of file Encrypt.php.

References hash().

Referenced by _xor_decode(), and _xor_encode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::CI_Encrypt (  ) 

Constructor.

Simply determines whether the mcrypt library exists.

Definition at line 44 of file Encrypt.php.

References get_instance(), and log_message().

00045         {
00046                 $this->CI =& get_instance();
00047                 $this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE;
00048                 log_message('debug', "Encrypt Class Initialized");
00049         }

Here is the call graph for this function:

CI_Encrypt::decode ( string,
key = '' 
)

Decode.

Reverses the above process

public

Parameters:
string 
string 
Returns:
string

Definition at line 140 of file Encrypt.php.

References _xor_decode(), get_key(), and mcrypt_decode().

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         }

Here is the call graph for this function:

CI_Encrypt::encode ( string,
key = '' 
)

Encode.

Encodes the message string using bitwise XOR encoding. The key is combined with a random hash, and then it too gets converted using XOR. The whole thing is then run through mcrypt (if supported) using the randomized key. The end result is a double-encrypted message string that is randomized with each call to this function, even if the supplied message and key are the same.

public

Parameters:
string the string to encode
string the key
Returns:
string

Definition at line 116 of file Encrypt.php.

References _xor_encode(), get_key(), and mcrypt_encode().

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         }

Here is the call graph for this function:

CI_Encrypt::get_key ( key = ''  ) 

Fetch the encryption key.

Returns it as MD5 in order to have an exact-length 128 bit key. Mcrypt is sensitive to keys that are not the correct length

public

Parameters:
string 
Returns:
string

Definition at line 63 of file Encrypt.php.

References $CI, get_instance(), and show_error().

Referenced by decode(), and encode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::hash ( str  ) 

Hash encode a string.

public

Parameters:
string 
Returns:
string

Definition at line 444 of file Encrypt.php.

References sha1().

Referenced by _add_cipher_noise(), _remove_cipher_noise(), _xor_encode(), and _xor_merge().

00445         {
00446                 return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);
00447         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::mcrypt_decode ( data,
key 
)

Decrypt using Mcrypt.

public

Parameters:
string 
string 
Returns:
string

Definition at line 271 of file Encrypt.php.

References _get_cipher(), _get_mode(), and _remove_cipher_noise().

Referenced by decode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::mcrypt_encode ( data,
key 
)

Encrypt using Mcrypt.

public

Parameters:
string 
string 
Returns:
string

Definition at line 254 of file Encrypt.php.

References _add_cipher_noise(), _get_cipher(), and _get_mode().

Referenced by encode().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Encrypt::set_cipher ( cipher  ) 

Set the Mcrypt Cipher.

public

Parameters:
constant 
Returns:
string

Definition at line 366 of file Encrypt.php.

00367         {
00368                 $this->_mcrypt_cipher = $cipher;
00369         }

CI_Encrypt::set_hash ( type = 'sha1'  ) 

Set the Hash type.

public

Parameters:
string 
Returns:
string

Definition at line 430 of file Encrypt.php.

00431         {
00432                 $this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;
00433         }

CI_Encrypt::set_key ( key = ''  ) 

Set the encryption key.

public

Parameters:
string 
Returns:
void

Definition at line 93 of file Encrypt.php.

00094         {
00095                 $this->encryption_key = $key;
00096         }

CI_Encrypt::set_mode ( mode  ) 

Set the Mcrypt Mode.

public

Parameters:
constant 
Returns:
string

Definition at line 380 of file Encrypt.php.

00381         {
00382                 $this->_mcrypt_mode = $mode;
00383         }

CI_Encrypt::sha1 ( str  ) 

Generate an SHA1 Hash.

public

Parameters:
string 
Returns:
string

Definition at line 458 of file Encrypt.php.

Referenced by hash().

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         }

Here is the caller graph for this function:


Member Data Documentation

CI_Encrypt::$_hash_type = 'sha1'

Definition at line 33 of file Encrypt.php.

CI_Encrypt::$_mcrypt_cipher

Definition at line 35 of file Encrypt.php.

CI_Encrypt::$_mcrypt_exists = FALSE

Definition at line 34 of file Encrypt.php.

CI_Encrypt::$_mcrypt_mode

Definition at line 36 of file Encrypt.php.

CI_Encrypt::$CI

Definition at line 31 of file Encrypt.php.

Referenced by get_key().

CI_Encrypt::$encryption_key = ''

Definition at line 32 of file Encrypt.php.


The documentation for this class was generated from the following file: