Skip to content

Commit 99015bc

Browse files
author
Phil Sturgeon
committed
Merge pull request bcit-ci#842 from narfbg/develop-encrypt
Improve the Encryption library
2 parents d5e07b0 + ed65313 commit 99015bc

File tree

1 file changed

+37
-75
lines changed

1 file changed

+37
-75
lines changed

system/libraries/Encrypt.php

Lines changed: 37 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22
/**
33
* CodeIgniter
44
*
55
* An open source application development framework for PHP 5.1.6 or newer
66
*
77
* NOTICE OF LICENSE
8-
*
8+
*
99
* Licensed under the Open Software License version 3.0
10-
*
10+
*
1111
* This source file is subject to the Open Software License (OSL 3.0) that is
1212
* bundled with this package in the files license.txt / license.rst. It is
1313
* also available through the world wide web at this URL:
@@ -25,8 +25,6 @@
2525
* @filesource
2626
*/
2727

28-
// ------------------------------------------------------------------------
29-
3028
/**
3129
* CodeIgniter Encryption Class
3230
*
@@ -46,15 +44,10 @@ class CI_Encrypt {
4644
protected $_mcrypt_cipher;
4745
protected $_mcrypt_mode;
4846

49-
/**
50-
* Constructor
51-
*
52-
* Simply determines whether the mcrypt library exists.
53-
*/
5447
public function __construct()
5548
{
5649
$this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE;
57-
log_message('debug', "Encrypt Class Initialized");
50+
log_message('debug', 'Encrypt Class Initialized');
5851
}
5952

6053
// --------------------------------------------------------------------
@@ -95,7 +88,7 @@ public function get_key($key = '')
9588
* Set the encryption key
9689
*
9790
* @param string
98-
* @return void
91+
* @return object
9992
*/
10093
public function set_key($key = '')
10194
{
@@ -122,18 +115,8 @@ public function set_key($key = '')
122115
*/
123116
public function encode($string, $key = '')
124117
{
125-
$key = $this->get_key($key);
126-
127-
if ($this->_mcrypt_exists === TRUE)
128-
{
129-
$enc = $this->mcrypt_encode($string, $key);
130-
}
131-
else
132-
{
133-
$enc = $this->_xor_encode($string, $key);
134-
}
135-
136-
return base64_encode($enc);
118+
$method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_encode' : '_xor_encode';
119+
return base64_encode($this->$method($string, $this->get_key($key)));
137120
}
138121

139122
// --------------------------------------------------------------------
@@ -149,28 +132,13 @@ public function encode($string, $key = '')
149132
*/
150133
public function decode($string, $key = '')
151134
{
152-
$key = $this->get_key($key);
153-
154135
if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
155136
{
156137
return FALSE;
157138
}
158139

159-
$dec = base64_decode($string);
160-
161-
if ($this->_mcrypt_exists === TRUE)
162-
{
163-
if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
164-
{
165-
return FALSE;
166-
}
167-
}
168-
else
169-
{
170-
$dec = $this->_xor_decode($dec, $key);
171-
}
172-
173-
return $dec;
140+
$method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_decode' : '_xor_decode';
141+
return $this->$method(base64_decode($string), $this->get_key($key));
174142
}
175143

176144
// --------------------------------------------------------------------
@@ -197,6 +165,10 @@ public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key
197165
log_message('error', 'Encoding from legacy is available only when Mcrypt is in use.');
198166
return FALSE;
199167
}
168+
elseif (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
169+
{
170+
return FALSE;
171+
}
200172

201173
// decode it first
202174
// set mode temporarily to what it was when string was encoded with the legacy
@@ -205,14 +177,7 @@ public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key
205177
$this->set_mode($legacy_mode);
206178

207179
$key = $this->get_key($key);
208-
209-
if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
210-
{
211-
return FALSE;
212-
}
213-
214180
$dec = base64_decode($string);
215-
216181
if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
217182
{
218183
return FALSE;
@@ -242,17 +207,18 @@ public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key
242207
protected function _xor_encode($string, $key)
243208
{
244209
$rand = '';
245-
while (strlen($rand) < 32)
210+
do
246211
{
247212
$rand .= mt_rand(0, mt_getrandmax());
248213
}
214+
while (strlen($rand) < 32);
249215

250216
$rand = $this->hash($rand);
251217

252218
$enc = '';
253-
for ($i = 0; $i < strlen($string); $i++)
219+
for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++)
254220
{
255-
$enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
221+
$enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]);
256222
}
257223

258224
return $this->_xor_merge($enc, $key);
@@ -275,9 +241,9 @@ protected function _xor_decode($string, $key)
275241
$string = $this->_xor_merge($string, $key);
276242

277243
$dec = '';
278-
for ($i = 0; $i < strlen($string); $i++)
244+
for ($i = 0, $l = strlen($string); $i < $l; $i++)
279245
{
280-
$dec .= (substr($string, $i++, 1) ^ substr($string, $i, 1));
246+
$dec .= ($string[$i++] ^ $string[$i]);
281247
}
282248

283249
return $dec;
@@ -298,9 +264,9 @@ protected function _xor_merge($string, $key)
298264
{
299265
$hash = $this->hash($key);
300266
$str = '';
301-
for ($i = 0; $i < strlen($string); $i++)
267+
for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++)
302268
{
303-
$str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
269+
$str .= $string[$i] ^ $hash[($i % $lh)];
304270
}
305271

306272
return $str;
@@ -359,18 +325,17 @@ public function mcrypt_decode($data, $key)
359325
*/
360326
protected function _add_cipher_noise($data, $key)
361327
{
362-
$keyhash = $this->hash($key);
363-
$keylen = strlen($keyhash);
328+
$key = $this->hash($key);
364329
$str = '';
365330

366-
for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
331+
for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
367332
{
368-
if ($j >= $keylen)
333+
if ($j >= $lk)
369334
{
370335
$j = 0;
371336
}
372337

373-
$str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
338+
$str .= chr((ord($data[$i]) + ord($key[$j])) % 256);
374339
}
375340

376341
return $str;
@@ -389,22 +354,21 @@ protected function _add_cipher_noise($data, $key)
389354
*/
390355
protected function _remove_cipher_noise($data, $key)
391356
{
392-
$keyhash = $this->hash($key);
393-
$keylen = strlen($keyhash);
357+
$key = $this->hash($key);
394358
$str = '';
395359

396-
for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
360+
for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
397361
{
398-
if ($j >= $keylen)
362+
if ($j >= $lk)
399363
{
400364
$j = 0;
401365
}
402366

403-
$temp = ord($data[$i]) - ord($keyhash[$j]);
367+
$temp = ord($data[$i]) - ord($key[$j]);
404368

405369
if ($temp < 0)
406370
{
407-
$temp = $temp + 256;
371+
$temp += 256;
408372
}
409373

410374
$str .= chr($temp);
@@ -435,7 +399,7 @@ public function set_cipher($cipher)
435399
* @param constant
436400
* @return string
437401
*/
438-
function set_mode($mode)
402+
public function set_mode($mode)
439403
{
440404
$this->_mcrypt_mode = $mode;
441405
return $this;
@@ -452,7 +416,7 @@ protected function _get_cipher()
452416
{
453417
if ($this->_mcrypt_cipher == '')
454418
{
455-
$this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
419+
return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
456420
}
457421

458422
return $this->_mcrypt_cipher;
@@ -469,7 +433,7 @@ protected function _get_mode()
469433
{
470434
if ($this->_mcrypt_mode == '')
471435
{
472-
$this->_mcrypt_mode = MCRYPT_MODE_CBC;
436+
return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
473437
}
474438

475439
return $this->_mcrypt_mode;
@@ -481,11 +445,11 @@ protected function _get_mode()
481445
* Set the Hash type
482446
*
483447
* @param string
484-
* @return string
448+
* @return void
485449
*/
486450
public function set_hash($type = 'sha1')
487451
{
488-
$this->_hash_type = ($type != 'sha1' AND $type != 'md5') ? 'sha1' : $type;
452+
$this->_hash_type = ($type !== 'sha1' && $type !== 'md5') ? 'sha1' : $type;
489453
}
490454

491455
// --------------------------------------------------------------------
@@ -498,11 +462,9 @@ public function set_hash($type = 'sha1')
498462
*/
499463
public function hash($str)
500464
{
501-
return ($this->_hash_type == 'sha1') ? sha1($str) : md5($str);
465+
return ($this->_hash_type === 'sha1') ? sha1($str) : md5($str);
502466
}
503467
}
504468

505-
// END CI_Encrypt class
506-
507469
/* End of file Encrypt.php */
508-
/* Location: ./system/libraries/Encrypt.php */
470+
/* Location: ./system/libraries/Encrypt.php */

0 commit comments

Comments
 (0)