I. Verification Code Introduction
The verification code function is generally used to prevent batch registration. Many websites use the verification code technology to prevent users from automatically registering, logging on, And bumping through robots. The so-called verification code is to generate an image with a random string of numbers, letters, symbols, or characters, and add some interference pixels to the image to prevent OCR ), the user can identify the verification code information with the naked eye, and enter a form to submit the website for verification. A function can be used only after the verification is successful.
Common verification codes are as follows:
1. Pure Digital verification code, generally four random numbers;
2. digit + letter verification code, generally from number (0 ~ 9) and letters (~ Z and ~ Z;
3. Chinese Character verification code, which is relatively rare and complicated to implement, can still be seen on many websites;
Ii. Verification Code Implementation
1. Implementation of pure digital verification Codes
The implementation of a pure digital verification code is relatively simple, and can be achieved through the following two methods
(1) Use the random number method. The Code is as follows:
Private string getrandomint (INT codecount) <br/>{< br/> random = new random (); <br/> string min = ""; <br/> string max = ""; <br/> for (INT I = 0; I <codecount; I ++) <br/>{< br/> min + = "1"; <br/> MAX + = "9"; <br/>}< br/> return (random. next (convert. toint32 (min), convert. toint32 (max )). tostring (); <br/>}
(2) Use a random combination. The Code is as follows:
Private string createrandomcode (INT codecount) <br/>{< br/> string allchar = "; <br/> string [] allchararray = allchar. split (','); <br/> string randomcode = ""; <br/> int temp =-1; <br/> random Rand = new random (); <br/> for (INT I = 0; I <codecount; I ++) <br/>{< br/> If (temp! =-1) <br/>{< br/> Rand = new random (I * temp * (INT) datetime. now. ticks); <br/>}< br/> int T = Rand. next (9); <br/> If (temp = T) <br/>{< br/> return createrandomcode (codecount ); <br/>}< br/> temp = T; <br/> randomcode + = allchararray [T]; <br/>}< br/> return randomcode; <br/>}
2. The Code is as follows:
Private string createrandomcode (INT codecount) <br/>{< br/> string allchar = ", 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 "; <br/> string [] allchararray = allchar. split (','); <br/> string randomcode = ""; <br/> int temp =-1; <br/> random Rand = new random (); <br/> for (INT I = 0; I <codecount; I ++) <br/>{< br/> If (TEM P! =-1) <br/>{< br/> Rand = new random (I * temp * (INT) datetime. now. ticks); <br/>}< br/> int T = Rand. next (61); <br/> If (temp = T) <br/>{< br/> return createrandomcode (codecount ); <br/>}< br/> temp = T; <br/> randomcode + = allchararray [T]; <br/>}< br/> return randomcode; <br/>}< br/>
3. The implementation of the Chinese character verification code is as follows:
/**/<Br/>/* This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents one Chinese character, and store the <br/> four byte arrays in the object array. <Br/> parameter: strlength, indicating the number of Chinese characters to be generated <br/> */<br/> Public static object [] createregioncode (INT strlength) <br/>{< br/> // define a string array to store the elements of Chinese character encoding <br/> string [] rbase = new string [16] {"0 ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "B", "C", "D", "E", "F" };< br/> random RND = new random (); <br/> // define an object array <br/> object [] bytes = new object [strlength]; </P> <p>/**/<br/>/* each cycle generates a hexadecimal byte array containing two elements, put it into the bject array. <br/> each Chinese character has four location codes. <br/> the location code 1st and the location code 2nd are used as the first element of the byte array. <br/> the location code 3rd the second element of the byte array <br/> */<br/> for (INT I = 0; I <strlength; I ++) <br/>{< br/> // location code 1st bits <br/> int R1 = RND. next (11, 14); <br/> string str_r1 = rbase [R1]. trim (); </P> <p> // location code 2nd bits <br/> RND = new random (R1 * unchecked (INT) datetime. now. ticks) + I); // Replace the seed of the random number generator to avoid repeated values <br/> int R2; <br/> If (r1 = 13) <br/>{< br/> r2 = RND. next (0, 7); <br/>}< br/> else <br/>{< br/> r2 = RND. next (0, 16); <br/>}< br/> string str_r2 = rbase [R2]. trim (); </P> <p> // location code 3rd bits <br/> RND = new random (R2 * unchecked (INT) datetime. now. ticks) + I); <br/> int R3 = RND. next (10, 16); <br/> string str_r3 = rbase [R3]. trim (); </P> <p> // location code 4th bits <br/> RND = new random (R3 * unchecked (INT) datetime. now. ticks) + I); <br/> int R4; <br/> If (R3 = 10) <br/>{< br/> r4 = RND. next (1, 16); <br/>}< br/> else if (R3 = 15) <br/>{< br/> r4 = RND. next (0, 15); <br/>}< br/> else <br/>{< br/> r4 = RND. next (0, 16); <br/>}< br/> string str_r4 = rbase [R4]. trim (); </P> <p> // defines the random Chinese character location code generated by storage of two byte variables <br/> byte byte1 = convert. tobyte (str_r1 + str_r2, 16); <br/> byte byte2 = convert. tobyte (str_r3 + str_r4, 16); <br/> // store two byte variables in the byte array <br/> byte [] str_r = new byte [] {byte1, byte2 }; </P> <p> // put the byte array of the generated Chinese characters into the object array. <br/> bytes. setvalue (str_r, I); <br/>}< br/> return bytes; <br/>}
Iii. Specific instances
Front-end code display. aspx
<Body> <br/> <Form ID = "form1" runat = "server"> <br/> <div> <br/> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <br/> <asp: image id = "image1" runat = "server" imageurl = "PNG. aspx "/> <br/> <asp: button id = "button2" runat = "server" onclick = "button2_click" text = "button"/> <br/> <asp: dropdownlist id = "dropdownlist1" runat = "server" autopostback = "true" onselectedindexchanged = "dropdownlist1_selectedindexchanged"> <br/> <asp: listitem value = "3"> default </ASP: listitem> <br/> <asp: listitem value = "1"> text </ASP: listitem> <br/> <asp: listitem value = "2"> Number </ASP: listitem> <br/> <asp: listitem value = "3"> hybrid </ASP: listitem> <br/> </ASP: dropdownlist> </div> <br/> </form> <br/> </body>
Background code display. CS
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs e) <br/>{< br/> switch (dropdownlist1.selectedvalue) <br/>{< br/> case "1 ": <br/> image1.imageurl = "PNG. aspx? AA = 1 "; <br/> break; <br/> case" 2 ": <br/> image1.imageurl =" PNG. aspx? AA = 2 "; <br/> break; <br/> case" 3 ": <br/> image1.imageurl =" PNG. aspx? AA = 3 "; <br/> break; <br/>}< br/> protected void button2_click (Object sender, eventargs E) <br/>{< br/> If (textbox1.text = session ["GIF"]. tostring () <br/> response. write ("OK, correct"); <br/> else <br/> response. write ("The Verification Code does not match"); <br/>}
Background code PNG. CS
Public partial class PNG: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> switch (request. querystring ["AA"]) <br/>{< br/> case "1": <br/> GIF = stxt (10 ); <br/> session ["GIF"] = stxt (10); <br/> break; <br/> case "2 ": <br/> GIF = getrandomint (4); <br/> session ["GIF"] = getrandomint (4); <br/> break; <br/> case "3": <br/> GIF = createrandomcode (4); <Br/> session ["GIF"] = createrandomcode (4); <br/> break; <br/> default: <br/> GIF = createrandomcode (4 ); <br/> session ["GIF"] = createrandomcode (4); <br/> break; <br/>}< br/> createimage (session ["GIF"]. tostring (); <br/>}</P> <p> private string getrandomint (INT codecount) <br/>{< br/> random = new random (); <br/> string min = ""; <br/> string max = ""; <br/> for (INT I = 0; I <codecount; I ++) <br/>{< br/> min + = "1"; <br/> MAX + = "9 "; <br/>}< br/> return (random. next (convert. toint32 (min), convert. toint32 (max )). tostring ()); <br/>}< br/>/**/<br/>/* <br/> This function randomly creates hexadecimal bytes containing two elements within the Chinese character encoding range. array, each byte array represents a Chinese character and stores the <br/> four byte arrays in the object array. <Br/> parameter: strlength, indicating the number of Chinese characters to be generated <br/> */<br/> Public static object [] createregioncode (INT strlength) <br/>{< br/> // define a string array to store the elements of Chinese character encoding <br/> string [] rbase = new string [16] {"0 ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "B", "C", "D", "E", "F" };< br/> random RND = new random (); <br/> // define an object array <br/> object [] bytes = new object [strlength]; </P> <p>/**/<br/>/* each cycle generates a hexadecimal element containing two elements. Byte array, put it into the bject array. <br/> each Chinese character has four location codes. <br/> the location code 1st and the location code 2nd are used as the first element of the byte array. <br/> the location code 3rd the second element of the byte array <br/> */<br/> for (INT I = 0; I <strlength; I ++) <br/>{< br/> // location code 1st bits <br/> int R1 = RND. next (11, 14); <br/> string str_r1 = rbase [R1]. trim (); </P> <p> // location code 2nd bits <br/> RND = new random (R1 * unchecked (INT) datetime. now. ticks) + I); // Replace the seed of the random number generator to avoid repeated values <br/> int R2; <br/> If (r1 = 13) <br/> {<br/> R2 = RND. next (0, 7); <br/>}< br/> else <br/>{< br/> r2 = RND. next (0, 16); <br/>}< br/> string str_r2 = rbase [R2]. trim (); </P> <p> // location code 3rd bits <br/> RND = new random (R2 * unchecked (INT) datetime. now. ticks) + I); <br/> int R3 = RND. next (10, 16); <br/> string str_r3 = rbase [R3]. trim (); </P> <p> // location code 4th bits <br/> RND = new random (R3 * unchecked (INT) datetime. now. ticks) + I); <br/> int R4; <br/> If (R3 = 10) <Br/> {<br/> r4 = RND. next (1, 16); <br/>}< br/> else if (R3 = 15) <br/>{< br/> r4 = RND. next (0, 15); <br/>}< br/> else <br/>{< br/> r4 = RND. next (0, 16); <br/>}< br/> string str_r4 = rbase [R4]. trim (); </P> <p> // defines the random Chinese character location code generated by storage of two byte variables <br/> byte byte1 = convert. tobyte (str_r1 + str_r2, 16); <br/> byte byte2 = convert. tobyte (str_r3 + str_r4, 16); <br/> // store two byte variables in the byte array <br/> byte [] str_r = new Byte [] {byte1, byte2}; </P> <p> // put the byte array of A Generated Chinese character into the object array. <br/> bytes. setvalue (str_r, I); <br/>}< br/> return bytes; <br/>}< br/> private string stxt (INT num) <br/> {<br/> encoding GB = encoding. getencoding ("gb2312"); </P> <p> // call the function to generate 10 random Chinese character codes. <br/> object [] bytes = createregioncode (Num ); <br/> string strtxt = ""; </P> <p> // decodes Chinese characters from the byte array encoded in Chinese characters. <br/> for (INT I = 0; I <num; I ++) <br/>{< br/> Strtxt + = GB. getstring (byte []) convert. changetype (Bytes [I], typeof (byte []); <br/>}< br/> return strtxt; <br/>}< br/> /// <summary> <br/> // This is a letter, digital mixing <br/> /// </Summary> <br/> // <Param name = "vcodenum"> </param> <br/> // <returns> </returns> <br/> private string createrandomcode (INT codecount) <br/> {<br/> string allchar = "0, 1, 2, 3, 4, 5, 6, 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, 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 "; <br/> string [] allchararray = allchar. split (','); <br/> string randomcode = ""; <br/> int temp =-1; <br/> random Rand = new random (); <br/> for (INT I = 0; I <codecount; I ++) <br/>{< br/> If (temp! =-1) <br/>{< br/> Rand = new random (I * temp * (INT) datetime. now. ticks); <br/>}< br/> int T = Rand. next (61); <br/> If (temp = T) <br/>{< br/> return createrandomcode (codecount ); <br/>}< br/> temp = T; <br/> randomcode + = allchararray [T]; <br/>}< br/> return randomcode; <br/>}</P> <p> private void createimage (string checkcode) <br/>{< br/> int iwidth = (INT) (checkcode. length * 20); <br/> system. drawing. bitmap image = new system. drawing. bitmap (iwidth, 25); <br/> graphics G = graphics. fromimage (image); <br/> font F = new system. drawing. font ("Arial", 10, system. drawing. fontstyle. bold); <br/> brush B = new system. drawing. solidbrush (color. white); <br/> // G. fillrectangle (new system. drawing. solidbrush (color. blue), 0, 0, image. width, image. height); <br/> G. clear (color. black); <br/> G. drawstring (checkcode, F, B, 3, 3); <br/> pen blackpen = new pen (color. black, 0); <br/> random Rand = new random (); <br/> // For (INT I = 0; I <5; I ++) <br/> // {<br/> // int y = Rand. next (image. height); <br/> // G. drawline (blackpen, 0, Y, image. width, Y); <br/>/}< br/> system. io. memorystream MS = new system. io. memorystream (); <br/> image. save (MS, system. drawing. imaging. imageformat. JPEG); <br/> response. clearcontent (); <br/> response. contenttype = "image/JPEG"; <br/> response. binarywrite (Ms. toarray (); <br/> G. dispose (); <br/> image. dispose (); <br/>}< br/>}
Iv. Presentation
Verification code number
Verification code number + letter
Verification Code Chinese Characters