Results 1 to 6 of 6

Thread: hide the query in URL string

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    hide the query in URL string

    I have a lot of query in my URL that I want to hide/shorten it.. is there a function in asp.net that I can use?

    E.x: http://www.test.com?c=test&i=1&id=2

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: hide the query in URL string

    Below is a DES 2 way crypto functions. Just put it in a class in the app_code folder and you can call encrypt/decript for a string like your QS - encrypt("c=test&i=1&id=2").

    Because you are using the result in a url querystring I think you need to html encode and decode them first to make sure any special characters aren't altered in that way.

    server.htmlencode(encrypt("..."))
    server.htmldecode(decrypt("..."))

    There is also tripleDES crypto for extra strong security but it uses extra resources and would be overkill for this purpose.


    Code:
    ' at top of pg
    Imports Microsoft.VisualBasic
    Imports System.IO
    Imports System.Security.Cryptography 
    
    
    
    
        '8 bytes randomly selected for both the Key and the Initialization Vector
        'the IV is used to encrypt the first block of text so that any repetitive 
        'patterns are not apparent
    '=======CHANGE THESE NUMBERS  ===========
        Private Shared KEY_64() As Byte = {13, 56, 211, 176, 129, 4, 213, 222}
        Private Shared IV_64() As Byte = {52, 143, 26, 173, 236, 9, 112, 39}
    
    
        ''' <summary>
        ''' Standard DES encryption
        ''' </summary>
        ''' <param name="value">string to encrypt</param>
        ''' <returns>encrypted string</returns>
        ''' <remarks></remarks>
        Public Shared Function Encrypt(ByVal value As String) As String
            If value <> "" Then
                Dim cryptoProvider As DESCryptoServiceProvider = _
                    New DESCryptoServiceProvider()
                Dim ms As MemoryStream = New MemoryStream()
                Dim cs As CryptoStream = _
                    New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
                        CryptoStreamMode.Write)
                Dim sw As StreamWriter = New StreamWriter(cs)
    
                sw.Write(value)
                sw.Flush()
                cs.FlushFinalBlock()
                ms.Flush()
    
                'convert back to a string
                Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
            Else
                Return String.Empty
            End If
        End Function
    
    
        ''' <summary>
        ''' Standard DES decryption
        ''' </summary>
        ''' <param name="value">string to decrypt</param>
        ''' <returns>decrypt string</returns>
        ''' <remarks></remarks>
        Public Shared Function Decrypt(ByVal value As String) As String
            If value <> "" Then
                Dim cryptoProvider As DESCryptoServiceProvider = _
                    New DESCryptoServiceProvider()
    
                'convert from string to byte array
                Dim buffer As Byte() = Convert.FromBase64String(value)
                Dim ms As MemoryStream = New MemoryStream(buffer)
                Dim cs As CryptoStream = _
                    New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), _
                        CryptoStreamMode.Read)
                Dim sr As StreamReader = New StreamReader(cs)
    
                Return sr.ReadToEnd()
            Else
                Return String.Empty
            End If
        End Function
    Last edited by brin351; Jun 30th, 2009 at 08:26 PM. Reason: fix

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: hide the query in URL string

    Hey,

    You might also want to consider URL Re-Writing as another option:

    http://www.urlrewriting.net/149/en/home.html

    Gary

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: hide the query in URL string

    hm.. why encrypt and decrypt works in this case? I am confused.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: hide the query in URL string

    Hey,

    I think the suggestion to encrypt and decrypt came from the fact that you wanted to hide the query string parameters. Sometimes hiding can mean obscuring. i.e. you don't want the user to be able to see and tamper with the parameters, in which case you would hide them so they can't.

    Did you have a look at URL Re-Writing, is this what you mean.

    There is also the concept of Form Routing. This is used in the ASP.Net MVC Framework, and it will be making an appearance in .Net 4.0.

    Gary

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: hide the query in URL string

    Quote Originally Posted by vbbit View Post
    hm.. why encrypt and decrypt works in this case? I am confused.
    I did actually think you wanted to hide the query parameters. Is the length a problem?

    You can compress strings to a byte array but I don't know of compressing a string to another string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width