Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

安全而且好看的验证码

Posted on 2010-05-04 10:28  analyzer  阅读(2476)  评论(0编辑  收藏  举报

转自:http://www.javaeye.com/topic/469170

验证码使用了20种字体,要下载上传的附件下来才能测试,不能直接copy代码就debug。在我的512内存的破Windows机上跑起来,生成一个验证码花的时间在0.0086619853973389-0.00999xxxx之间,在vps上跑是0.00354886054993左右。我是在我的框架控制器中调用来测试的,如果直接测试,速度会更快些。我觉得性能还可以,我测试的代码是:

Php代码 
  1. public function getAction() {  
  2.     $start = microtime(1);  
  3.     YL_Security_Secoder::entry();  
  4.     file_put_contents(dirname(__FILE__) . '/test.php', (microtime(1)-$start));  
  5. }  



给我评评看,这安全度和用户识别麻烦度各有多大呢?

这用php写的代码我自己都觉得有点晕,如果用py或rb写的,可读性该好很多,只是精通了php以后让我再学习用ry/py来做,难度有点大。

   1 <?php

 

  2 /**
  3  * 安全验证码
  4  * 
  5  * 安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码。
  6  * 如果用中文做验证码(我这里不是哦,有兴趣你来改成用中文的),安全度会更好些,但验证码扭曲和旋转是王道,用了字体也算是已经给字体扭曲了,我就不再去给他添一只扭曲的足了。
  7  * 可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了
  8  *
  9  * @author 流水孟春 <cmpan(at)qq.com>
 10  * @copyright NEW BSD
 11  * @link http://labs.yulans.cn/YL_Security_Secoder
 12  * @link http://wiki.yulans.cn/docs/yl/security/secoder
 13  */
 14 class YL_Security_Secoder {
 15     /**
 16      * 验证码的session的下标
 17      * 
 18      * @var string
 19      */
 20     public static $seKey = 'sid.sekey.ylans.cn';
 21     public static $expire = 3000;     // 验证码过期时间(s)
 22     /**
 23      * 验证码中使用的字符,01IO容易混淆,建议不用
 24      *
 25      * @var string
 26      */
 27     public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY';
 28     public static $fontSize = 25;     // 验证码字体大小(px)
 29     public static $useCurve = true;   // 是否画混淆曲线
 30     public static $useNoise = true;   // 是否添加杂点    
 31     public static $imageH = 0;        // 验证码图片宽
 32     public static $imageL = 0;        // 验证码图片长
 33     public static $length = 4;        // 验证码位数
 34     public static $bg = array(243, 251, 254);  // 背景
 35     
 36     protected static $_image = null;     // 验证码图片实例
 37     protected static $_color = null;     // 验证码字体颜色
 38     
 39     /**
 40      * 输出验证码并把验证码的值保存的session中
 41      * 验证码保存到session的格式为: $_SESSION[self::$seKey] = array('code' => '验证码值', 'time' => '验证码创建时间');
 42      */
 43     public static function entry() {
 44         // 图片宽(px)
 45         self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5
 46         // 图片高(px)
 47         self::$imageH || self::$imageH = self::$fontSize * 2;
 48         // 建立一幅 self::$imageL x self::$imageH 的图像
 49         self::$_image = imagecreate(self::$imageL, self::$imageH); 
 50         // 设置背景      
 51         imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); 
 52         // 验证码字体随机颜色
 53         self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));
 54         // 验证码使用随机字体 
 55         $ttf = dirname(__FILE__. '/ttfs/' . mt_rand(1, 20. '.ttf';  
 56         
 57         if (self::$useNoise) {
 58             // 绘杂点
 59             self::_writeNoise();
 60         } 
 61         if (self::$useCurve) {
 62             // 绘干扰线
 63             self::_writeCurve();
 64         }
 65         
 66         // 绘验证码
 67         $code = array(); // 验证码
 68         $codeNX = 0// 验证码第N个字符的左边距
 69         for ($i = 0$i<self::$length$i++) {
 70             $code[$i= self::$codeSet[mt_rand(0, 27)];
 71 //改成中文验证码
 72 //$code[$i] = chr(mt_rand(0xB0,0xF7)).chr(mt_rand(0xA1,0xFE));   
 73             $codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6);
 74             // 写一个验证码字符
 75             imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]);
 76         }
 77         
 78         // 保存验证码
 79         isset($_SESSION|| session_start();
 80         $_SESSION[self::$seKey]['code'= join('', $code); // 把校验码保存到session
 81         $_SESSION[self::$seKey]['time'= time();  // 验证码创建时间
 82                 
 83         header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
 84         header('Cache-Control: post-check=0, pre-check=0', false);        
 85         header('Pragma: no-cache');        
 86         header("content-type: image/png");
 87     
 88         // 输出图像
 89         imagepng(self::$_image); 
 90         imagedestroy(self::$_image);
 91     }
 92     
 93     /*
 94      * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数) 
 95      *      
 96      *      高中的数学公式咋都忘了涅,写出来
 97      *        正弦型函数解析式:y=Asin(ωx+φ)+b
 98      *      各常数值对函数图像的影响:
 99      *        A:决定峰值(即纵向拉伸压缩的倍数)
100      *        b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
101      *        φ:决定波形与X轴位置关系或横向移动距离(左加右减)
102      *        ω:决定周期(最小正周期T=2π/∣ω∣)
103      *
104      */
105     protected static function _writeCurve() {
106         $A = mt_rand(1, self::$imageH/2);                  // 振幅
107         $b = mt_rand(-self::$imageH/4, self::$imageH/4);   // Y轴方向偏移量
108         $f = mt_rand(-self::$imageH/4, self::$imageH/4);   // X轴方向偏移量
109         $T = mt_rand(self::$imageH*1.5, self::$imageL*2);  // 周期
110         $w = (2* M_PI)/$T;
111                         
112         $px1 = 0;  // 曲线横坐标起始位置
113         $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667);  // 曲线横坐标结束位置             
114         for ($px=$px1$px<=$px2$px=$px+ 0.9) {
115             if ($w!=0) {
116                 $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2;  // y = Asin(ωx+φ) + b
117                 $i = (int) ((self::$fontSize - 6)/4);
118                 while ($i > 0) {    
119                     imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color);  // 这里画像素点比imagettftext和imagestring性能要好很多                    
120                     $i--;
121                 }
122             }
123         }
124         
125         $A = mt_rand(1, self::$imageH/2);                  // 振幅        
126         $f = mt_rand(-self::$imageH/4, self::$imageH/4);   // X轴方向偏移量
127         $T = mt_rand(self::$imageH*1.5, self::$imageL*2);  // 周期
128         $w = (2* M_PI)/$T;        
129         $b = $py - $A * sin($w*$px + $f- self::$imageH/2;
130         $px1 = $px2;
131         $px2 = self::$imageL;
132         for ($px=$px1$px<=$px2$px=$px+ 0.9) {
133             if ($w!=0) {
134                 $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2;  // y = Asin(ωx+φ) + b
135                 $i = (int) ((self::$fontSize - 8)/4);
136                 while ($i > 0) {            
137                     imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color);  // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多    
138                     $i--;
139                 }
140             }
141         }
142     }
143     
144     /**
145      * 画杂点
146      * 往图片上写不同颜色的字母或数字
147      */
148     protected static function _writeNoise() {
149         for($i = 0$i < 10$i++){
150             //杂点颜色
151             $noiseColor = imagecolorallocate(
152                               self::$_image, 
153                               mt_rand(150,225), 
154                               mt_rand(150,225), 
155                               mt_rand(150,225)
156                           );
157             for($j = 0$j < 5$j++) {
158                 // 绘杂点
159                 imagestring(
160                     self::$_image,
161                     5, 
162                     mt_rand(-10, self::$imageL), 
163                     mt_rand(-10, self::$imageH), 
164                     self::$codeSet[mt_rand(0, 27)], // 杂点文本为随机的字母或数字
165                     $noiseColor
166                 );
167             }
168         }
169     }
170     
171     /**
172      * 验证验证码是否正确
173      *
174      * @param string $code 用户验证码
175      * @return bool 用户验证码是否正确
176      */
177     public static function check($code) {
178         isset($_SESSION|| session_start();
179         // 验证码不能为空
180         if(empty($code|| empty($_SESSION[self::$seKey])) {
181             return false;
182         }
183         // session 过期
184         if(time() - $_SESSION[self::$seKey]['time'> self::$expire) {
185             unset($_SESSION[self::$seKey]);
186             return false;
187         }
188 
189         if($code == $_SESSION[self::$seKey]['code']) {
190             return true;
191         }
192 
193         return false;
194     }
195 }
196 
197 
198 // useage
199 /*
200 YL_Security_Secoder::$useNoise = false;  // 要更安全的话改成true
201 YL_Security_Secoder::$useCurve = true;
202 YL_Security_Secoder::entry();
203 */
204 
205 /*
206 // 验证验证码
207 if (!YL_Security_Secoder::check(@$_POST['secode'])) {
208     print 'error secode';
209 }
210 */
211 

 

大张的图不清楚,就再加些直接保存下来的验证码吧。有兴趣多刷几张图看看的话,刷这里http://www.yulans.cn/dp/secode/get 别把我的vps刷死就行
















 

 

我要啦免费统计