1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Encryption Class
21 * Provides two-way keyed encoding using XOR Hashing and Mcrypt
23 * @package CodeIgniter
24 * @subpackage Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/encryption.html
32 var $encryption_key = '';
33 var $_hash_type = 'sha1';
34 var $_mcrypt_exists = FALSE;
41 * Simply determines whether the mcrypt library exists.
46 $this->CI =& get_instance();
47 $this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE;
48 log_message('debug', "Encrypt Class Initialized");
51 // --------------------------------------------------------------------
54 * Fetch the encryption key
56 * Returns it as MD5 in order to have an exact-length 128 bit key.
57 * Mcrypt is sensitive to keys that are not the correct length
63 function get_key($key = '')
67 if ($this->encryption_key != '')
69 return $this->encryption_key;
72 $CI =& get_instance();
73 $key = $CI->config->item('encryption_key');
77 show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
84 // --------------------------------------------------------------------
87 * Set the encryption key
93 function set_key($key = '')
95 $this->encryption_key = $key;
98 // --------------------------------------------------------------------
103 * Encodes the message string using bitwise XOR encoding.
104 * The key is combined with a random hash, and then it
105 * too gets converted using XOR. The whole thing is then run
106 * through mcrypt (if supported) using the randomized key.
107 * The end result is a double-encrypted message string
108 * that is randomized with each call to this function,
109 * even if the supplied message and key are the same.
112 * @param string the string to encode
113 * @param string the key
116 function encode($string, $key = '')
118 $key = $this->get_key($key);
119 $enc = $this->_xor_encode($string, $key);
121 if ($this->_mcrypt_exists === TRUE)
123 $enc = $this->mcrypt_encode($enc, $key);
125 return base64_encode($enc);
128 // --------------------------------------------------------------------
133 * Reverses the above process
140 function decode($string, $key = '')
142 $key = $this->get_key($key);
144 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
149 $dec = base64_decode($string);
151 if ($this->_mcrypt_exists === TRUE)
153 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
159 return $this->_xor_decode($dec, $key);
162 // --------------------------------------------------------------------
167 * Takes a plain-text string and key as input and generates an
168 * encoded bit-string using XOR
175 function _xor_encode($string, $key)
178 while (strlen($rand) < 32)
180 $rand .= mt_rand(0, mt_getrandmax());
183 $rand = $this->hash($rand);
186 for ($i = 0; $i < strlen($string); $i++)
188 $enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
191 return $this->_xor_merge($enc, $key);
194 // --------------------------------------------------------------------
199 * Takes an encoded string and key as input and generates the
200 * plain-text original message
207 function _xor_decode($string, $key)
209 $string = $this->_xor_merge($string, $key);
212 for ($i = 0; $i < strlen($string); $i++)
214 $dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));
220 // --------------------------------------------------------------------
223 * XOR key + string Combiner
225 * Takes a string and key as input and computes the difference using XOR
232 function _xor_merge($string, $key)
234 $hash = $this->hash($key);
236 for ($i = 0; $i < strlen($string); $i++)
238 $str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
244 // --------------------------------------------------------------------
247 * Encrypt using Mcrypt
254 function mcrypt_encode($data, $key)
256 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
257 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
258 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
261 // --------------------------------------------------------------------
264 * Decrypt using Mcrypt
271 function mcrypt_decode($data, $key)
273 $data = $this->_remove_cipher_noise($data, $key);
274 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
276 if ($init_size > strlen($data))
281 $init_vect = substr($data, 0, $init_size);
282 $data = substr($data, $init_size);
283 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
286 // --------------------------------------------------------------------
289 * Adds permuted noise to the IV + encrypted data to protect
290 * against Man-in-the-middle attacks on CBC mode ciphers
291 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
293 * Function description
300 function _add_cipher_noise($data, $key)
302 $keyhash = $this->hash($key);
303 $keylen = strlen($keyhash);
306 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
313 $str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
319 // --------------------------------------------------------------------
322 * Removes permuted noise from the IV + encrypted data, reversing
323 * _add_cipher_noise()
325 * Function description
331 function _remove_cipher_noise($data, $key)
333 $keyhash = $this->hash($key);
334 $keylen = strlen($keyhash);
337 for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
344 $temp = ord($data[$i]) - ord($keyhash[$j]);
357 // --------------------------------------------------------------------
360 * Set the Mcrypt Cipher
366 function set_cipher($cipher)
368 $this->_mcrypt_cipher = $cipher;
371 // --------------------------------------------------------------------
374 * Set the Mcrypt Mode
380 function set_mode($mode)
382 $this->_mcrypt_mode = $mode;
385 // --------------------------------------------------------------------
388 * Get Mcrypt cipher Value
393 function _get_cipher()
395 if ($this->_mcrypt_cipher == '')
397 $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
400 return $this->_mcrypt_cipher;
403 // --------------------------------------------------------------------
406 * Get Mcrypt Mode Value
413 if ($this->_mcrypt_mode == '')
415 $this->_mcrypt_mode = MCRYPT_MODE_ECB;
418 return $this->_mcrypt_mode;
421 // --------------------------------------------------------------------
430 function set_hash($type = 'sha1')
432 $this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;
435 // --------------------------------------------------------------------
438 * Hash encode a string
446 return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);
449 // --------------------------------------------------------------------
452 * Generate an SHA1 Hash
460 if ( ! function_exists('sha1'))
462 if ( ! function_exists('mhash'))
464 require_once(BASEPATH.'libraries/Sha1'.EXT);
466 return $SH->generate($str);
470 return bin2hex(mhash(MHASH_SHA1, $str));
481 // END CI_Encrypt class
483 /* End of file Encrypt.php */
484 /* Location: ./system/libraries/Encrypt.php */