塔读登陆汉字验证码识别 开源一半汉字一半字符怎么输入?

下次自动登录
现在的位置:
& 综合 & 正文
net 实现三种验证码(汉字验证码,数字验证码,数字+英文验证)附带登陆验证实例
首先,新建createImage.aspx
在CreateImage.aspx.cs中,添加如下:
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.T
public partial class CreateImage : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
string checkCode = CreateCode(4);
Session["CheckCode"] = checkC
CreateImages(checkCode);
思想:将所有数字及字母存储在字符串中,调用Random()函数随机选取子字符串数组中的一个字符,加入
public string CreateCode(int codeLength)
string so = "1,2,3,4,5,6,7,8,9,0,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,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[] strArr=so.Split(',');
string code = "";
Random rand=new Random();
for (int i = 0; i & codeL i++)
code+=strArr[rand.Next(0, strArr.Length)];
Session["CheckCode"] =
private string CreateNumCode(int codeCount)
string allChar = "0,1,2,3,4,5,6,7,8,9";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i & codeC i++)
if (temp != -1)
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
int t = rand.Next(9);
if (temp == t)
return CreateNumCode(codeCount);
randomCode += allCharArray[t];
Session["CheckCode"] = randomC
return randomC
public static object[] CreateChiCode(int strlength)
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();
object[] bytes = new object[strlength];
for (int i = 0; i & i++)
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
if (r1 == 13)
r2 = rnd.Next(0, 7);
r2 = rnd.Next(0, 16);
string str_r2 = rBase[r2].Trim();
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
if (r3 == 10)
r4 = rnd.Next(1, 16);
else if (r3 == 15)
r4 = rnd.Next(0, 15);
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 };
bytes.SetValue(str_r, i);
private string GetString(int length)
object[] obj=CreateChiCode(length);
Encoding gb = Encoding.GetEncoding("gb2312");
String[] bytes= new String[length];
String code = "";
for (int i = 0; i & i++)
bytes[i] = gb.GetString((byte[])Convert.ChangeType(obj[i], typeof(byte[])));
code = code+bytes[i];
Session["CheckCode"] =
public void CreateImages(string code)
Bitmap image = new Bitmap(120, 40);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww=new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("#FAE264"));
Random random = new Random();
for (int i = 0; i & 12; 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.LightGray), x1, y1, x2, y2);
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,1.2f,true);
g.DrawString(code, font, brush, 0, 0);
for (int i = 0; i & 10; i++)
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.White);
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());
g.Dispose();image.Dispose();
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
InitializeComponent();
base.OnInit(e);
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);
#endregion
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.T
public partial class CreateImage : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
// string checkCode=CreateNumCode(4);//生成四位数字验证码
string checkCode = CreateCode(4);//生成四位数字+字母验证
//string checkCode = GetString(4);//生成四位汉字验证码
Session["CheckCode"] = checkC
CreateImages(checkCode);
/*产生数字+字母验证码*/
/*算法思想:将所有数字及字母存储在字符串中,调用Random()函数随机选取子字符串数组中的一个字符,加入
指定的字符串末尾,如此反复,最终返回生成好的验证码*/
public string CreateCode(int codeLength)
string so = "1,2,3,4,5,6,7,8,9,0,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,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[] strArr=so.Split(',');
string code = "";
Random rand=new Random();
for (int i = 0; i & codeL i++)
code+=strArr[rand.Next(0, strArr.Length)];
Session["CheckCode"] =
private string CreateNumCode(int codeCount) //生成纯数字验证码
string allChar = "0,1,2,3,4,5,6,7,8,9";
string[] allCharArray = allChar.Split(',');//将allChar的每个由逗号分隔的元素添加到数组中
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i & codeC i++)
if (temp != -1)
rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); //更换随机数生成器种子避免产生相同随机数
int t = rand.Next(9);//返回一个小于9的非负随机数
if (temp == t) //保证连续两个生成的随机数不同
return CreateNumCode(codeCount);
randomCode += allCharArray[t];
Session["CheckCode"] = randomC
return randomC
此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将
四个字节数组存储在object数组中。
参数:strlength,代表需要产生的汉字个数
public static object[] CreateChiCode(int strlength)
//定义一个字符串数组储存汉字编码的组成元素
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();
//定义一个object数组用来存放随机生成的汉字
object[] bytes = new object[strlength];
/*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
每个汉字有四个区位码组成
区位码第1位和区位码第2位作为字节数组第一个元素
区位码第3位和区位码第4位作为字节数组第二个元素
for (int i = 0; i & i++)
//区位码第1位
int r1 = rnd.Next(11, 14);//返回一个介于11到14的随机数
string str_r1 = rBase[r1].Trim();
//区位码第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
if (r1 == 13)
r2 = rnd.Next(0, 7);
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);
if (r3 == 10)
r4 = rnd.Next(1, 16);
else if (r3 == 15)
r4 = rnd.Next(0, 15);
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);
private string GetString(int length)
//生成指定位数的汉字验证码,并转换为String类型
object[] obj=CreateChiCode(length);
Encoding gb = Encoding.GetEncoding("gb2312");
String[] bytes= new String[length];
String code = "";
for (int i = 0; i & i++)
bytes[i] = gb.GetString((byte[])Convert.ChangeType(obj[i], typeof(byte[])));
// bytes[i] = obj[i].ToString();
code = code+bytes[i];
Session["CheckCode"] =
/*产生验证图片*/
public void CreateImages(string code)
Bitmap image = new Bitmap(120, 40);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww=new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("#FAE264"));
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i & 12; 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.LightGray), x1, y1, x2, y2);
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,1.2f,true);
g.DrawString(code, font, brush, 0, 0);
//画图片的前景噪音点
for (int i = 0; i & 10; i++)
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.White);
//画图片的边框线
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());
g.Dispose();image.Dispose();
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InitializeComponent();
base.OnInit(e);
/// &summary&
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// &/summary&
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);
#endregion
CreateImage.aspx中不需添加任何代码
之后,新建webform1.aspx
在网页中添加如下代码:
%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" %&!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"
name="CODE_LANGUAGE" Content="C#"
name="vs_defaultClientScript" content="JavaScript"
name="vs_targetSchema" content="/intellisense/ie5"
MS_POSITIONING="gridlayout"
id="Form1" method="post" runat="server"
src="CreateImage.aspx" align="middle"
&%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" %&&!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &
&title&WebForm1
&meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"/&
&meta name="CODE_LANGUAGE" Content="C#"/&
&meta name="vs_defaultClientScript" content="JavaScript"/&
&meta name="vs_targetSchema" content="/intellisense/ie5"/&
&body MS_POSITIONING="gridlayout"&
&form id="Form1" method="post" runat="server"&
&img src="CreateImage.aspx" align="middle" &
对应的cs文件中不需添加任何代码,此时即可完成
看看效果吧
附加一个登陆界面应用验证码的实例:
新建login.aspx
login.aspx中
%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %
&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
xmlns="http://www.w3.org/1999/xhtml"
runat="server"
id="form1" runat="server"
id="login"
cellpadding="0" cellspacing="0" border="0" style="width: 400px"
ID="TextBox1" runat="server"
ID="TextBox2" runat="server"
ID="TextBox3" runat="server" Width="120px"
src="CreateImage.aspx" alt="点击刷新" id="imgValidateCode" style="width:100 height:40line-height:30vertical-align:"
colspan="2"
style="margin-left: 42px"
ID="Button1" runat="server" onclick="Button1_Click1" Text="登陆"
ID="Button2" runat="server" onclick="Button2_Click1" Text="重置"
&%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %&
&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&
&html xmlns="http://www.w3.org/1999/xhtml"&
&head runat="server"&
&title&&/title&
&form id="form1" runat="server"&
&div id="login"&
&table cellpadding="0" cellspacing="0" border="0" style="width: 400px"&
&span&账号&/span&
&asp:TextBox ID="TextBox1" runat="server"&&/asp:TextBox&
&span&密码&/span&
&asp:TextBox ID="TextBox2" runat="server"&&/asp:TextBox&
&span&验证码&/span&
&asp:TextBox ID="TextBox3" runat="server" Width="120px"&&/asp:TextBox&
&img src="CreateImage.aspx" alt="点击刷新" id="imgValidateCode" style="width:100 height:40line-height:30vertical-align:" /&
&td colspan="2"&
&div style="margin-left: 42px"&
&asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="登陆" /&
&asp:Button ID="Button2" runat="server" onclick="Button2_Click1" Text="重置" /&
对应的cs文件中,
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
public partial class login : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click1(object sender, EventArgs e)
string checkCode = this.TextBox3.Text.Trim();
if (checkCode == "" || checkCode == null)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('请输入验证码!');&/script&");
if (pare(Session["CheckCode"].ToString(), checkCode, true) != 0)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('验证码错误!');&/script&");
else if (pare(Session["CheckCode"].ToString(), checkCode, true) == 0)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('验证成功!');&/script&");
protected void Button2_Click1(object sender, EventArgs e)
this.TextBox1.Text = "";
this.TextBox3.Text = "";
this.TextBox2.Text = "";
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
public partial class login : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click1(object sender, EventArgs e)
string checkCode = this.TextBox3.Text.Trim();
//判断验证码是否为空
if (checkCode == "" || checkCode == null)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('请输入验证码!');&/script&");
//判断验证码输入是否正确
if (pare(Session["CheckCode"].ToString(), checkCode, true) != 0) //compare函数可以选择是否忽略大小写
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('验证码错误!');&/script&");
else if (pare(Session["CheckCode"].ToString(), checkCode, true) == 0)
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "&script&alert('验证成功!');&/script&");
protected void Button2_Click1(object sender, EventArgs e)
this.TextBox1.Text = "";
this.TextBox3.Text = "";
this.TextBox2.Text = "";
&&&&推荐文章:
【上篇】【下篇】输入下面动画图片中最大的字符 这个验证码 怎么填呀 试了N次了 都不对 应该怎么办_百度知道
输入下面动画图片中最大的字符 这个验证码 怎么填呀 试了N次了 都不对 应该怎么办
网页关了 再重试一下就OK了
其他类似问题
为您推荐:
其他3条回答
把你最好看到的字符都输入,这个验证码,我刚开始也折腾好久。
输入验证码时,提示输入最大字符,不是让你之输入一个字母或数字、汉字,而是输入你看到的一排验证字符。 您看到验证码后请输入最明显的字符 杂乱干扰的不计
我也碰到过这样的问题,你把图片中的字符全部填进去!
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁网上注册验证码为汉字提示字符该怎样输入_百度知道
网上注册验证码为汉字提示字符该怎样输入
提问者采纳
直接输入里面的汉字就可以了。
提问者评价
其他类似问题
为您推荐:
汉字的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁qq安全中心验证码昰汉字要输入字符怎么办_百度知道
qq安全中心验证码昰汉字要输入字符怎么办
我有更好的答案
有两种,一种是选择,一种是输入汉字的
其他类似问题
为您推荐:
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁为什么登录qq总是提示帐号存在异常,需输入验证字符?_百度知道
为什么登录qq总是提示帐号存在异常,需输入验证字符?
为什么登录qq总是提示帐号存在异常,需输入验证字符?以前都没的。
提问者采纳
请您用杀毒软件在安全模式下全面查杀您的电脑、也有可能是网络问题造成的缘故,不易被原号码主人发现:
1,请您卸载之前的一切QQ版本,强制删除大量好友的,并查杀木马
1,系统将自动取消:
具体原因: 即并在您不知情的情况下可能被其他人用来发送广告或者诈骗消息。
2,现在我将这些规类以下几个原因,共同营造一个绿色健康的上网环境,正常使用10~20天左右、你的电脑中存在的病毒木马的缘故,这些都是由于病毒造成的缘故。
大家一直在讨论为什么要有这个验证码呢、在一些不安全的场所登陆过Q:通常盗号者会利用帮用户挂机达到盗取用户财产及号码、可能你使用过一些非官方的软件登陆QQ,或者在网站上挂过QQ。导致这样的结果是很明显的,突然检测到登陆地点有变动时,请耐心等待观察时间的通过。
关于用户号码存在双主号被要求输验证码解决方法
因近期发现有大量号码存在双主号(即有两人同时知道号码密码在不同时间不同地点使用),故我们推出了验证码来引起真正号码主人关注。
2,或者骗人的消息,从而被系统检测到,会被继续要求输入验证码?在调查中发现不少Q友都一般有两个Q或者两个Q以上、异地登录 !)在这些之间、 发现有验证码后立即修改密码,网络不稳定;如果在此期间密码再次泄漏、 发送广告消息。希望大家一起来加强号码的防范意识,减少原主人的损失、恶意发送QQ消息、同时也请不要在您登录号码的时间段提供密码让好友在其它地方登录试用您的QQ。
6,被人举报,建议您将这个号码的本地聊天记录清除即可(在QQ安装目录下搜索以该号码命名文件夹进行删除即可,但是并不是他们的这些QQ都需要输入:
4,验证码也就会消失,而只是一个而已,下载最新的QQ官方版本使用。
解决的办法,如存在不安全因素的网吧。但是许多人都不理解这个、 在您被需要验证码后将进入观察期,但需要重新像上面所操作的一下,都是会出现的、号码有挂机行为:即您的号码经常在一个地方登陆,比如你中了病毒发送一些病毒消息。现列举出具体的原因及解决方法。
5,为了防止盗号者使用恶意软件以登录的方式扫描您的密码,亲身加了一些需要输入验证码的Q友。
3,为安全起见会加上验证码
解决方法推出验证码的策略主要是为了保障您的权益,从中得到了一些问题所在,等时间一过。还有就是被举报的QQ号码,比如用代理上。由于感到这类问题的严重性
其他类似问题
为您推荐:
登录qq的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 汉字验证码怎么输入 的文章

 

随机推荐