From cc51d943e22b0d04f83c113b09bf5d88360f0ce0 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 21 Apr 2014 09:31:41 +0200 Subject: [PATCH] Fixed CBC encryption - added IV to cookie --- src/Helpers/TextHelper.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 1ec48994..825e09e8 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -182,16 +182,25 @@ class TextHelper $alg = MCRYPT_RIJNDAEL_256; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND); - return trim(base64_encode(mcrypt_encrypt($alg, $salt, $text, $mode, $iv))); + return base64_encode($iv) . '|' . base64_encode(mcrypt_encrypt($alg, $salt, $text, $mode, $iv)); } public static function decrypt($text) { - $salt = \Chibi\Registry::getConfig()->main->salt; - $alg = MCRYPT_RIJNDAEL_256; - $mode = MCRYPT_MODE_CBC; - $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND); - return trim(mcrypt_decrypt($alg, $salt, base64_decode($text), $mode, $iv)); + try + { + $salt = \Chibi\Registry::getConfig()->main->salt; + list ($iv, $hash) = explode('|', $text, 2); + $iv = base64_decode($iv); + $hash = base64_decode($hash); + $alg = MCRYPT_RIJNDAEL_256; + $mode = MCRYPT_MODE_CBC; + return trim(mcrypt_decrypt($alg, $salt, $hash, $mode, $iv)); + } + catch (Exception $e) + { + throw new SimpleException('Supplied input is not valid encrypted text'); + } } public static function cleanPath($path)