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