原文地址:http://www.oschina.net/CODE/snippet_162279_7186
<?php
classMcrypt
{
/**
* 解密
*
* @param string $encryptedText 已加密字符串
* @param string $key 密钥
* @return string
*/
publicstaticfunction _decrypt($encryptedText,$KEY= NULL)
{
$KEY=$KEY=== NULL ? Config::get('secret_key') : $KEY;
$cryptText=base64_decode($encryptedText);
$ivSize= mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv= mcrypt_create_iv($ivSize, MCRYPT_RAND);
$decryptText= mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $KEY,$cryptText, MCRYPT_MODE_ECB, $iv);
returntrim($decryptText);
}
/**
* 加密
*
* @param string $plainText 未加密字符串
* @param string $key 密钥
*/
publicstaticfunction _encrypt($plainText,$KEY= NULL)
{
$KEY=$KEY=== NULL ? Config::get('secret_key') : $KEY;
$ivSize= mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv= mcrypt_create_iv($ivSize, MCRYPT_RAND);
$encryptText= mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $KEY,$plainText, MCRYPT_MODE_ECB, $iv);
returntrim(base64_encode($encryptText));
}
}
<?php
classCookieextendsMcrypt
{
/**
* 删除cookie
*
* @param array $args
* @return boolean
*/
publicstaticfunction del($args)
{
$NAME=$args['name'];
$domain= isset($args['domain']) ? $args['domain'] : NULL;
returnisset($_COOKIE[$NAME]) ? setcookie($NAME,'', TIME() - 86400, '/',$domain) : TRUE;
}
/**
* 得到指定cookie的值
*
* @param string $name
*/
publicstaticfunction get($NAME)
{
returnisset($_COOKIE[$NAME]) ? parent::_decrypt($_COOKIE[$NAME]) : NULL;
}
/**
* 设置cookie
*
* @param array $args
* @return boolean
*/
publicstaticfunction SET($args)
{
$NAME=$args['name'];
$VALUE= parent::_encrypt($args['value']);
$expire= isset($args['expire']) ? $args['expire'] : NULL;
$path= isset($args['path']) ? $args['path'] : '/';
$domain= isset($args['domain']) ? $args['domain'] : NULL;
$secure= isset($args['secure']) ? $args['secure'] : 0;
returnsetcookie($NAME,$VALUE,$expire,$path,$domain,$secure);
}
}