~~~~~~Aresmeng~~~~~~~~~~~~~~~~~~~~

导航

ASP.NET校验码的制着(汉字/字母数字校验码)

闲着没事,在网上看了几个校验码例子,自己做了一下,还可以,现在综合了一下写了下来:
[参照文献(如有侵权,请发邮件以便删除aresmeng@126.com):
1, 用C#生成随机中文汉字验证码的基本原理
2,FW:网上几种常见校验码图片分析]
汉字校验码:
一,建立一个web窗体Check.aspx
页面加载代码如下:
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            this.CreateCheckImage(GenerateCheckstring());
        }
加如下方法随机生成汉字:
    private string GenerateCheckstring()
        
{
            Encoding e
=Encoding.GetEncoding("gb2312");
            
object[] yyy=GetObjects(4);
            
string firstString=e.GetString((byte[])Convert.ChangeType(yyy[0],typeof(byte[])));
            
string secondString=e.GetString((byte[])Convert.ChangeType(yyy[1],typeof(byte[])));
            
string thridString=e.GetString((byte[])Convert.ChangeType(yyy[2],typeof(byte[])));
            
string fourthString=e.GetString((byte[])Convert.ChangeType(yyy[3],typeof(byte[])));
        
            
string CheckSession=firstString+secondString+thridString+fourthString;
            Session[
"CheckSession"]=CheckSession;

            
return Session["CheckSession"].ToString();
        }


        
        
private object[] GetObjects(int i)
        
{
            
//每个汉字都由十六进制组成 如[b2,b3] 二个byte构成一个汉字
            string[] strings=new string[16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
            
object[] returnbyte=new object[i];
            
//Random随机数
            Random rd=new Random();
            
//循环产生i个汉字的byte数组
            for(int j=0;j<i;j++)
            
{   //第一位十六进制的数
                int byteFirst=rd.Next(11,14);//随机产生第一位byte的数对应的int
                string stringFirstByte=strings[byteFirst].Trim();//取出第一位的那个十六进制数
                
//第二位十六进制的数
                rd=new Random(byteFirst*unchecked((int)DateTime.Now.Ticks)+j);//重新生成随机种字,防止重复
                int byteCecond;
                
if(byteFirst == 13)//第一位是D的话,第二位区位码就不能是7以后的十六进制数
                {
                    byteCecond
=rd.Next(0,7);
                }

                
else
                    byteCecond
=rd.Next(0,16);
                
string stringCecondByte=strings[byteCecond].Trim();
        
//第三位十六进制的数
                rd=new Random(byteCecond*unchecked((int)DateTime.Now.Ticks)+j);
                
int byteThrid=rd.Next(10,16);
                
string stringThridByte=strings[byteThrid].Trim();
        
//第四位十六进制的数
                rd=new Random(byteThrid*unchecked((int)DateTime.Now.Ticks)+j);
                
int byteFourth;
                
if(byteThrid == 10)//第3位如果是A的话,第4位就不能是0;第3位如果是F的话,第4位就不能是F
                {
                    byteFourth
=rd.Next(1,16);
                }

                
else if(byteThrid == 15)
                
{
                    byteFourth
=rd.Next(0,15);
                }

                
else
                
{
                    byteFourth
=rd.Next(0,16);
                }

                
string stringFourthByte=strings[byteFourth].Trim();

                
byte firstSection=Convert.ToByte(stringFirstByte+stringCecondByte,16);
                
byte cecondSection=Convert.ToByte(stringThridByte+stringFourthByte,16);
                
byte[] bytes=new byte[]{firstSection,cecondSection};
                
                returnbyte.SetValue(bytes,j);

            }

            
return returnbyte;
        }

加如下方法生成干扰背景:
private void CreateCheckImage(string checkString)
        
{
            
//判断Session
            if(checkString == null || checkString == String.Empty)
                
return;

            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap((int)Math.Ceiling((checkString.Length * 23)), 25);
            Graphics g 
= Graphics.FromImage(image);

            
try
            
{
                
//生成随机生成器
                Random random = new Random();

                
//清空图片背景色
                g.Clear(Color.White);

                
//画图片的背景噪音线
                for(int i=0; i<25; 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.Silver), x1, y1, x2, y2);
                }


                Font font 
= new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush 
= new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
                g.DrawString(checkString, font, brush, 
22);

                
//画图片的前景噪音点
                for(int i=0; i<100; 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), 00, 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();
            }

        }
二,在需要页面的html里用<img src="Check.aspx"/>
字母数字校验码
            int number;
            char code;
            string CheckSession = String.Empty;

            System.Random random = new Random();

            for(int i=0; i<5; i++)
            {
                number = random.Next();

                if(number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));

                CheckSession += code.ToString();
            }

posted on 2007-02-09 14:08  Aresmeng  阅读(400)  评论(0)    收藏  举报