UCenter中的一個可逆加密函數(shù)authcode函數(shù)代碼
更新時間:2010年07月20日 01:40:37 作者:
瀏覽UCenter源代碼的時候發(fā)現(xiàn)這個函數(shù),剛好有需要,就記錄一下。
復(fù)制代碼 代碼如下:
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4; // 隨機密鑰長度 取值 0-32;
// 加入隨機密鑰,可以令密文無任何規(guī)律,即便是原文和密鑰完全相同,加密結(jié)果也會每次不同,增大破解難度。
// 取值越大,密文變動規(guī)律越大,密文變化 = 16 的 $ckey_length 次方
// 當(dāng)此值為 0 時,則不產(chǎn)生隨機密鑰
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
/***************************************************************************/
$string = authcode("Hello","ENCODE","HTML_TCCJ_AUTH");
echo $string,'<br/>';
echo authcode($string,"DECODE","HTML_TCCJ_AUTH"),'<br/>';
相關(guān)文章
php+mysqli使用面向?qū)ο蠓绞讲樵償?shù)據(jù)庫實例
這篇文章主要介紹了php+mysqli使用面向?qū)ο蠓绞讲樵償?shù)據(jù)庫的方法,實例分析了mysqli對象的創(chuàng)建、連接、查詢及返回結(jié)果、釋放資源等技巧,需要的朋友可以參考下2015-01-01php實現(xiàn)有序數(shù)組旋轉(zhuǎn)后尋找最小值方法
在本篇文章中我們給大家詳細分享了php實現(xiàn)有序數(shù)組旋轉(zhuǎn)后尋找最小值方法,有需要的朋友們可以學(xué)習(xí)下。2018-09-09php switch語句多個值匹配同一代碼塊的實現(xiàn)
switch 語句一行接一行地執(zhí)行(實際上是語句接語句),下面為大家介紹下php switch語句多個值匹配同一代碼塊2014-03-03詳解如何使用PHP實現(xiàn)動態(tài)代理IP的功能
動態(tài)代理IP是一種通過不斷切換不同的代理IP來隱藏真實IP地址的技術(shù),動態(tài)代理IP可以有效地解決IP被封鎖或訪問限制的問題,本文將使用PHP語言實現(xiàn)動態(tài)代理IP的功能,需要的朋友可以參考下2024-03-03