Color Verification Code image (C #)

Source: Internet
Author: User

1. Why should I add a verification code?

Because the verification code can be used to defend against attacks on your website, for example, someone else can use the webbrowser control to make a simulated browser, and simulate the submission form (simulate filling in the form information and clicking the submit button), then your server must receive the values passed from these forms and make a judgment, whether or not it is correct. In this way, others can use your server resources without limit, and the account and password are not secure. in case someone else makes a round robin and one account polls, it is very likely that someone else has cracked your information, so the website logon with a slightly higher security level has a color picture verification code.
2. Why can color Verification Code images defend against attacks?

When someone else uses the polling technology to simulate login, he does not know what your verification code is, nor can he obtain it, because this is an image, the computer cannot identify the number in it (unless the interference in the verification code is cracked, the verification code may be read using the relevant image recognition technology. This will not be used here ). If you cannot read the verification code, you will not be able to perform round-robin. Of course, we must first determine whether the verification code is correct in the background to avoid occupying server resources.

3. Random Number Code

① Random Number

 
1 /// <summary>
2 // Random Number
3 /// </Summary>
4 /// <returns> </returns>
5 private string getrndnum ()
6 {
7 string code = string. empty;
8 random = new random ();
9 For (INT I = 0; I <4; I ++)
10 {
11 code = code + random. Next (9). tostring ();
12}
13 return code;
14}

② String Random Number
1 /// <summary>
2 // string Verification Code
3 /// </Summary>
4 /// <returns> </returns>
5 private string getrndstr ()
6 {
7 string vchar = "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 ";
8 string [] vcarray = vchar. Split (',');
9 string checkcode = string. empty;
10 random Rand = new random ();
11 For (INT I = 0; I <4; I ++)
12 {
13 Rand = new random (unchecked (INT) datetime. Now. ticks); // to obtain different random sequences
14 int T = Rand. next (vcarray. length); // The exclusive upper bound of the random number to be generated. maxvalue must be greater than or equal to zero, subscript starts from 0
15 checkcode + = vcarray [T];
16}
17 return checkcode;
18}

③ Chinese Random Number
1 /// <summary>
2 // random Chinese Code
3 /// </Summary>
4 /// <returns> </returns>
5 private string getrndch ()
6 {
7 system. Text. Encoding GB = system. Text. encoding. Default; // obtain the gb2312 encoding page (table)
8 object [] bytes = createregioncode (4); // call the function to generate four random Chinese character codes
9 string [] STR = new string [4];
10 system. Text. stringbuilder sb = new system. Text. stringbuilder ();
11 For (INT I = 0; I <4; I ++)
12 {
13 // decodes Chinese Characters Based on the byte array encoded by Chinese Characters
14 STR [I] = GB. getstring (byte []) convert. changetype (Bytes [I], typeof (byte []);
15 sb. append (STR [I]. tostring ());
16}
17 return sb. tostring ();
18}
19
20
21 /// <summary>
22 // generate random Chinese character encoding
23 /// </Summary>
24 /// <Param name = "strlength"> </param>
25 /// <returns> </returns>
26 private static object [] createregioncode (INT strlength)
27 {
28 // define a string array to store the components of Chinese character encoding
29 string [] rbase = new string [16] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", "F "};
30 random RND = new random ();
31 object [] bytes = new object [strlength];
32
33 for (INT I = 0; I <strlength; I ++)
34 {
35 // code 1st characters
36 int R1 = RND. Next (11, 14 );
37 string str_r1 = rbase [R1]. Trim ();
38
39 // location code 2nd characters
40 RND = new random (R1 * unchecked (INT) datetime. Now. ticks) + I );
41 int R2;
42 if (r1 = 13)
43 {
44 r2 = RND. Next (0, 7 );
45}
46 else
47 {
48 r2 = RND. Next (0, 16 );
49}
50 string str_r2 = rbase [R2]. Trim ();
51
52 // location code 3rd characters
53 RND = new random (R2 * unchecked (INT) datetime. Now. ticks) + I); // replace random Seeds
54 int R3 = RND. Next (10, 16 );
55 string str_r3 = rbase [R3]. Trim ();
56
57 // location code 4th characters
58 RND = new random (R3 * unchecked (INT) datetime. Now. ticks) + I );
59 int R4;
60 if (R3 = 10)
61 {
62 r4 = RND. Next (1, 16 );
63}
64 else if (R3 = 15)
65 {
66 r4 = RND. Next (0, 15 );
67}
68 else
69 {
70 r4 = RND. Next (0, 16 );
71}
72 string str_r4 = rbase [R4]. Trim ();
73
74 // define the random Chinese character location code generated by storing two byte Variables
75 byte byte1 = convert. tobyte (str_r1 + str_r2, 16 );
76 byte byte2 = convert. tobyte (str_r3 + str_r4, 16 );
77
78 // store two byte variables in the byte array
79 byte [] str_r = new byte [] {byte1, byte2 };
80
81 // put the byte array of the generated Chinese character into the object Array
82 bytes. setvalue (str_r, I );
83}
84 return bytes;
85}

4. Now that we have materials (random numbers), we have added and troubled the color image verification code-troubled background + image code.

 
1 /// <summary>
2 // picture background, interference
3 /// </Summary>
4 /// <Param name = "checkcode"> </param>
5 /// <returns> </returns>
6 private bitmap createimages (string checkcode, string type)
7 {
8 int step = 0;
9 If (type = "ch ")
10 {
11 step = 5; // Chinese characters are relatively large, so the word distance is relatively large
12}
13 int iwidth = (INT) (checkcode. length * (13 + stepw ));
14 system. Drawing. bitmap image = new system. Drawing. Bitmap (iwidth, 22 );
15 graphics G = graphics. fromimage (image );
16
17G. Clear (color. White); // clear the background color
18
19 color [] C = {color. black, color. red, color. darkblue, color. green, color. orange, color. brown, color. darkcyan, color. purple}; // define a random color
20
21 string [] font = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""};
22 random Rand = new random ();
23
24 For (INT I = 0; I <50; I ++)
25 {
26 int X1 = Rand. Next (image. width );
27 int X2 = Rand. Next (image. width );
28 int Y1 = Rand. Next (image. Height );
29 int y2 = Rand. Next (image. Height );
30g. drawline (new pen (color. lightgray, 1), X1, Y1, X2, Y2); // draw lines based on coordinates
31}
32
33 for (INT I = 0; I <checkcode. length; I ++)
34 {
35 int cIndex = Rand. Next (7 );
36 int findex = Rand. Next (5 );
37
38 font F = new system. Drawing. Font (font [findex], 10, system. Drawing. fontstyle. Bold );
39 brush B = new system. Drawing. solidbrush (C [cIndex]);
40 int II = 4;
41 if (I + 1) % 2 = 0)
42 {
43 II = 2;
44}
45g. drawstring (checkcode. substring (I, 1), F, B, 3 + (I * (12 + stepw), II );
46}
47
48g. drawrectangle (new pen (color. Black, 0), 0, 0, image. Width-1, image. Height-1 );
49
50 system. Io. memorystream MS = new system. Io. memorystream ();
51 return image;
52}

5. Summary

You can return the bitmap Array Based on the random number and background, and save the bitmap array as an image to the memory stream. Here, we only provide ideas and production methods, and do not provide all the code and encapsulation processes. If you have any questions about the code or need to use an instance, you can add the QQ Group 128584255. Let's take a look at it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.