PHP 加密/解密函数 dencrypt(动态密文,带压缩功能,支持中文)

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

// +----------------------------------------------------------------------+
// | 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);
}

相关文章

php中关于换行的实例写法

在编写PHP脚本代码的时候,我们经常会看到\n和<br/>这两个字符,它们都有换行的作用,那么到底有什么区别呢? 1.\n是使源代码换行,而浏览器显示的内容不换行;...

php时间不正确的解决方法

2。date_default_timezone_set("PRC");    3。PHP   5.1以上  ...

PHP的几个常用加密函数

MD5加密: string md5 ( string $str [, bool $raw_output = false ] ) 1.md5()默认情况下以 32 字符十六进制数字形式返回...

php如何实现只替换一次或N次

 我们都知道,在PHP里Strtr,strreplace等函数都可以用来替换,不过他们每次替换的时候都是全部替换,举个例子: "abcabbc",这个字符串如果使用上边的函数来...

PHP 全角转半角实现代码

复制代码 代码如下: /** * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符 * @access public * @param string $s...