PHP 加密/解密函數(shù) dencrypt(動態(tài)密文,帶壓縮功能,支持中文)
更新時間:2009年01月30日 18:48:54 作者:
采用SHA1生成密匙簿,超過300個字符使用ZLIB壓縮 支持中文,大家可以測試下。
復制代碼 代碼如下:
// +----------------------------------------------------------------------+
// | 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使用sql server驗證連接數(shù)據(jù)庫的方法
這篇文章主要介紹了php使用sql server驗證連接數(shù)據(jù)庫的方法,以實例形式分析了php采用基于SQL Server驗證進行數(shù)據(jù)庫連接的原理及技巧,并總結了相關注意事項,需要的朋友可以參考下2014-12-12
PHP在innodb引擎下快速代建全文搜索功能簡明教程【基于xunsearch】
這篇文章主要介紹了PHP在innodb引擎下快速代建全文搜索功能的方法,可基于開源搜索引擎xunsearch實現(xiàn),簡明扼要的講述了安裝與使用的步驟與相關操作技巧,需要的朋友可以參考下2016-10-10
PHP nl2br函數(shù) 將換行字符轉成 <br>
PHP nl2br函數(shù) 將換行字符轉成 <br>,不是很了解的朋友可以參考下。2009-08-08

