风中小郎君

导航

验证码生成原理的解剖

目前,许多网站的会员登录时都要求输入验证码,尽管验证码的形式五花八门,但是所使用的原理基本是一样的,都是生成随机字符串,然后描绘成图片的形式输出。

     验证码的生产主要分两部分:1是随机字符串的生成;2是生产验证码图片

1   随机字符串的生成:

     随机字符串生成有很多方法,这里介绍一种利用字符数组生产随机串的方法

     代码如图1所示,自定义自己的随机字符数组,然后使用随机函数随机抽取4个字符组成一个随机字符串

 

/// <summary>
   /// 生成随机字符串
   /// </summary>
   /// <returns></returns>
   private string GenerateCheckCode()
   {
       //  PageUtils.VerifyCode = checkCode.ToLower();
 
 
       //自定义自己验证码需要显示的字符
       //如果仅仅是数字和26个字母,您可以写一个循环遍历
       char[] myCodeChar = { '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd', 'e', 'f' };
       int number;
       char code;
       string checkCode = string.Empty;
 
 
       //使用随机函数,产生一个随机数
       System.Random random = new Random();
       for (int i = 0; i < 4; i++)
       {
           number = random.Next(myCodeChar.Length);
 
           code = (char)(myCodeChar[number]);
 
           checkCode += code.ToString();
       }
 
       Session["checkCode"] = checkCode.ToLower();//把生成的验证码存入session
       return checkCode;
   }

                                                           图1

2 生产验证码图片,其实就是用c#的Graphics把生成德随机串描绘到图片上,具体请看图2代码示意:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// <summary>
   /// 生成验证码图片,并输出
   /// </summary>
   /// <param name="checkCode"></param>
   private void CreateCheckCodeImage(string checkCode)
   {
       if (checkCode == null || checkCode.Trim() == String.Empty) return;
 
       Matrix m = new Matrix();//定义几何变换
       Bitmap charbmp = new Bitmap(90, 30);//图片前景色,即生成背景透明的随机字符串图片
 
       //定义字体
       Font[] fonts = {
                                       new Font(new FontFamily("Times New Roman"), 17, FontStyle.Regular),
                                       new Font(new FontFamily("Georgia"), 17, FontStyle.Regular),
                                       new Font(new FontFamily("Arial"), 17, FontStyle.Regular),
                                       new Font(new FontFamily("Comic Sans MS"), 17, FontStyle.Regular)
                                    };
 
        
       //定义图片背景色
       System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 22.5)), 30);
 
       //开始描绘
       Graphics g = Graphics.FromImage(image);
 
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
 
       //定义背景色为白色
       g.Clear(Color.White);
       try
       {
           Random random = new Random();       //生成随机生成器
           g.Clear(Color.White);               //清空图片背景色
           for (int i = 0; i < 2; i++)              //画图片的背景噪音线,i表示画多少条噪音线
           {
               int x1 = random.Next(image.Width);
               int x2 = random.Next(image.Width);
               int y1 = random.Next(image.Height);
               int y2 = random.Next(image.Height);
               g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
           }
 
           
 
           //开始描绘前景图
           Graphics charg = Graphics.FromImage(charbmp);
 
           SolidBrush drawBrush = new SolidBrush(Color.FromArgb(random.Next(101), random.Next(101), random.Next(101)));
           float charx = -18;
 
           //把随机字符串,逐个写入前景图
           for (int i = 0; i < checkCode.Length; i++)
           {
               m.Reset();
               m.RotateAt(random.Next(31) - 25, new PointF(random.Next(4) + 7, random.Next(4) + 7));
 
               charg.Clear(Color.Transparent);//定义前景图为透明
               charg.Transform = m;
               //定义前景色为黑色
               drawBrush.Color = Color.Black;
 
               charx = charx + 20 + random.Next(3);
               PointF drawPoint = new PointF(charx, 0.1F);
               charg.DrawString(checkCode[i].ToString(), fonts[random.Next(fonts.Length)], drawBrush, new PointF(0, 0));//通过特定的几何变换,旋转或变形随机字符,写入前景图
 
               charg.ResetTransform();
 
               g.DrawImage(charbmp, drawPoint);
           }
 
 
           //画图片的前景噪音点
           for (int i = 0; i < 25; i++)
           {
               int x = random.Next(image.Width);
               int y = random.Next(image.Height);
 
               image.SetPixel(x, y, Color.FromArgb(random.Next()));
           }
 
           //画图片的边框线
           g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
 
           //输出
           System.IO.MemoryStream ms = new System.IO.MemoryStream();
           image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
           Response.ClearContent();
           Response.ContentType = "image/Gif";
           Response.BinaryWrite(ms.ToArray());
       }
       finally
       {
           g.Dispose();
           image.Dispose();
       }
   }

posted on 2015-05-05 12:42  风中小郎君  阅读(393)  评论(0)    收藏  举报