using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class CnCode :
{
protected void Page_Load(object sender, EventArgs e)
{
//Get GB2312 encoded page (table)
Encoding gb = ("gb2312");
//Calling the function to generate 4 random Chinese character encodings
object[] bytes = CreateRegionCode(4);
//Decode Chinese characters based on the byte array encoded by Chinese characters.
string str1 = ((byte[])(bytes[0], typeof(byte[])));
string str2 = ((byte[])(bytes[1], typeof(byte[])));
string str3 = ((byte[])(bytes[2], typeof(byte[])));
string str4 = ((byte[])(bytes[3], typeof(byte[])));
//Output console
CreateImage(str1 + str2 + str3 + str4);
}
/*
This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents a Chinese character and will
The four byte array is stored in the object array.
Parameters: strlength, representing the number of Chinese characters to be generated
*/
public static object[] CreateRegionCode(int strlength)
{
//Define a string array to store the components of Chinese character encoding
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();
//Define an object array to use
object[] bytes=new object[strlength];
/**//*Every loop generates a hexadecimal byte array containing two elements at a time and puts it into the bject array.
Each Chinese character consists of four location codes.
The first bit of the location code and the second bit of the location code are used as the first element of the byte array.
The third bit of the location code and the fourth bit of the location code are used as the second element of the byte array.
*/
for(int i=0;i<strlength;i++)
{
//The first position of the area code
int r1=(11,14);
string str_r1=rBase[r1].Trim();
//The second position code
rnd=new Random(r1*unchecked((int))+i);//Replace the seed of the random number generator to avoid duplicate values.
int r2;
if (r1==13)
{
r2=(0,7);
}
else
{
r2=(0,16);
}
string str_r2=rBase[r2].Trim();
//The third position code
rnd=new Random(r2*unchecked((int))+i);
int r3=(10,16);
string str_r3=rBase[r3].Trim();
//The fourth position code
rnd=new Random(r3*unchecked((int))+i);
int r4;
if (r3==10)
{
r4=(1,16);
}
else if (r3==15)
{
r4=(0,15);
}
else
{
r4=(0,16);
}
string str_r4=rBase[r4].Trim();
//Define the random Chinese character location code generated by two byte variable storage
byte byte1=(str_r1 + str_r2,16);
byte byte2=(str_r3 + str_r4,16);
//Storing two byte variables in a byte array
byte[] str_r=new byte[]{byte1,byte2};
//Put the byte array of a generated Chinese character into the object array.
(str_r,i);
}
return bytes;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)( * 25);
image = new (iwidth, 20);
Graphics g = (image);
Font f = new ("Arial", 12, );
Brush b = new ();
//(new (),0,0,, );
();
(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(, 0);
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
int y = ();
(blackPen, 0, y, , y);
}
ms = new ();
(ms, );
();
= "image/png";
(());
();
();
}
}
This article is reproduced from 'Dazhi's Blog': /?id=1274
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class CnCode :
{
protected void Page_Load(object sender, EventArgs e)
{
//Get GB2312 encoded page (table)
Encoding gb = ("gb2312");
//Calling the function to generate 4 random Chinese character encodings
object[] bytes = CreateRegionCode(4);
//Decode Chinese characters based on the byte array encoded by Chinese characters.
string str1 = ((byte[])(bytes[0], typeof(byte[])));
string str2 = ((byte[])(bytes[1], typeof(byte[])));
string str3 = ((byte[])(bytes[2], typeof(byte[])));
string str4 = ((byte[])(bytes[3], typeof(byte[])));
//Output console
CreateImage(str1 + str2 + str3 + str4);
}
/*
This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents a Chinese character and will
The four byte array is stored in the object array.
Parameters: strlength, representing the number of Chinese characters to be generated
*/
public static object[] CreateRegionCode(int strlength)
{
//Define a string array to store the components of Chinese character encoding
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();
//Define an object array to use
object[] bytes=new object[strlength];
/**//*Every loop generates a hexadecimal byte array containing two elements at a time and puts it into the bject array.
Each Chinese character consists of four location codes.
The first bit of the location code and the second bit of the location code are used as the first element of the byte array.
The third bit of the location code and the fourth bit of the location code are used as the second element of the byte array.
*/
for(int i=0;i<strlength;i++)
{
//The first position of the area code
int r1=(11,14);
string str_r1=rBase[r1].Trim();
//The second position code
rnd=new Random(r1*unchecked((int))+i);//Replace the seed of the random number generator to avoid duplicate values.
int r2;
if (r1==13)
{
r2=(0,7);
}
else
{
r2=(0,16);
}
string str_r2=rBase[r2].Trim();
//The third position code
rnd=new Random(r2*unchecked((int))+i);
int r3=(10,16);
string str_r3=rBase[r3].Trim();
//The fourth position code
rnd=new Random(r3*unchecked((int))+i);
int r4;
if (r3==10)
{
r4=(1,16);
}
else if (r3==15)
{
r4=(0,15);
}
else
{
r4=(0,16);
}
string str_r4=rBase[r4].Trim();
//Define the random Chinese character location code generated by two byte variable storage
byte byte1=(str_r1 + str_r2,16);
byte byte2=(str_r3 + str_r4,16);
//Storing two byte variables in a byte array
byte[] str_r=new byte[]{byte1,byte2};
//Put the byte array of a generated Chinese character into the object array.
(str_r,i);
}
return bytes;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)( * 25);
image = new (iwidth, 20);
Graphics g = (image);
Font f = new ("Arial", 12, );
Brush b = new ();
//(new (),0,0,, );
();
(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(, 0);
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
int y = ();
(blackPen, 0, y, , y);
}
ms = new ();
(ms, );
();
= "image/png";
(());
();
();
}
}
This article is reproduced from 'Dazhi's Blog': /?id=1274