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

yipeiwu_com6年前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 数组遍历顺序理解

比如: <?php$arr['laruence'] = 'huixinchen';$arr['yahoo']    = 2007;$arr['baidu']...

phpstudy默认不支持64位php的解决方法

备忘一下: windows上用phpstudy比较简便,但是其默认的php所有版本都是32位的,有坑,比如int最大值。 所以从php官网 点击打开链接http://windows.ph...

php的access操作类

复制代码 代码如下:<?php     --------------------------------------------------...

PHP 中文处理技巧

折腾了一天的时间,才弄出点眉目来。 做AJAX应用或Flash应用,提交中文内容到后台,涉及到编码解码(encode、decode)及编码格式的转换。 网上的PHP端escape une...

php使用iconv中文截断问题的解决方法

本文实例讲述了php使用iconv中文截断问题的解决方法。分享给大家供大家参考。具体分析如下: 今天做了一个采集程序,原理很简单,使用curl方法把对方页面的html获取分析,然后正则提...