安静的栖居

安静的栖居

2007年7月20日星期五

PHP的加密算法并一些应用

function passport_encrypt($txt, $key) {
srand((double)microtime() * 1000000);
$encrypt_key = md5(rand(0, 32000));
$ctr = 0;
$tmp = '';
for($i = 0;$i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
}
return base64_encode(passport_key($tmp, $key));
}

1 )在php中 microtime()返回的是两个值,一个是当前毫秒和时间戳。(double)microtime()可以只返回当前毫秒值。

2)srand()是种的种子,php4.3以后就不需要在srand,然后rand($min_num, $max_num)了。但是为了兼容性质,最好还是使用srand()做一个种子。

3)$first ? $second : $third 的意思是 如果满足条件$first ,则返回$second ,否则则返回 $third。

4)$a = 'abcdeft' ,如果输出$a[0],则输出为a . 所以 $a[$i] ,相当于 substr($a,$i,1)。

5) php中 ^符号的意思是卫运算符(寒!!!很少用,真不知道是什么意思!!以后有时间需要经常看看手册)。

6) 对每个字符进行移位,参考php手册里的位运算符。

7).............待续




function passport_key($txt, $encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}

标签:

0 条评论:

发表评论

订阅 博文评论 [Atom]

<< 主页