Encrypt QueryString parameters in a GridView Hyperlink column

Last post 08-09-2007 7:07 AM by griff_1000. 2 replies.

Sort Posts:

  • Encrypt QueryString parameters in a GridView Hyperlink column

    08-09-2007, 6:32 AM
    Hi
    I want to encrypt the query string appearing in the URL of a web page,
    how to do this

    for eg..
    I have a Hyperlink column in GridView ,, when I click that column, I pass two parameters ,
    these parameters should not be displayed in the URL ,,
    It should be in encrypted form with some other numbers …

    Any idea how to perform this
    Best Regards
    Winnie Cherian
    -------------------------------------------


  • Re: Encrypt QueryString parameters in a GridView Hyperlink column

    08-09-2007, 6:55 AM
    Answer
    • Loading...
    • klaus_b
    • Joined on 05-03-2006, 9:11 AM
    • Bavaria, Germany
    • Posts 565

    Hi  winniecherian,

    winniecherian:
    Hi
    I want to encrypt the query string appearing in the URL of a web page,
    how to do this

    may have a look at this article. The source is available and easy understandable. Only .NET classes are used. Try it.

     

    Servus,
    Klaus

    I haven't the faintest idea, but great many therefrom.
  • Re: Encrypt QueryString parameters in a GridView Hyperlink column

    08-09-2007, 7:07 AM
    Answer
    • Loading...
    • griff_1000
    • Joined on 01-08-2007, 3:04 PM
    • Bristol, UK
    • Posts 203

    If you need to keep the data secure, a query string perhaps isn't the safest method to use - there are other ways of passing data between pages.

    However, if you need to do it that way, here's an example of how to encrypt and decrypt a string that I got from
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemSecurityCryptography.asp

    I've pulled out a few bits which weren't relevant, see the URL above for the full example 

    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;

    namespace RC2CryptoServiceProvider_Examples
    {
    class MyMainClass
    {
    public static void Main()
    {
    string original = "This is a much longer string of data than a public/private key algorithm will accept.";
    string roundtrip;
    ASCIIEncoding textConverter = new ASCIIEncoding();
    RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
    byte[] fromEncrypt;
    byte[] encrypted;
    byte[] toEncrypt;
    byte[] key;
    byte[] IV;

    //Create a new key and initialization vector.
    rc2CSP.GenerateKey(); // You'll probably want to do this once, save it and use it in the sending and receiving pages
    rc2CSP.GenerateIV();

    //Get the key and IV.
    key = rc2CSP.Key;
    IV = rc2CSP.IV;

    //Get an encryptor.
    ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);

    //Encrypt the data.
    MemoryStream msEncrypt = new MemoryStream();
    CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

    //Convert the data to a byte array.
    toEncrypt = textConverter.GetBytes(original);

    //Write all data to the crypto stream and flush it.
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
    csEncrypt.FlushFinalBlock();

    //Get encrypted array of bytes.
    encrypted = msEncrypt.ToArray();
    // You can convert this to a string, HttpUtility.UrlEncode it and use it in your querystring - you'll have to do the reverse to decrypt
    	   // You'll need code like this in your receving page to decrypt - you'll have to store the key somewhere in order to pass it
    // between the two pages
    //Get a decryptor that uses the same key and IV as the encryptor.
    ICryptoTransform decryptor = rc2CSP.CreateDecryptor(key, IV);

    //Now decrypt the previously encrypted message using the decryptor
    // obtained in the above step.
    MemoryStream msDecrypt = new MemoryStream(encrypted);
    CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

    fromEncrypt = new byte[encrypted.Length];

    //Read the data out of the crypto stream.
    csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

    //Convert the byte array back into a string.
    roundtrip = textConverter.GetString(fromEncrypt);
    }
    }
    }

     

    -- Please Mark Posts that helped you as Answers, and share a summary of what solved the problem.
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter