Monday, May 23, 2005

QueryString Encryptor HTTPModule

I wrote an HttpModule that Encrypts querystrings.  This module is seamless to the web developer. You can create html that looks like this: myPage.aspx?id=1&customer=2 and the source on the client will automatically be converted to this: myPage.aspx?eqs=KS%2bthrckechBKT%2bZ8IB44Bz3qvW3853f. Then to access the value in the code behind page you would use QueryString["id"] or QueryString["customer"].

To install it add QSHttpModules.dll to your bin directory and then add the following to your web.config (any where in <system.web>):

<httpModules>
   <
add type="QSHttpModules.QueryStringEncryptor, QSHttpModules" name="QueryStringEncryptor" />

</httpModules>


The module does this by finding the links and replacing them with the encrypted version as the page is sent out to the client. Then on every request the module looks for an encrypted querystring, if found it decrypts it and rewrites the url.

The code is listed below, but you can also download it from my message board at: http://csharpboard.com/ShowPost.aspx?PostID=44. This module of course takes some overhead to process and it is not recommended to be used as a full security feature. Rights checking should always be in place. But, if a little cpu time is worth hiding the contents of your querystring, this may be for you.

As always, use at your own risk.


Code:

7 Comments:

At 9/08/2004 10:31 PM , Anonymous Anonymous said...

Dude, this is really tight!

 
At 1/19/2006 6:27 AM , Anonymous Roli said...

Hi

The QueryStringEncryptor is very nice and works fine!

But I can't use the QueryStringEncryptor and ASP.NET themes at the same time. I think the problem is the line

application.Response.Filter = new QueryStringResponseFilter(application.Response.Filter, application.Server);

Perhaps themes would applicated by the Framework 2.0 by Response.Filter too?

Has anyother the same problems and a solution for that?

Cu, Roli

 
At 2/23/2006 10:58 AM , Anonymous Anonymous said...

change it to the code below, then you should be fine

if(context.Request.Path.ToLower().EndsWith("aspx")) // set the response stream filter only for aspx page
{
application.Response.Filter = new QueryStringResponseFilter(application.Response.Filter, application.Server);
}

 
At 2/23/2006 11:06 AM , Anonymous crypto128 said...

Overall it is great. Just there is 2 small bugs I found for this module. If you have "xxxx.aspx?id=1" and "xxxx.aspx?id=11", when you do a string replace in your overrided write, you will get trouble. Also if the page size bigger then buffer size, you will get trouble.

So change it to:

public override void Write(byte[] buffer, int offset, int count)
{
string sBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
StringBuilder html = new StringBuilder();

MatchCollection aspxPageMatches = Regex.Matches(
sBuffer,
"([-A-Z0-9+&@#/%~_|!:,.;]*)?\\.(aspx|ashx|axd)\\?([-A-Z0-9+&@#/%=~_|!:,.;]*)?",
RegexOptions.IgnoreCase);

if(aspxPageMatches.Count > 0)
{
html.Append(sBuffer.Substring(0, aspxPageMatches[0].Index));
for(int i = 0; i < aspxPageMatches.Count-1; i++)
{
// StreamWriter sw = new StreamWriter(@"C:\temp\log.txt", true);
// sw.WriteLine(aspxPageMatches[i].Value);
// sw.Flush();
// sw.Close();

html.Append(EncryptQueryString(aspxPageMatches[i].Value));
html.Append(sBuffer.Substring(aspxPageMatches[i].Index + aspxPageMatches[i].Length, aspxPageMatches[i+1].Index - (aspxPageMatches[i].Index + aspxPageMatches[i].Length)));
}
html.Append(EncryptQueryString(aspxPageMatches[aspxPageMatches.Count-1].Value));
html.Append(sBuffer.Substring(aspxPageMatches[aspxPageMatches.Count-1].Index + aspxPageMatches[aspxPageMatches.Count-1].Length, sBuffer.Length - (aspxPageMatches[aspxPageMatches.Count-1].Index + aspxPageMatches[aspxPageMatches.Count-1].Length)));

byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(html.ToString());
responseStream.Write(data, 0, data.Length);
}
else
{
responseStream.Write(buffer, 0, buffer.Length);
}
}

This works for me.

 
At 11/14/2006 3:56 PM , Anonymous marlon said...

It's not working for me. In the view source option is working but not replace the query string in the address bar.

 
At 2/07/2008 11:35 PM , Anonymous Anonymous said...

Hi,

This HTTPModule is very nice module for Query String encription.

It takes the manual work involved in encryption, decryption of QS elements.

But, When I use it in my application...CSS styles are not coming. Totally NO STYLES are coming.

Please suggest some point to get ride of this problem.

 
At 3/13/2008 8:21 PM , Anonymous Anonymous said...

Doesn't seem to handle Response.Redirect

 

Post a Comment

Links to this post:

Create a Link

<< Home