Friday, June 17, 2005

Encrypting QueryStrings

The whole trick to this working is that the QueryString is encrypted and decrypted with the same private key. This is the secret key - if anyone gets a hold of your key, they can decrypt the data themselves, so keep it a secret!

We're going to use a hard-to-crack 8 byte key, !#$a54?3, to keep parts of our QueryString secret.

public string encryptQueryString(string strQueryString)
{
ExtractAndSerialize.Encryption64 oES = new ExtractAndSerialize.Encryption64();
return oES.Encrypt(strQueryString,"!#$a54?3");
}

public string decryptQueryString(string strQueryString)
{
ExtractAndSerialize.Encryption64 oES = new ExtractAndSerialize.Encryption64();
return oES.Decrypt(strQueryString,"!#$a54?3");
}

If we wanted to encrypt our QueryString on our first page, we could do something like this:

string strValues = "search term";
string strURL = "http://yoursite.com?search=" + encryptQueryString(strValues);
Response.Redirect(strURL);

Happy coding!

No comments: