【转】ASP.NET中文验证码详解
生活随笔
收集整理的這篇文章主要介紹了
【转】ASP.NET中文验证码详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
漢字驗證碼技術:它比字母跟數字混合技術更先進。主要用過生成漢字的區位碼將其轉換為漢字,區位碼是漢字一一對應的編碼。用4為數字表示,前面兩位從01到94成為區碼,同理后面兩位成為位碼。
CheckCode.aspx.cs:?
??1public?partial?class?_Default?:?System.Web.UI.Page?
??2{?
??3protected?void?Page_Load(object?sender,?EventArgs?e)?
??4{?
??5GraphicsImage(4);?//調用方法生成四位漢字驗證碼?
??6}?
??7
??8private?object[]?CreateString(int?strlength)?
??9{?
?10//定義一個數組存儲漢字編碼的組成元素?
?11string[]?str?=?new?string[16]?{?"0",?"1",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"a",?"b",?"c",?"d",?
?12"e",?"f"?};?
?13
?14Random?ran?=?new?Random();?//定義一個隨機數對象?
?15object[]?bytes?=?new?object[strlength];?
?16for?(int?i?=?0;?i?<?strlength;?i++)?
?17{?
?18//獲取區位碼第一位?
?19int?ran1?=?ran.Next(11,?14);?
?20string?str1?=?str[ran1].Trim();?
?21
?22//獲取區位碼第二位并防止數據重復?
?23ran?=?new?Random(ran1?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?24int?ran2;?
?25if?(ran1?==?13)?
?26{?
?27ran2?=?ran.Next(0,?7);?
?28}?
?29else?
?30{?
?31ran2?=?ran.Next(0,?16);?
?32}?
?33string?str2?=?str[ran2].Trim();?
?34
?35//獲取區位碼第三位?
?36ran?=?new?Random(ran2?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?37int?ran3?=?ran.Next(10,?16);?
?38string?str3?=?str[ran3].Trim();?
?39
?40//獲取區位碼第四位?
?41ran?=?new?Random(ran3?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?42int?ran4;?
?43if?(ran3?==?10)?
?44{?
?45ran4?=?ran.Next(1,?16);?
?46}?
?47else?if?(ran3?==?15)?
?48{?
?49ran4?=?ran.Next(0,?15);?
?50}?
?51else?
?52{?
?53ran4?=?ran.Next(0,?16);?
?54}?
?55string?str4?=?str[ran4].Trim();?
?56
?57//定義字節變量存儲產生的隨機漢字區位碼?
?58byte?byte1?=?Convert.ToByte(str1?+?str2,?16);?
?59byte?byte2?=?Convert.ToByte(str3?+?str4,?16);?
?60
?61byte[]?stradd?=?new?byte[]?{?byte1,byte2};?
?62//將產生的漢字字節放入數組?
?63bytes.SetValue(stradd,?i);?
?64}?
?65return?bytes;?
?66}?
?67
?68private?string?GetString(int?length)?
?69{?
?70Encoding?gb?=?Encoding.GetEncoding("gb2312");?
?71object[]?bytes?=?CreateString(length);?
?72
?73//根據漢字字節解碼出中文漢字?
?74string?str1?=?gb.GetString((byte[])Convert.ChangeType(bytes[0],?typeof(byte[])));?
?75string?str2?=?gb.GetString((byte[])Convert.ChangeType(bytes[1],?typeof(byte[])));?
?76string?str3?=?gb.GetString((byte[])Convert.ChangeType(bytes[2],?typeof(byte[])));?
?77string?str4?=?gb.GetString((byte[])Convert.ChangeType(bytes[3],?typeof(byte[])));?
?78
?79string?str?=?str1?+?str2?+?str3?+?str4;?
?80Response.Cookies.Add(new?HttpCookie("CheckCode",?str));?
?81return?str;?
?82}?
?83
?84private?void?GraphicsImage(int?length)?
?85{?
?86System.Drawing.Bitmap?image?=?new?System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length?*?
?8722.5)),?22);?
?88Graphics?g?=?Graphics.FromImage(image);?//創建畫布?
?89
?90try?
?91{?
?92//生成隨機生成器?
?93Random?random?=?new?Random();?
?94
?95//清空圖片背景色?
?96g.Clear(Color.White);?
?97
?98//畫圖片的背景噪音線?
?99for?(int?i?=?0;?i?<?1;?i++)?
100{?
101int?x1?=?random.Next(image.Width);?
102int?x2?=?random.Next(image.Width);?
103int?y1?=?random.Next(image.Height);?
104int?y2?=?random.Next(image.Height);?
105
106g.DrawLine(new?Pen(Color.Black),?x1,?y1,?x2,?y2);?
107}?
108
109Font?font?=?new?System.Drawing.Font("Couriew?New",?12,?System.Drawing.FontStyle.Bold?);?
110System.Drawing.Drawing2D.LinearGradientBrush?brush?=?new?
111System.Drawing.Drawing2D.LinearGradientBrush?
112(new?Rectangle(0,?0,?image.Width,?image.Height),?Color.Blue,?Color.DarkRed,?1.2f,?true);?
113g.DrawString(GetString(length),?font,?brush,?2,?2);?
114
115//畫圖片的前景噪音點?
116for?(int?i?=?0;?i?<?50;?i++)?
117{?
118int?x?=?random.Next(image.Width);?
119int?y?=?random.Next(image.Height);?
120
121image.SetPixel(x,?y,?Color.FromArgb(random.Next()));?
122}?
123
124//畫圖片的邊框線?
125g.DrawRectangle(new?Pen(Color.Silver),?0,?0,?image.Width?-?1,?image.Height?-?1);?
126System.IO.MemoryStream?ms?=?new?System.IO.MemoryStream();?
127image.Save(ms,?System.Drawing.Imaging.ImageFormat.Gif);?
128Response.ClearContent();?
129Response.ContentType?=?"image/Gif";?
130Response.BinaryWrite(ms.ToArray());?
131}?
132catch?(Exception?ms)?
133{?
134Response.Write(ms.Message);?
135}?
136}?
137}
138 引用文件:
?1????protected?void?Button1_Click(object?sender,?EventArgs?e)
?2????{
?3????????HttpCookie?cookie?=?Request.Cookies["CheckCode"];
?4????????if?(cookie.Value?==?this.TextBox3.Text.Trim())
?5????????{
?6????????????Response.Write("<script>alert('驗證碼正確!')</script>");
?7????????}
?8????????else
?9????????{
10????????????Response.Write("<script>alert('驗證碼錯誤!')</script>");
11????????}
12????}
CheckCode.aspx.cs:?
??1public?partial?class?_Default?:?System.Web.UI.Page?
??2{?
??3protected?void?Page_Load(object?sender,?EventArgs?e)?
??4{?
??5GraphicsImage(4);?//調用方法生成四位漢字驗證碼?
??6}?
??7
??8private?object[]?CreateString(int?strlength)?
??9{?
?10//定義一個數組存儲漢字編碼的組成元素?
?11string[]?str?=?new?string[16]?{?"0",?"1",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"a",?"b",?"c",?"d",?
?12"e",?"f"?};?
?13
?14Random?ran?=?new?Random();?//定義一個隨機數對象?
?15object[]?bytes?=?new?object[strlength];?
?16for?(int?i?=?0;?i?<?strlength;?i++)?
?17{?
?18//獲取區位碼第一位?
?19int?ran1?=?ran.Next(11,?14);?
?20string?str1?=?str[ran1].Trim();?
?21
?22//獲取區位碼第二位并防止數據重復?
?23ran?=?new?Random(ran1?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?24int?ran2;?
?25if?(ran1?==?13)?
?26{?
?27ran2?=?ran.Next(0,?7);?
?28}?
?29else?
?30{?
?31ran2?=?ran.Next(0,?16);?
?32}?
?33string?str2?=?str[ran2].Trim();?
?34
?35//獲取區位碼第三位?
?36ran?=?new?Random(ran2?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?37int?ran3?=?ran.Next(10,?16);?
?38string?str3?=?str[ran3].Trim();?
?39
?40//獲取區位碼第四位?
?41ran?=?new?Random(ran3?*?unchecked((int)DateTime.Now.Ticks)?+?i);?
?42int?ran4;?
?43if?(ran3?==?10)?
?44{?
?45ran4?=?ran.Next(1,?16);?
?46}?
?47else?if?(ran3?==?15)?
?48{?
?49ran4?=?ran.Next(0,?15);?
?50}?
?51else?
?52{?
?53ran4?=?ran.Next(0,?16);?
?54}?
?55string?str4?=?str[ran4].Trim();?
?56
?57//定義字節變量存儲產生的隨機漢字區位碼?
?58byte?byte1?=?Convert.ToByte(str1?+?str2,?16);?
?59byte?byte2?=?Convert.ToByte(str3?+?str4,?16);?
?60
?61byte[]?stradd?=?new?byte[]?{?byte1,byte2};?
?62//將產生的漢字字節放入數組?
?63bytes.SetValue(stradd,?i);?
?64}?
?65return?bytes;?
?66}?
?67
?68private?string?GetString(int?length)?
?69{?
?70Encoding?gb?=?Encoding.GetEncoding("gb2312");?
?71object[]?bytes?=?CreateString(length);?
?72
?73//根據漢字字節解碼出中文漢字?
?74string?str1?=?gb.GetString((byte[])Convert.ChangeType(bytes[0],?typeof(byte[])));?
?75string?str2?=?gb.GetString((byte[])Convert.ChangeType(bytes[1],?typeof(byte[])));?
?76string?str3?=?gb.GetString((byte[])Convert.ChangeType(bytes[2],?typeof(byte[])));?
?77string?str4?=?gb.GetString((byte[])Convert.ChangeType(bytes[3],?typeof(byte[])));?
?78
?79string?str?=?str1?+?str2?+?str3?+?str4;?
?80Response.Cookies.Add(new?HttpCookie("CheckCode",?str));?
?81return?str;?
?82}?
?83
?84private?void?GraphicsImage(int?length)?
?85{?
?86System.Drawing.Bitmap?image?=?new?System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length?*?
?8722.5)),?22);?
?88Graphics?g?=?Graphics.FromImage(image);?//創建畫布?
?89
?90try?
?91{?
?92//生成隨機生成器?
?93Random?random?=?new?Random();?
?94
?95//清空圖片背景色?
?96g.Clear(Color.White);?
?97
?98//畫圖片的背景噪音線?
?99for?(int?i?=?0;?i?<?1;?i++)?
100{?
101int?x1?=?random.Next(image.Width);?
102int?x2?=?random.Next(image.Width);?
103int?y1?=?random.Next(image.Height);?
104int?y2?=?random.Next(image.Height);?
105
106g.DrawLine(new?Pen(Color.Black),?x1,?y1,?x2,?y2);?
107}?
108
109Font?font?=?new?System.Drawing.Font("Couriew?New",?12,?System.Drawing.FontStyle.Bold?);?
110System.Drawing.Drawing2D.LinearGradientBrush?brush?=?new?
111System.Drawing.Drawing2D.LinearGradientBrush?
112(new?Rectangle(0,?0,?image.Width,?image.Height),?Color.Blue,?Color.DarkRed,?1.2f,?true);?
113g.DrawString(GetString(length),?font,?brush,?2,?2);?
114
115//畫圖片的前景噪音點?
116for?(int?i?=?0;?i?<?50;?i++)?
117{?
118int?x?=?random.Next(image.Width);?
119int?y?=?random.Next(image.Height);?
120
121image.SetPixel(x,?y,?Color.FromArgb(random.Next()));?
122}?
123
124//畫圖片的邊框線?
125g.DrawRectangle(new?Pen(Color.Silver),?0,?0,?image.Width?-?1,?image.Height?-?1);?
126System.IO.MemoryStream?ms?=?new?System.IO.MemoryStream();?
127image.Save(ms,?System.Drawing.Imaging.ImageFormat.Gif);?
128Response.ClearContent();?
129Response.ContentType?=?"image/Gif";?
130Response.BinaryWrite(ms.ToArray());?
131}?
132catch?(Exception?ms)?
133{?
134Response.Write(ms.Message);?
135}?
136}?
137}
138 引用文件:
?1????protected?void?Button1_Click(object?sender,?EventArgs?e)
?2????{
?3????????HttpCookie?cookie?=?Request.Cookies["CheckCode"];
?4????????if?(cookie.Value?==?this.TextBox3.Text.Trim())
?5????????{
?6????????????Response.Write("<script>alert('驗證碼正確!')</script>");
?7????????}
?8????????else
?9????????{
10????????????Response.Write("<script>alert('驗證碼錯誤!')</script>");
11????????}
12????}
轉載于:https://www.cnblogs.com/milk3q/archive/2008/01/10/1034161.html
總結
以上是生活随笔為你收集整理的【转】ASP.NET中文验证码详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多個excel文件合并到一個excel文
- 下一篇: SQL注入攻击再度肆虐殃及大量网站