天天看點

c#完美實作驗證碼功能 - HackerVirus

c#完美實作驗證碼功能

code

函數(CheckCode.ASpx) 

添加引用: 

using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Drawing2D; 
using System.Text; 

/// <summary> 
/// 建立驗證碼,傳回驗證碼字元串 
/// </summary> 
/// <param name="CodeLen">int CodeLe:驗證碼長度</param> 
/// <param name="CodeType">int CodeType:0純數字;1純字母;2數字與字母混合;3純漢字</param> 
/// <returns></returns> 
/// 
private string CreateCheckCode(int CodeLen, int CodeType) 
{ 
/**參數說明:CodeLen(驗證碼長度);CodeType(0純數字,1純字母,2,數字與字母混合,3,純漢字)**/ 
if (CodeType > 3) 
{ 
CodeType = 3; 
} 
string codestring = ""; 
//定義驗證圖檔的長度與寬度 
int Clen=30*CodeLen, Cheight=40; 
//定義驗證圖檔的背景顔色 
Color CBcolor = Color .FromArgb(200,200,200); 
string[] font = { "Bell MT", "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體","幼圓", "楷體_GB2312","仿宋_GB2312" }; 
Font Cfont = new Font("Arial", 20, FontStyle.Bold); 
//産生随機驗證碼--------------------------------------------------------------------------------------------------- 
switch (CodeType) 
{ 
//純數字 
case 0: 
Random random1 = new Random(); 
codestring = random1.Next((int)System.Math.Pow(10,CodeLen-1),(int) System.Math.Pow(10,CodeLen)-1).ToString(); 
break; 
//純數字 
//純字母 
case 1: 
string Vchar1 = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; 
String[] VcArray1 = Vchar1.Split(\',\'); 
int vclen1 = VcArray1.Length; 
int vcindex1; 
Random random2 = new Random(); 
for (int i = 0; i < CodeLen; i++) 
{ 
vcindex1 = random2.Next(vclen1); 
codestring = codestring + VcArray1[vcindex1]; 
} 
break; 
case 2: 
string Vchar2 = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; 
String[] VcArray2 = Vchar2.Split(\',\'); 
int vclen2 = VcArray2.Length; 
int vcindex2; 
Random random3 = new Random(); 
for (int i = 0; i < CodeLen; i++) 
{ 
vcindex2 = random3.Next(vclen2); 
codestring = codestring + VcArray2[vcindex2]; 
} 
break; 
//純字母 
//純漢字 
case 3: 
//定義一個字元串數組儲存漢字編碼的組成元素 
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 

Random rnd = new Random(); 
int strlength = CodeLen*2; 
//定義一個object數組用來 
object[] bytes = new object[strlength]; 

/**/ 
/*每循環一次産生一個含兩個元素的十六進制位元組數組,并将其放入bject數組中 
每個漢字有四個區位碼組成 
區位碼第1位和區位碼第2位作為位元組數組第一個元素 
區位碼第3位和區位碼第4位作為位元組數組第二個元素 
*/ 
for (int i = 0; i < strlength; i++) 
{ 
//區位碼第1位 
int r1 = rnd.Next(11, 14); 
string str_r1 = rBase[r1].Trim(); 

//區位碼第2位 
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更換随機數發生器的種子避免産生重複值 
int r2; 
if (r1 == 13) 
{ 
r2 = rnd.Next(0, 7); 
} 
else 
{ 
r2 = rnd.Next(0, 16); 
} 
string str_r2 = rBase[r2].Trim(); 

//區位碼第3位 
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i); 
int r3 = rnd.Next(10, 16); 
string str_r3 = rBase[r3].Trim(); 

//區位碼第4位 
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); 
int r4; 
if (r3 == 10) 
{ 
r4 = rnd.Next(1, 16); 
} 
else if (r3 == 15) 
{ 
r4 = rnd.Next(0, 15); 
} 
else 
{ 
r4 = rnd.Next(0, 16); 
} 
string str_r4 = rBase[r4].Trim(); 

//定義兩個位元組變量存儲産生的随機漢字區位碼 
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16); 
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16); 
//将兩個位元組變量存儲在位元組數組中 
byte[] str_r = new byte[] { byte1, byte2 }; 

//将産生的一個漢字的位元組數組放入object數組中 
bytes.SetValue(str_r, i); 

} 
Encoding gb = Encoding.GetEncoding("gb2312"); 
//根據漢字編碼的位元組數組解碼出中文漢字 
for(int i=0;i<strlength/2;i++) 
{ 
codestring =codestring+ gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); 
} 
break; 
//純漢字 
} 

//顯示驗證碼----------------------------------------------------------------------------------------------------- 
//建立一個圖像 
//定義畫筆,用于繪制文字 
Brush brush1 = new SolidBrush(Color.Black); 
Bitmap bitmap1 = new System.Drawing.Bitmap(Clen,Cheight); 
//從圖像擷取一個繪畫面 
Graphics graphics1 = Graphics.FromImage(bitmap1); 
//清除整個繪圖畫面并用顔色填充 
graphics1.Clear(CBcolor); 
PointF Cpoint1=new PointF(5,5); 
Random rnd1 = new Random(); 
int x1 = 0, y1 = 0; 
//繪制邊框 
graphics1.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap1.Width - 1, bitmap1.Height - 1); 

//繪制文字 
for (int i = 0; i < codestring.Length; i++) 
{ 
//随機字元位置 
x1 = rnd1.Next(10)+25*i; 
y1 = rnd1.Next(bitmap1.Height/4); 
Cpoint1 = new PointF(x1, y1); 
//随機字元顔色,應根據背景作适當調整以免顯示模糊不清 
brush1 = new SolidBrush(Color.FromArgb(rnd1.Next(100), rnd1.Next(100), rnd1.Next(100))); 
Cfont=new Font(font[rnd1.Next(font.Length-1)],16,FontStyle.Bold); 
//随機傾斜字元 
graphics1.TranslateTransform(10, 0); 
Matrix transform = graphics1.Transform; 
transform.Shear(Convert.ToSingle( rnd1.NextDouble()-0.5),0.001f); 
graphics1.Transform = transform; 
graphics1.DrawString(codestring.Substring(i,1) , Cfont, brush1, Cpoint1); 
graphics1.ResetTransform(); 

} 
//繪制幹擾點 
rnd1 = new Random(); 
for (int i = 0; i < 50*CodeLen; i++) 
{ 
//随機幹擾位置 
x1 = rnd1.Next(bitmap1.Width); 
y1 = rnd1.Next(bitmap1.Height); 
bitmap1.SetPixel(x1, y1, Color.FromArgb(rnd1.Next(50), rnd1.Next(50), rnd1.Next(50))); 
} 
//繪制幹擾線 
rnd1 = new Random(); 
for (int i = 0; i < 10; i++) 
{ 
//随機幹擾位置 
x1 = rnd1.Next(bitmap1.Width); 
y1 = rnd1.Next(bitmap1.Height); 
int x2 = rnd1.Next(bitmap1.Width); 
int y2 = rnd1.Next(bitmap1.Height); 
//随機幹擾線顔色 
Pen pen1 = new Pen(Color.FromArgb(rnd1.Next(100, 255), rnd1.Next(100, 255), rnd1.Next(100, 255))); 
graphics1.DrawLine(pen1, x1, y1, x2, y2); 
} 
bitmap1.Save(Response.OutputStream, ImageFormat.Jpeg); 
bitmap1.Dispose(); 
graphics1.Dispose(); 
return codestring; 
} 

二、調用(CheckCode.ASpx) 

protected void Page_Load(object sender, EventArgs e) 
{ 
int clen = 4,ctype = 0;; 
try 
{ 
clen = Convert.ToInt16(Request.QueryString["Len"].ToString().Trim()); 
ctype = Convert.ToInt16(Request.QueryString["Type"].ToString().Trim()); 
} 
catch 
{ } 
string checkcodestr=CreateCheckCode(clen,ctype); 
//對checkcodestr進行加密 
Session["CheckStr"] = checkcodestr; 
} 
三、應用(Login.aspx) 

在頁面添加Image控件,将Image控件的ImageUrl屬性設定為"CheckCode.aspx?len=4&type=3"
      

posted on

2010-05-17 17:49 

HackerVirus 

閱讀(721) 

評論(0) 

編輯 

收藏 

舉報