汉字验证码技术:它比字母跟数字混合技术更先进。主要用过生成汉字的区位码将其转换为汉字,区位码是汉字一一对应的编码。用4为数字表示,前面两位从01到94成为区码,同理后面两位成为位码。懒得打字就贴出出要的程序,如有问题请联系我QQ:1264373.
CheckCode.aspx.cs:
CheckCode.aspx.cs:
1
public partial class _Default : System.Web.UI.Page
2

{
3
protected void Page_Load(object sender, EventArgs e)
4

{
5
GraphicsImage(4); //调用方法生成四位汉字验证码
6
}
7
8
private object[] CreateString(int strlength)
9

{
10
//定义一个数组存储汉字编码的组成元素
11
string[] str = new string[16]
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
12
"e", "f" };
13
14
Random ran = new Random(); //定义一个随机数对象
15
object[] bytes = new object[strlength];
16
for (int i = 0; i < strlength; i++)
17

{
18
//获取区位码第一位
19
int ran1 = ran.Next(11, 14);
20
string str1 = str[ran1].Trim();
21
22
//获取区位码第二位并防止数据重复
23
ran = new Random(ran1 * unchecked((int)DateTime.Now.Ticks) + i);
24
int ran2;
25
if (ran1 == 13)
26

{
27
ran2 = ran.Next(0, 7);
28
}
29
else
30

{
31
ran2 = ran.Next(0, 16);
32
}
33
string str2 = str[ran2].Trim();
34
35
//获取区位码第三位
36
ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i);
37
int ran3 = ran.Next(10, 16);
38
string str3 = str[ran3].Trim();
39
40
//获取区位码第四位
41
ran = new Random(ran3 * unchecked((int)DateTime.Now.Ticks) + i);
42
int ran4;
43
if (ran3 == 10)
44

{
45
ran4 = ran.Next(1, 16);
46
}
47
else if (ran3 == 15)
48

{
49
ran4 = ran.Next(0, 15);
50
}
51
else
52

{
53
ran4 = ran.Next(0, 16);
54
}
55
string str4 = str[ran4].Trim();
56
57
//定义字节变量存储产生的随机汉字区位码
58
byte byte1 = Convert.ToByte(str1 + str2, 16);
59
byte byte2 = Convert.ToByte(str3 + str4, 16);
60
61
byte[] stradd = new byte[]
{ byte1,byte2};
62
//将产生的汉字字节放入数组
63
bytes.SetValue(stradd, i);
64
}
65
return bytes;
66
}
67
68
private string GetString(int length)
69

{
70
Encoding gb = Encoding.GetEncoding("gb2312");
71
object[] bytes = CreateString(length);
72
73
//根据汉字字节解码出中文汉字
74
string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
75
string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
76
string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
77
string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
78
79
string str = str1 + str2 + str3 + str4;
80
Response.Cookies.Add(new HttpCookie("CheckCode", str));
81
return str;
82
}
83
84
private void GraphicsImage(int length)
85

{
86
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((GetString(length).Length *
87
22.5)), 22);
88
Graphics g = Graphics.FromImage(image); //创建画布
89
90
try
91

{
92
//生成随机生成器
93
Random random = new Random();
94
95
//清空图片背景色
96
g.Clear(Color.White);
97
98
//画图片的背景噪音线
99
for (int i = 0; i < 1; i++)
100

{
101
int x1 = random.Next(image.Width);
102
int x2 = random.Next(image.Width);
103
int y1 = random.Next(image.Height);
104
int y2 = random.Next(image.Height);
105
106
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
107
}
108
109
Font font = new System.Drawing.Font("Couriew New", 12, System.Drawing.FontStyle.Bold );
110
System.Drawing.Drawing2D.LinearGradientBrush brush = new
111
System.Drawing.Drawing2D.LinearGradientBrush
112
(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
113
g.DrawString(GetString(length), font, brush, 2, 2);
114
115
//画图片的前景噪音点
116
for (int i = 0; i < 50; i++)
117

{
118
int x = random.Next(image.Width);
119
int y = random.Next(image.Height);
120
121
image.SetPixel(x, y, Color.FromArgb(random.Next()));
122
}
123
124
//画图片的边框线
125
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
126
System.IO.MemoryStream ms = new System.IO.MemoryStream();
127
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
128
Response.ClearContent();
129
Response.ContentType = "image/Gif";
130
Response.BinaryWrite(ms.ToArray());
131
}
132
catch (Exception ms)
133

{
134
Response.Write(ms.Message);
135
}
136
}
137
}
138
引用文件:
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

98

99

100



101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117



118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133



134

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
}

2



3

4

5



6

7

8

9



10

11

12
