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.