加密代码
1 public static string Encode(string str, string key)
2 {
3 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
4 provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
5 provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
6 byte[] bytes = Encoding.UTF8.GetBytes(str);
7 MemoryStream stream = new MemoryStream();
8 CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
9 stream2.Write(bytes, 0, bytes.Length);
10 stream2.FlushFinalBlock();
11 StringBuilder builder = new StringBuilder();
12 foreach (byte num in stream.ToArray())
13 {
14 builder.AppendFormat("{0:X2}", num);
15 }
16 stream.Close();
17 return builder.ToString();
18 }
解密代码
1 public static string Decode(string str, string key)
2 {
3 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
4 provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
5 provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
6 byte[] buffer = new byte[str.Length / 2];
7 for (int i = 0; i < (str.Length / 2); i++)
8 {
9 int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
10 buffer[i] = (byte)num2;
11 }
12 MemoryStream stream = new MemoryStream();
13 CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
14 stream2.Write(buffer, 0, buffer.Length);
15 stream2.FlushFinalBlock();
16 stream.Close();
17 return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
18 }
前台
后台代码
1 protected void btnok_Click(object sender, EventArgs e)
2 {
3
4 txtbox.Text = Helper.Encode(txtbox.Text.Trim(), "Rainight").Trim();
5
6
7
8
9 }
10
11 protected void btncanel_Click(object sender, EventArgs e)
12 {
13 txtbox.Text = Helper.Decode(txtbox.Text.Trim(), "Rainight").Trim();
14 Response.Write(Helper.Decode(txtbox.Text.Trim(), "Rainight"));
15 }
阅读(1668) | 评论(0) | 转发(0) |