日期:2010-10-12 08:39:00 来源:本站整理
URL参数加密解密历程[网站编程]
本文“URL参数加密解密历程[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
加密代码
- public static string Encode(string str, string key)
- {
- DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
- provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
- provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
- byte[] bytes = Encoding.UTF8.GetBytes(str);
- MemoryStream stream = new MemoryStream();
- CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
- stream2.Write(bytes, 0, bytes.Length);
- stream2.FlushFinalBlock();
- StringBuilder builder = new StringBuilder();
- foreach (byte num in stream.ToArray())
- {
- builder.AppendFormat("{0:X2}", num);
- }
- stream.Close();
- return builder.ToString();
- }
解密代码
- public static string Decode(string str, string key)
- {
- DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
- provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
- provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
- byte[] buffer = new byte[str.Length / 2];
- for (int i = 0; i < (str.Length / 2); i++)
- {
- int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
- buffer[i] = (byte)num2;
- }
- MemoryStream stream = new MemoryStream();
- CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
- stream2.Write(buffer, 0, buffer.Length);
- stream2.FlushFinalBlock();
- stream.Close();
- return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
- }
前台
- <p>
- <asp:TextBox ID="txtbox" runat="server"></asp:TextBox>
- <asp:Button ID="btnok" runat="server" onclick="btnok_Click" Text="加密" />
- <asp:Button ID="btncanel" runat="server" Text="解密" onclick="btncanel_Click" />
- </p>
后台代码
- protected void btnok_Click(object sender, EventArgs e)
- {
- txtbox.Text = Helper.Encode(txtbox.Text.Trim(), "Rainight").Trim();
- }
- protected void btncanel_Click(object sender, EventArgs e)
- {
- txtbox.Text = Helper.Decode(txtbox.Text.Trim(), "Rainight").Trim();
- Response.Write(Helper.Decode(txtbox.Text.Trim(), "Rainight"));
- }
以上是“URL参数加密解密历程[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论