Sunday, August 05, 2007 4:50 PM
kolbis
Screen cast: Using Cryptography Application Block to Encrypt & Decrypt QueryStrings
In this screen cast, I will be show how to use the cryptography application block in order to encrypt and decrypt query string parameters. In addition I am attaching the source code for the screen cast here.
You can download the screen cast from here.
Screen cast walk-through
In this screen cast I will create a web site application that will contain two pages: Default.aspx and Default2.aspx. The first page will ask the user for a product id input, then it will encrypt it and perform a redirection to the second page while passing the encrypted product id. The second page will decrypt the product id and will display it on screen.
Security Helper
The encryption and decryption will be done through a security helper that in turns will invoke methods exposed by the Cryptography application block. Here is the piece of code that invokes those operations:
public static class SecurityHelper
{
public static string Encrypt(string string2Encrypt)
{
byte[] arr =
CryptographyUtility.GetBytesFromHexString(string2Encrypt);
byte[] arr2 =
Cryptographer.EncryptSymmetric("RijndaelManaged", arr);
return CryptographyUtility.GetHexStringFromBytes(arr2);
}
public static string Decrypt(string string2Decrypt)
{
byte[] arr =
CryptographyUtility.GetBytesFromHexString(string2Decrypt);
byte[] arr2 =
Cryptographer.DecryptSymmetric("RijndaelManaged", arr);
return CryptographyUtility.GetHexStringFromBytes(arr2);
}
}
Cryptography Utility
The Cryptography Application Block includes a CryptographyUtility Class that it uses internally. I will use it to get the bytes from HexString and get the HexString back from the bytes.
Cryptographer
The Cryptographer is a Facade for using the Cryptographer application block. By using the Cryptographer Class encrypting and decrypting is very simple.
תגים:Visual Studio, Videos, Tutorials, Enterprise Library 3.1