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 302 of file Encrypt.php.

References hash().

Referenced by mcrypt_encode().

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         }

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 395 of file Encrypt.php.

Referenced by mcrypt_decode(), and mcrypt_encode().

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

Here is the caller graph for this function:

CI_Encrypt::_get_mode (  ) 

Get Mcrypt Mode Value.

private

Returns:
string

Definition at line 413 of file Encrypt.php.

Referenced by mcrypt_decode(), and mcrypt_encode().

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

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 333 of file Encrypt.php.

References hash().

Referenced by mcrypt_decode().

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         }

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 209 of file Encrypt.php.

References _xor_merge().

Referenced by decode().

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         }

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 177 of file Encrypt.php.

References _xor_merge(), and hash().

Referenced by encode().

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         }

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 234 of file Encrypt.php.

References hash().

Referenced by _xor_decode(), and _xor_encode().

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         }

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                 $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         }

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 446 of file Encrypt.php.

References sha1().

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

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

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 273 of file Encrypt.php.

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

Referenced by decode().

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         }

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 256 of file Encrypt.php.

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

Referenced by encode().

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         }

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 368 of file Encrypt.php.

00369         {
00370                 $this->_mcrypt_cipher = $cipher;
00371         }

CI_Encrypt::set_hash ( type = 'sha1'  ) 

Set the Hash type.

public

Parameters:
string 
Returns:
string

Definition at line 432 of file Encrypt.php.

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

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 382 of file Encrypt.php.

00383         {
00384                 $this->_mcrypt_mode = $mode;
00385         }

CI_Encrypt::sha1 ( str  ) 

Generate an SHA1 Hash.

public

Parameters:
string 
Returns:
string

Definition at line 460 of file Encrypt.php.

Referenced by hash().

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         }

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: