下面代码保存成MD5Crypt.class.php文件
代码如下 | 复制代码 |
<?php class MD5Crypt { /** * Enter description here ... * @param unknown_type $str * @return string */ public final static function mdsha($str) { $code = substr ( md5 ( $str ), 10 ); $code .= substr ( sha1 ( $str ), 0, 28 ); $code .= substr ( md5 ( $str ), 0, 22 ); $code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str ); return self::chkToken () ? $code : null; } /** * Enter description here ... * @param unknown_type $param */ private final static function chkToken() { return true; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $encrypt_key * @return Ambigous <string, boolean> */ private final static function keyED($txt, $encrypt_key) { $encrypt_key = md5 ( $encrypt_key ); $ctr = 0; $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { if ($ctr == strlen ( $encrypt_key )) $ctr = 0; $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ); $ctr ++; } return $tmp; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $key * @return string */ public final static function Encrypt($txt, $key) { srand ( ( double ) microtime () * 1000000 ); $encrypt_key = md5 ( rand ( 0, 32000 ) ); $ctr = 0; $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { if ($ctr == strlen ( $encrypt_key )) $ctr = 0; $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 )); $ctr ++; } $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key ); return self::chkToken () ? $_code : null; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $key * @return Ambigous <string, boolean> */ public final static function Decrypt($txt, $key) { $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key ); $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { $md5 = substr ( $txt, $i, 1 ); $i ++; $tmp .= (substr ( $txt, $i, 1 ) ^ $md5); } return self::chkToken () ? $tmp : null; } /** * Enter description here ... * @var unknown_type */ private static $_key = 'lau'; } ?> |
用法
代码如下 | 复制代码 |
<?php //Code Start define ( 'WORKSPACE', '.' . DIRECTORY_SEPARATOR ); header ( "Content-Type: text/html; charset=utf-8" ); include_once 'Core/Library/MD5Crypt.class.php'; $a = MD5Crypt::Encrypt ( "A", 100 ); echo "EnCode:" . $a, "<br />"; echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 ); ?> |
PHP包括其他任何网络编程语言的安全性,具体表现在本地安全性和远程安全性两个方面,这里我们应该养成如下的几个习惯确保我们的PHP程序本身是安全的。
1、 验证用户输入的任何数据,保证PHP代码的安全
这里有一个技巧就是使用白名单,所谓白名单就是说:我们要求用户的数据应该是这样的,例如我们要求用户的输入是一个数字,我们就只检验这个值是否是一个数字就行了,而不必检验他到底是什么——其实他有可能是个恶意脚本。
对于这个检验我们不能只在客户端的javascript进行,战地认为JS只是为了提高来访用户的体验而产生的,而不是验证的工具。因为任何一个来访的用户都可能会,也有可能无意间就禁用了客户端脚本的执行,从而跳过这层验证。所以我们必须在PHP的服务器端程序上检验这些数据。
2、 保护数据库的安全——对即将运行于数据库的Sql语句进行安全性预处理。
任何时候都要对执行前的Mysql语句,进行mysql_real_escape_string操作——该函数的用法请参考PHP手册。诸多PHP的数据库抽象层例如ADODB都提供了类似的方法。
3、 不要依赖不该依赖的PHP设置——环境有时候不可靠
不依赖,magic_quotes_gpc=On,在程序编制的过程,尽量关闭这个配置选项,任何时候判断这个选项后再对用户输入的数据进行处理。切记——PHP v6 中将会删除这个选项。尽量在合适的时候使用addcslashes 系列函数——请参考手册
4、 验证数据来源,避免远程表单提交
不要使用$_SERVER['HTTP_REFERER']这个超级变量来检查数据的来源地址,一个很小的菜鸟黑客都会利用工具来伪造这个变量的数据,尽可能利用Md5,或者rand等函数来产生一个令牌,验证来源的时候,验证这个令牌是否匹配。
5、 保护会话数据,特别是Cookies
Cookie是保存在用户的计算机上的,保存之后任何用户都有可能出于某种原因更改他,我们必须对敏感数据进行加密处理。Md5、sha1都是个不错的加密方法。
6、 利用htmlentities()预防XSS攻击
对用户可能输入脚本语言的地方的数据进行htmlentities()操,将多数可以产生程序错误的用户输入进行实体化。记住要遵循第一个习惯:在 Web 应用程序的名称、电子邮件地址、电话号码和帐单信息的输入中用白名单中的值验证输入数据。
本生成程序需要调用一些字体库哦,你可以调用你系统自带的一些字体,当然也可以像dedecms一样自己把字体放到一个目录,这样在服务器上也可以使用了。
代码如下 | 复制代码 |
<?php header("Cache-Control: no-cache, must-revalidate"); session_start(); //绘制杂点 //绘制文字 imagepng($image); |
/*
* Auth_code()是验证码函数
* @access public
* @param int $width 表示验证码的长度,默认为80
* @param int $height 表示验证码的高度,默认为20
* @param int $num 表示验证码中数字位数,默认为4
* @param int $line 表示验证码中线的条数,默认为4
* @param int $line 表示验证码中点的个数,默认为150
*/
代码如下 | 复制代码 |
function Auth_code($width = 80,$height = 20,$num = 4,$line = 4,$dot = 150) { $length = floor($width/$num); //floor:取整数部分 length:每段平均长度 |
-主要函数:
imagecreatetruecolor //新建一个黑色底的画板
imagecolorallocate //取色
imagefill //填充画板
imageline //画线
imagesetpixel //画点
imagestring //画字符串(本验证码为数字)
注意:在将数字验证码画上画板时要注意字体越界,所以要相应减去一些范围,详见附件。
-局部刷新验证码:
代码如下 | 复制代码 |
<img" width=100% src="code.php" onclick="javascript:this.src='code.php?id='+Math.random()"> |
代码如下 | 复制代码 |
代码如下: //随机生成一个4位数的数字验证码 //随机绘制两条虚线,起干扰作用 //在画布上随机生成大量黑点,起干扰作用; |
使用方法:
本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码。
在某页面中使用此程序时,可以用以下代码:
代码如下:
.....
请输入验证码:
代码如下 | 复制代码 |
<input type=text name=passcode> <img" width=100% src=showimg.php> ..... |
这样即可显示出验证码图片。到了验证页面,用以下代码:
代码如下:
...
代码如下 | 复制代码 |
$code=$_POST["passcode"]; if( $code == $_SESSION["Checknum"]){ 验证通过 }else{ 验证码错误 } ... |