Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Encryption Class |
| 30 | * |
| 31 | * Provides two-way keyed encoding using XOR Hashing and Mcrypt |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/encryption.html |
| 38 | */ |
| 39 | class CI_Encrypt { |
| 40 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 41 | /** |
| 42 | * Reference to the user's encryption key |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 46 | public $encryption_key = ''; |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 47 | |
| 48 | /** |
| 49 | * Type of hash operation |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 53 | protected $_hash_type = 'sha1'; |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 54 | |
| 55 | /** |
| 56 | * Flag for the existance of mcrypt |
| 57 | * |
| 58 | * @var bool |
| 59 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 60 | protected $_mcrypt_exists = FALSE; |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 61 | |
| 62 | /** |
| 63 | * Current cipher to be used with mcrypt |
| 64 | * |
| 65 | * @var string |
| 66 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 67 | protected $_mcrypt_cipher; |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 68 | |
| 69 | /** |
| 70 | * Method for encrypting/decrypting data |
| 71 | * |
| 72 | * @var int |
| 73 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 74 | protected $_mcrypt_mode; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 76 | /** |
| 77 | * Initialize Encryption class |
| 78 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 79 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 81 | $this->_mcrypt_exists = function_exists('mcrypt_encrypt'); |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 82 | log_message('debug', 'Encrypt Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
| 88 | * Fetch the encryption key |
| 89 | * |
| 90 | * Returns it as MD5 in order to have an exact-length 128 bit key. |
| 91 | * Mcrypt is sensitive to keys that are not the correct length |
| 92 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | * @param string |
| 94 | * @return string |
| 95 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 96 | public function get_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | { |
| 98 | if ($key == '') |
| 99 | { |
| 100 | if ($this->encryption_key != '') |
| 101 | { |
| 102 | return $this->encryption_key; |
| 103 | } |
| 104 | |
| 105 | $CI =& get_instance(); |
| 106 | $key = $CI->config->item('encryption_key'); |
| 107 | |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 108 | if ($key === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
| 110 | show_error('In order to use the encryption class requires that you set an encryption key in your config file.'); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return md5($key); |
| 115 | } |
| 116 | |
| 117 | // -------------------------------------------------------------------- |
| 118 | |
| 119 | /** |
| 120 | * Set the encryption key |
| 121 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | * @param string |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 123 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 125 | public function set_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | { |
| 127 | $this->encryption_key = $key; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 128 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
| 134 | * Encode |
| 135 | * |
| 136 | * Encodes the message string using bitwise XOR encoding. |
| 137 | * The key is combined with a random hash, and then it |
| 138 | * too gets converted using XOR. The whole thing is then run |
| 139 | * through mcrypt (if supported) using the randomized key. |
| 140 | * The end result is a double-encrypted message string |
| 141 | * that is randomized with each call to this function, |
| 142 | * even if the supplied message and key are the same. |
| 143 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | * @param string the string to encode |
| 145 | * @param string the key |
| 146 | * @return string |
| 147 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 148 | public function encode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 150 | $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_encode' : '_xor_encode'; |
| 151 | return base64_encode($this->$method($string, $this->get_key($key))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Decode |
| 158 | * |
| 159 | * Reverses the above process |
| 160 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | * @param string |
| 162 | * @param string |
| 163 | * @return string |
| 164 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 165 | public function decode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string)) |
| 168 | { |
| 169 | return FALSE; |
| 170 | } |
| 171 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 172 | $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_decode' : '_xor_decode'; |
| 173 | return $this->$method(base64_decode($string), $this->get_key($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 178 | /** |
| 179 | * Encode from Legacy |
| 180 | * |
| 181 | * Takes an encoded string from the original Encryption class algorithms and |
| 182 | * returns a newly encoded string using the improved method added in 2.0.0 |
| 183 | * This allows for backwards compatibility and a method to transition to the |
| 184 | * new encryption algorithms. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | * |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 186 | * For more details, see http://codeigniter.com/user_guide/installation/upgrade_200.html#encryption |
| 187 | * |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 188 | * @param string |
| 189 | * @param int (mcrypt mode constant) |
| 190 | * @param string |
| 191 | * @return string |
| 192 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 193 | public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '') |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 194 | { |
| 195 | if ($this->_mcrypt_exists === FALSE) |
| 196 | { |
| 197 | log_message('error', 'Encoding from legacy is available only when Mcrypt is in use.'); |
| 198 | return FALSE; |
| 199 | } |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 200 | elseif (preg_match('/[^a-zA-Z0-9\/\+=]/', $string)) |
| 201 | { |
| 202 | return FALSE; |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 205 | // decode it first |
| 206 | // set mode temporarily to what it was when string was encoded with the legacy |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | // algorithm - typically MCRYPT_MODE_ECB |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 208 | $current_mode = $this->_get_mode(); |
| 209 | $this->set_mode($legacy_mode); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 211 | $key = $this->get_key($key); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 212 | $dec = base64_decode($string); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 213 | if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE) |
| 214 | { |
| 215 | return FALSE; |
| 216 | } |
| 217 | |
| 218 | $dec = $this->_xor_decode($dec, $key); |
| 219 | |
| 220 | // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC |
Derek Jones | 092103e | 2010-09-02 11:11:58 -0500 | [diff] [blame] | 221 | $this->set_mode($current_mode); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 222 | |
| 223 | // and re-encode |
| 224 | return base64_encode($this->mcrypt_encode($dec, $key)); |
| 225 | } |
| 226 | |
| 227 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | /** |
| 230 | * XOR Encode |
| 231 | * |
| 232 | * Takes a plain-text string and key as input and generates an |
| 233 | * encoded bit-string using XOR |
| 234 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | * @param string |
| 236 | * @param string |
| 237 | * @return string |
| 238 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 239 | protected function _xor_encode($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
| 241 | $rand = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 242 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
| 244 | $rand .= mt_rand(0, mt_getrandmax()); |
| 245 | } |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 246 | while (strlen($rand) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | |
| 248 | $rand = $this->hash($rand); |
| 249 | |
| 250 | $enc = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 251 | for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 253 | $enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return $this->_xor_merge($enc, $key); |
| 257 | } |
| 258 | |
| 259 | // -------------------------------------------------------------------- |
| 260 | |
| 261 | /** |
| 262 | * XOR Decode |
| 263 | * |
| 264 | * Takes an encoded string and key as input and generates the |
| 265 | * plain-text original message |
| 266 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | * @param string |
| 268 | * @param string |
| 269 | * @return string |
| 270 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 271 | protected function _xor_decode($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
| 273 | $string = $this->_xor_merge($string, $key); |
| 274 | |
| 275 | $dec = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 276 | for ($i = 0, $l = strlen($string); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 278 | $dec .= ($string[$i++] ^ $string[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | return $dec; |
| 282 | } |
| 283 | |
| 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * XOR key + string Combiner |
| 288 | * |
| 289 | * Takes a string and key as input and computes the difference using XOR |
| 290 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | * @param string |
| 292 | * @param string |
| 293 | * @return string |
| 294 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 295 | protected function _xor_merge($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
| 297 | $hash = $this->hash($key); |
| 298 | $str = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 299 | for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 301 | $str .= $string[$i] ^ $hash[($i % $lh)]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return $str; |
| 305 | } |
| 306 | |
| 307 | // -------------------------------------------------------------------- |
| 308 | |
| 309 | /** |
| 310 | * Encrypt using Mcrypt |
| 311 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | * @param string |
| 313 | * @param string |
| 314 | * @return string |
| 315 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 316 | public function mcrypt_encode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | { |
| 318 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
| 319 | $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND); |
| 320 | return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key); |
| 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
| 326 | * Decrypt using Mcrypt |
| 327 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | * @param string |
| 329 | * @param string |
| 330 | * @return string |
| 331 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 332 | public function mcrypt_decode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
| 334 | $data = $this->_remove_cipher_noise($data, $key); |
| 335 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
| 336 | |
| 337 | if ($init_size > strlen($data)) |
| 338 | { |
| 339 | return FALSE; |
| 340 | } |
| 341 | |
| 342 | $init_vect = substr($data, 0, $init_size); |
| 343 | $data = substr($data, $init_size); |
| 344 | return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0"); |
| 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
| 350 | * Adds permuted noise to the IV + encrypted data to protect |
| 351 | * against Man-in-the-middle attacks on CBC mode ciphers |
| 352 | * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV |
| 353 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | * @param string |
| 355 | * @param string |
| 356 | * @return string |
| 357 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 358 | protected function _add_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 360 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | $str = ''; |
| 362 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 363 | for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 365 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | { |
| 367 | $j = 0; |
| 368 | } |
| 369 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 370 | $str .= chr((ord($data[$i]) + ord($key[$j])) % 256); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return $str; |
| 374 | } |
| 375 | |
| 376 | // -------------------------------------------------------------------- |
| 377 | |
| 378 | /** |
| 379 | * Removes permuted noise from the IV + encrypted data, reversing |
| 380 | * _add_cipher_noise() |
| 381 | * |
| 382 | * Function description |
| 383 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 384 | * @param string $data |
| 385 | * @param string $key |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 386 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 388 | protected function _remove_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 390 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | $str = ''; |
| 392 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 393 | for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 395 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | { |
| 397 | $j = 0; |
| 398 | } |
| 399 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 400 | $temp = ord($data[$i]) - ord($key[$j]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | |
| 402 | if ($temp < 0) |
| 403 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 404 | $temp += 256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | $str .= chr($temp); |
| 408 | } |
| 409 | |
| 410 | return $str; |
| 411 | } |
| 412 | |
| 413 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | /** |
| 416 | * Set the Mcrypt Cipher |
| 417 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 418 | * @param int |
| 419 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 421 | public function set_cipher($cipher) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | { |
| 423 | $this->_mcrypt_cipher = $cipher; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 424 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * Set the Mcrypt Mode |
| 431 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 432 | * @param int |
| 433 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | */ |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 435 | public function set_mode($mode) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | { |
| 437 | $this->_mcrypt_mode = $mode; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 438 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // -------------------------------------------------------------------- |
| 442 | |
| 443 | /** |
| 444 | * Get Mcrypt cipher Value |
| 445 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 446 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 448 | protected function _get_cipher() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | { |
| 450 | if ($this->_mcrypt_cipher == '') |
| 451 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 452 | return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | return $this->_mcrypt_cipher; |
| 456 | } |
| 457 | |
| 458 | // -------------------------------------------------------------------- |
| 459 | |
| 460 | /** |
| 461 | * Get Mcrypt Mode Value |
| 462 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 463 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 465 | protected function _get_mode() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | { |
| 467 | if ($this->_mcrypt_mode == '') |
| 468 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 469 | return $this->_mcrypt_mode = MCRYPT_MODE_CBC; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | return $this->_mcrypt_mode; |
| 473 | } |
| 474 | |
| 475 | // -------------------------------------------------------------------- |
| 476 | |
| 477 | /** |
| 478 | * Set the Hash type |
| 479 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | * @param string |
Andrey Andreev | f4cb94e | 2012-01-19 15:16:55 +0200 | [diff] [blame] | 481 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 483 | public function set_hash($type = 'sha1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 485 | $this->_hash_type = ($type !== 'sha1' && $type !== 'md5') ? 'sha1' : $type; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // -------------------------------------------------------------------- |
| 489 | |
| 490 | /** |
| 491 | * Hash encode a string |
| 492 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | * @param string |
| 494 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 496 | public function hash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 498 | return ($this->_hash_type === 'sha1') ? sha1($str) : md5($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | } |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | /* End of file Encrypt.php */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 504 | /* Location: ./system/libraries/Encrypt.php */ |