Encryption and decryption of URL parameters in asp.net

Source: Internet
Author: User
Encryption and decryption of URL parameters in asp.net

 

 

Encryption Code

  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();           }

 

Decryption code

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());           }

 

Front-end

<Asp: TextBox ID = "txtbox" runat = "server"> </asp: TextBox> <asp: button ID = "btnok" runat = "server" onclick = "btnok_Click" Text = "encrypted"/> <asp: button ID = "btncanel" runat = "server" Text = "decrypt" onclick = "btncanel_Click"/>

 

Background code

  

  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"));           }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.