<%@ Page Language=
"C#"
AutoEventWireup=
"true"
%>
<%@ Import Namespace=
"System.Drawing"
%>
<%@ Import Namespace=
"System.Drawing.Imaging"
%>
<%@ Import Namespace=
"System.IO"
%>
<%@ Import Namespace=
"System.Security.Cryptography"
%>
<script runat=
"server"
>
public
static
string Encrypt(string Text)
{
string sKey =
"nysoftland.com.cn"
;
DESCryptoServiceProvider des =
new
DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,
"md5"
).Substring(
0
,
8
));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,
"md5"
).Substring(
0
,
8
));
System.IO.MemoryStream ms =
new
System.IO.MemoryStream();
CryptoStream cs =
new
CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray,
0
, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret =
new
StringBuilder();
foreach (byte b
in
ms.ToArray())
{
ret.AppendFormat(
"{0:X2}"
, b);
}
return
ret.ToString();
}
public
static
string Decrypt(string Text)
{
string sKey =
"Exchange"
;
DESCryptoServiceProvider des =
new
DESCryptoServiceProvider();
int
len;
len = Text.Length /
2
;
byte[] inputByteArray =
new
byte[len];
int
x, i;
for
(x =
0
; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x *
2
,
2
),
16
);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,
"md5"
).Substring(
0
,
8
));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,
"md5"
).Substring(
0
,
8
));
System.IO.MemoryStream ms =
new
System.IO.MemoryStream();
CryptoStream cs =
new
CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray,
0
, inputByteArray.Length);
cs.FlushFinalBlock();
return
Encoding.Default.GetString(ms.ToArray());
}
protected
void
Page_Load(object sender, EventArgs e)
{
int
codeW =
80
;
int
codeH =
22
;
int
fontSize =
16
;
string chkCode = string.Empty;
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
string[] font = {
"Times New Roman"
,
"Verdana"
,
"Arial"
,
"Gungsuh"
,
"Impact"
};
char[] character = {
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'8'
,
'9'
,
'a'
,
'b'
,
'd'
,
'e'
,
'f'
,
'h'
,
'k'
,
'm'
,
'n'
,
'r'
,
'x'
,
'y'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
,
'G'
,
'H'
,
'J'
,
'K'
,
'L'
,
'M'
,
'N'
,
'P'
,
'R'
,
'S'
,
'T'
,
'W'
,
'X'
,
'Y'
};
Random rnd =
new
Random();
for
(
int
i =
0
; i <
4
; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
HttpCookie cook =
new
HttpCookie(
"yzmCode"
, Encrypt(chkCode));
cook.Expires = DateTime.Now.AddMinutes(
20
);
Response.Cookies.Add(cook);
Bitmap bmp =
new
Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
for
(
int
i =
0
; i <
1
; i++)
{
int
x1 = rnd.Next(codeW);
int
y1 = rnd.Next(codeH);
int
x2 = rnd.Next(codeW);
int
y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(
new
Pen(clr), x1, y1, x2, y2);
}
for
(
int
i =
0
; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft =
new
Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft,
new
SolidBrush(clr), (float)i *
18
+
2
, (float)
0
);
}
for
(
int
i =
0
; i <
100
; i++)
{
int
x = rnd.Next(bmp.Width);
int
y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
Response.Buffer =
true
;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(
0
);
Response.Expires =
0
;
Response.CacheControl =
"no-cache"
;
Response.AppendHeader(
"Pragma"
,
"No-Cache"
);
MemoryStream ms =
new
MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType =
"image/Png"
;
Response.BinaryWrite(ms.ToArray());
}
finally
{
bmp.Dispose();
g.Dispose();
}
}
</script>