2014-05-27 07:04:25 +00:00
|
|
|
<?php
|
|
|
|
namespace Helpers;
|
|
|
|
|
|
|
|
class MCrypt
|
|
|
|
{
|
2014-06-10 02:16:50 +00:00
|
|
|
private $config;
|
|
|
|
private $code;
|
|
|
|
private $salt;
|
|
|
|
|
2014-05-27 07:04:25 +00:00
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->config = \Zend_Registry::get('config');
|
2014-06-10 02:16:50 +00:00
|
|
|
$this->code = isset($this->config->auth->mcryptcode) ? $this->config->auth->mcryptcode : "SJY001";
|
|
|
|
$this->salt = '!kQm*fF3pXe1Kbm%9';
|
|
|
|
}
|
|
|
|
|
|
|
|
//invoke encrypt
|
|
|
|
public function encode()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//invoke decrypt
|
|
|
|
public function decode()
|
|
|
|
{
|
|
|
|
|
2014-05-27 07:04:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:16:50 +00:00
|
|
|
public function encrypt($decrypted) {
|
2014-05-27 07:04:25 +00:00
|
|
|
|
2014-06-10 02:16:50 +00:00
|
|
|
$key = hash('SHA256', $this->salt.$this->code , true);
|
2014-05-27 07:04:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:16:50 +00:00
|
|
|
public function decrypt($encrypted) {
|
2014-05-27 07:04:25 +00:00
|
|
|
|
2014-06-10 02:16:50 +00:00
|
|
|
$key = hash('SHA256', $this->salt.$this->code , true);
|
2014-05-27 07:04:25 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|