How to encrypt a query string ?

Few months back I had used a nice component from TSHAK to encrypt query strings. it requires just one line of code to encrypt a query string. Right now I am unable to find the exact page at TSHAK where I got the control. Due to a site re organization at TSHAK , they might have moved it to some other place. You can download the component here and place it in your bin folder.

To encrypt a query string we will need to pass it an encryption key which is a byte array

Dim qs As TSHAK.Components.SecureQueryString
qs = New TSHAK.Components.SecureQueryString(New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 8})

To add the query string values
qs("Myname")="John"
qs("MyAge")=5
Response.Redirect("Page2.aspx?data=" + HttpUtility.UrlEncode(qs.ToString()))

Now to decrypt and use the values in Page2.aspx
Dim qs As TSHAK.Components.SecureQueryString
qs= New TSHAK.Components.SecureQueryString(New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 8}, Request("data"))
Dim myName=qs("MyName")
Dim age=qs("MyAge")

kick it on DotNetKicks.com

22 Responses to “How to encrypt a query string ?”

  1. Thanks,

    This is working properly.

  2. Hi,

    I just wanted to point out an error in the code. Shouldn’t the line: Dim age=qs(“Age”) be Dim age=qs(“MyAge”) in the decryption code?

    Thanks,

    SyntaxC4

  3. Thank You SyntaxC4 , Corrected

  4. Hi,
    There is a simple way to encrypt querystring.just convert the query u have passed into binary format and while chcking it with database convert it to text again.

  5. How secured is the query string that is encrypted

  6. Nice!

    Mads Kristensen made a post s few years ago that I used which implements a IHttpModule to encrypt and decrpyt data;

    http://blog.madskristensen.dk/post/HttpModule-for-query-string-encryption.aspx

    This was sweet since you didn’t need to do anything to your code, just use Response.Redirect(“MyPage.aspx?id=1201″); and the module code will encrypt the QueryString on the way out the door, and then decrypt it on the way back in.

    There were some draw backs of this at one point with form controls postback info posting in the status bar of the browser, yet using a Form Control Adpater lets you get around this!

    RA

  7. Is there any way I can use this component for automatic encryption/decryption ?

  8. hi,
    i need to encrypted my query string when i pass in gridview.
    for example,i pass in query string like following

    so how i encrypted it?
    Thanking you.

  9. hi,
    i need to encrypted my query string when i pass in gridview.
    for example,i pass in query string like following,
    asp:HyperLinkField DataNavigateUrlFields=”PRJCODE,ALTCODE,ENTCODE”
    DataNavigateUrlFormatString=”AllotmentEdit.aspx?pcode={0}&altcode={1}&ENTPCODE={2}”
    Text=”[Select]” HeaderText=”Action”

    so how i encrypted it?
    Thanking you.

  10. nice article

  11. thanks
    i search for it
    i will try it now :)

  12. how can i use this in c#… Is there any namespace to include for vs.2005

  13. Originally Posted By pradeep kumar
    Thanks,

    The component will work either in c# or VB.NET. You may declare it like this in c#
    TSHAK.Components.SecureQueryString qs;

  14. how can i encrypt querystirng when i am passing from a hyperling in
    gridview

  15. Hi,

    This one works fine with IE, but throws Exception, Invalid QueryString in Firefox 3.0.6.

    Any help is appreciated.

  16. RA,
    http://blog.madskristensen.dk/post/HttpModule-for-query-string-encryption.aspx

    faced problem for POST form methods but searched for Form Control and got help from the article: http://www.codeproject.com/KB/aspnet/UrlRewriteForAjax.aspx
    which explains the use of form control adapters.

    this saved lot of work for me,

    thanks a lot.

    please keep helping

  17. i am getting one error in c#

  18. i am getting one error in c# i.e semicolon expected in line 2 but i have written everything right. my code is
    TSHAK.Components.SecureQueryString qs = New TSHAK.Components.SecureQueryString (new Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 8});
    qs(“Myname”)=”John”;
    qs(“MyAge”)=5;
    Response.Redirect(“Page2.aspx?data=” + HttpUtility.UrlEncode(qs.ToString()));

  19. @varun -
    in c# it will be
    qs["Myname"]=”John”;

  20. i m getting d following error…in c#

    Cannot initialize type ‘byte’ with a collection initializer because it does not implement ‘System.Collections.IEnumerable’.

    is dere any extra namespace required???

    pls help me out

  21. hi…i am getting an error on this particular link

    http://blog.madskristensen.dk/post/HttpModule-for-query-string-encryption.aspx

    please check and get the same corrected…… :)

  22. nirav pandya on May 14th, 2010 at 2:12 pm

    i like ur post but i just want to know that is there any license require for dll , can i use freely in my code. its working in my code.

    thanks for post

Leave a Reply