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

相关文章

ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼

前言 读过一篇关于Zend Framework2的技术文章《ZF2多级树形路由Route配置实例》,是介绍路由配置的。我觉得很有意思,这是的需求: /user对应用户列表页面 /user...

关于使用coreseek并为其做分页的介绍

coreseek 做分页时找数据总量还真不好找。以为他会给一个方法(函数)什么的去获取,结果却不是。首先需要了解:num_matches: 当前返回的结果数,<= limit设置值...

php生成短域名函数

php生成短域名函数 public function createRandCode($string) { $code = ''; $hex_code = '1qaz2...

php Ajax乱码

而AJAX支持UTF8 好了,先在PHP页上加个header(”content-type:text/html; charset=utf-8″); 告诉网页这个实现的编码是UTF...

php实现购物车功能(以大苹果购物网为例)

php实现购物车功能(以大苹果购物网为例)

首先是几个简单的登录页面 <body> <form action="chuli.php" method="post"> <div style="mar...