Thursday, September 26, 2013

Encrypt Or Decrypt QueryString at Asp.net gridview Hyperlink using .net 3.5

Encrypt Or Decrypt QueryString at Asp.net gridview Hyperlink using .net 3.5

1.Include the namespace
using System.Text;
using System.Globalization;
using System.Security;
using System.Security.Cryptography;
using System.IO;

2.Add Hyperlink in template field of gridveiw

<asp:TemplateField HeaderText="Reference No." SortExpression="fullref">
                <ItemTemplate>      
                <asp:hyperlink
                runat="server"
                id="hyperlink1"
             
                text='<%# Eval("fullref") %>'>
                </asp:hyperlink>
                </ItemTemplate>
            <HeaderStyle BackColor="#660066" ForeColor="White"></HeaderStyle>
                </asp:TemplateField>  

3. Add the menthod to encrypt

 private byte[] key = { };
    private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
 public string Encrypt(string stringToEncrypt, string SEncryptionKey)
    {
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,
              des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }


4.Use the follwing menthod to bind encrypted querystring on rowdatabaound event of gridveiw


  protected void GDVClientView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            if (e.Row.DataItem == null) return;

            HyperLink hlobj = (HyperLink)e.Row.FindControl("hyperlink1") ;

            hlobj.NavigateUrl = String.Format("warrantdetailclient.aspx?fullref={0}",
                                                       Server.UrlEncode(Encrypt(hlobj.Text, "r0b1nr0y")));
        }
        catch (Exception ex)
        {
            //
        }

    }


5.Now on next page use the decript method as:
 protected void Page_Load(object sender, EventArgs e)
    {
 if (Request.Params["fullref"] != null)
        {
            string decrpval = Decrypt(Request.Params["fullref"].ToString(), "r0b1nr0y");
}
}

     private byte[] key = { };
    private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
    public string Decrypt(string stringToDecrypt, string sEncryptionKey)
    {
        byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(stringToDecrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms,
              des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

2 comments:

  1. I really enjoyed reading this post, big fan. Keep up the good work andplease tell me when can you publish more articles or where can I read more on the subject?
    cs代做

    ReplyDelete
  2. Im no expert, but I believe you just made an excellent point. You certainly fully understand what youre speaking about, and I can truly get behind that.
    cs代写

    ReplyDelete