From f2dd8cecb41e5569f87ff3111a01d82be7f0fb44 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 8 Oct 2014 17:14:44 +0200 Subject: [PATCH] Fixed mcrypt_encode using key with invalid size --- src/Helpers/TextHelper.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 54e10093..5064fb46 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -218,24 +218,24 @@ class TextHelper public static function encrypt($text) { - $salt = Core::getConfig()->main->salt; + $key = self::getEncryptionKey(); $alg = MCRYPT_RIJNDAEL_256; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND); - return base64_encode($iv) . '|' . base64_encode(mcrypt_encrypt($alg, $salt, $text, $mode, $iv)); + return base64_encode($iv) . '|' . base64_encode(mcrypt_encrypt($alg, $key, $text, $mode, $iv)); } public static function decrypt($text) { try { - $salt = Core::getConfig()->main->salt; + $key = self::getEncryptionKey(); list ($iv, $hash) = explode('|', $text, 2); $iv = base64_decode($iv); $hash = base64_decode($hash); $alg = MCRYPT_RIJNDAEL_256; $mode = MCRYPT_MODE_CBC; - $ret = mcrypt_decrypt($alg, $salt, $hash, $mode, $iv); + $ret = mcrypt_decrypt($alg, $key, $hash, $mode, $iv); $pos = strpos($ret, "\0"); if ($pos !== false) $ret = substr($ret, 0, $pos); @@ -354,4 +354,10 @@ class TextHelper ? $mimeTypes[$mimeType] : null; } + + private static function getEncryptionKey() + { + $salt = Core::getConfig()->main->salt; + return hex2bin(md5($salt)); + } }