From e6ed812e8d071fb9404290e38d7e8b7b1a368505 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Thu, 28 Nov 2013 09:01:11 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86MCrypt=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E5=8A=A9=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/module/Helpers/MCrypt.php | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 application/module/Helpers/MCrypt.php diff --git a/application/module/Helpers/MCrypt.php b/application/module/Helpers/MCrypt.php new file mode 100644 index 00000000..542d6de2 --- /dev/null +++ b/application/module/Helpers/MCrypt.php @@ -0,0 +1,46 @@ +config = \Zend_Registry::get('config'); + } + + static function encrypt($decrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') { + + $key = hash('SHA256', $salt . $password, true); + + srand(); + + $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND); + + if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false; + + $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv)); + + return $iv_base64 . $encrypted; + } + + static function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') { + + $key = hash('SHA256', $salt . $password, true); + + $iv = base64_decode(substr($encrypted, 0, 22) . '=='); + + $encrypted = substr($encrypted, 22); + + $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4"); + + $hash = substr($decrypted, -32); + + $decrypted = substr($decrypted, 0, -32); + + if (md5($decrypted) != $hash) return false; + + return $decrypted; + } + + +} \ No newline at end of file