PHP 加密/解密函数 dencrypt(动态密文,带压缩功能,支持中文)
程序员文章站
2023-03-22 22:30:27
复制代码 代码如下:// +----------------------------------------------------------------------+...
复制代码 代码如下:
// +----------------------------------------------------------------------+
// | willko framework |
// +----------------------------------------------------------------------+
// | copyright (c) 2008-2009 willko cheng |
// +----------------------------------------------------------------------+
// | authors: willko cheng <willko@foxmail.com> |
// +----------------------------------------------------------------------+
// $string 明文 或 密文
// $isencrypt 是否加密
// $key 密匙
// 采用sha1生成密匙簿,超过300个字符使用zlib压缩
function dencrypt($string, $isencrypt = true, $key = key_space) {
if (!isset($string{0}) || !isset($key{0})) {
return false;
}
$dynkey = $isencrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40);
$fixedkey = hash('sha1', $key);
$dynkeypart1 = substr($dynkey, 0, 20);
$dynkeypart2 = substr($dynkey, 20);
$fixedkeypart1 = substr($fixedkey, 0, 20);
$fixedkeypart2 = substr($fixedkey, 20);
$key = hash('sha1', $dynkeypart1 . $fixedkeypart1 . $dynkeypart2 . $fixedkeypart2);
$string = $isencrypt ? $fixedkeypart1 . $string . $dynkeypart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));
$n = 0;
$result = '';
$len = strlen($string);
for ($n = 0; $n < $len; $n++) {
$result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));
}
return $isencrypt ? $dynkey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);
}
上一篇: 冬天多吃这6款红色食物 增强免疫力哦
下一篇: 冬季补肾方法大全 定时饮水拉拉耳垂