安静的栖居

安静的栖居

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

标签:

php中{}的意思(转)

小弟我最近学习php,观摩人家的源代码。由于没有注释,有些不明白,请大虾们帮帮忙,首先在此谢过!
源代码中有些地方{1},{mod}等形式,不知{}是不是解释变量的意思?我翻了一些书也没找到

cnredarrow 回复于:2004-06-02 09:38:16
{程序段的开始
}程序段的结束

xuzuning 回复于:2004-06-02 09:43:48


在程序中表示代码块,有$前导时表示数据块
在引号中表示数据块

longnetpro 回复于:2004-06-02 14:30:45
楼上两位答得没错,但楼主并不是这个意思,因此有些答非所问。

{1}或{mod}表示字符串下标。

比如$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法,我写的好几个类中都有这种表示方法。

geel 回复于:2004-06-02 14:54:15
综合一下楼上各位
1、
{} 表示程序块的开始和结束,例如
if ($x==$y)
{
 do_nothing();
}

2、
{}用来表示字符串下标,例如
(引用longnetpro兄弟的话)
$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法

3、
分离变量,例如
$s = "Di, ";
echo ("${s}omething");
//Output: Di, omething
而如果用echo ("$something");
那么就会输出 $something 这个变量。

numlock 回复于:2004-06-03 07:59:39
对于任何更复杂的情况,应该使用复杂语法。

复杂(花括号)语法
不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。

事实上,用此语法你可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用 { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 { 后面时被识别(用“{\$”或者“\{$”来得到一个字面上的“{$”)。用一些例子可以更清晰:

numlock 回复于:2004-06-03 08:00:20
[code:1:7efebf26a1]// Let's show all errors
error_reporting(E_ALL);

$great = 'fantastic';

// 不行,输出为:This is { fantastic}
echo "This is { $great}";

// 可以,输出为:This is fantastic
echo "This is {$great}";
echo "This is ${great}";

// Works
echo "This square is {$square->width}00 centimeters broad.";

// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong
// outside a string. In otherwords, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " . $arr['foo'][3];

echo "You can even write {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";
?>

标签: