It is important to secure information on the web specially when transferring data from one page to another. Some information you may not want to make it readable by end user. Microsoft .NET provides Cryptography namespace which can be easily utilized for this purpose. The following example contains a simple class containing Encryption and Decryption method. It supports 2 types – DESC and Rijndael. Symmetric type and encryption key can be configured easily through .config file.
Imports SystemImports System.IOImports System.TextImports System.Security.CryptographyImports System.Configuration''' <summary>''' Custom Encryption Class''' Author: Vishwa''' Date :05/28/08''' Purpose: To Encrypt and Decrypt the Data''' Design Pattern : Singleton''' </summary>''' <remarks>''' This class implements a Base 64 encryption and decryption Using Symetric''' IMPT: Config File will contain 2 keys and its value - SymmetricType and SymmetricKey''' </remarks>Public NotInheritable Class RegCryptographerPrivate Shared _instance As New RegCryptographer#Region "Constructor"Private Sub New()End Sub#End Region#Region "Public Methods"Public Shared Function DecryptQueryString(ByVal stringToDecrypt As String) As StringReturn DecryptSymmetric(stringToDecrypt.Replace(" ", "+"))End FunctionPublic Shared Function EncryptQueryString(ByVal stringToEncrypt As String) As StringReturn EncryptSymmetric(stringToEncrypt)End Function#End Region#Region "Custom Cryptography"Private Shared _key() As Byte = {}Private Shared _IV() As Byte = {}Private Shared _symmetricKey As String = String.Empty#Region "Rijndael Encryption/Decryption"Public Shared Function DecryptSymmetric(ByVal stringToDecrypt As String) As StringIf SymmetricType = "Rijndael" ThenReturn DecryptSymmetricRijndael(stringToDecrypt)ElseReturn DecryptSymmetricDESC(stringToDecrypt)End IfEnd FunctionPublic Shared Function EncryptSymmetric(ByVal stringToEncrypt As String) As StringIf SymmetricType = "Rijndael" ThenReturn EncryptSymmetricRijndael(stringToEncrypt)ElseReturn EncryptSymmetricDESC(stringToEncrypt)End IfEnd FunctionPrivate Shared Function DecryptSymmetricRijndael(ByVal stringToDecrypt As String) As StringTryDim inputByteArray(stringToDecrypt.Length) As ByteDim rijndael As New RijndaelManaged()rijndael.GenerateIV()'rijndael.GenerateKey()'_key = rijndael.Key_IV = rijndael.IVrijndael.KeySize = 128rijndael.BlockSize = 128rijndael.Mode = CipherMode.ECBrijndael.Padding = PaddingMode.PKCS7_key = System.Text.Encoding.UTF32.GetBytes(SymmetricKey)inputByteArray = Convert.FromBase64String(stringToDecrypt)Dim decryptor As ICryptoTransform = rijndael.CreateDecryptor(_key, _IV)Dim msDecrypt As New MemoryStream()Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write)csDecrypt.Write(inputByteArray, 0, inputByteArray.Length)csDecrypt.FlushFinalBlock()Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8Return encoding.GetString(msDecrypt.ToArray())Catch e As ExceptionReturn e.MessageEnd TryEnd FunctionPrivate Shared Function EncryptSymmetricRijndael(ByVal stringToEncrypt As String) As StringTryDim rijndael As New RijndaelManaged()rijndael.GenerateIV()'rijndael.GenerateKey()'_key = rijndael.Key_IV = rijndael.IV_key = System.Text.Encoding.UTF32.GetBytes(SymmetricKey)rijndael.KeySize = 128rijndael.BlockSize = 128rijndael.Mode = CipherMode.ECBrijndael.Padding = PaddingMode.PKCS7Dim encryptor As ICryptoTransform = rijndael.CreateEncryptor(_key, _IV)Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)Dim msEncrypt As New MemoryStream()Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)csEncrypt.Write(inputByteArray, 0, inputByteArray.Length)csEncrypt.FlushFinalBlock()Return Convert.ToBase64String(msEncrypt.ToArray())Catch e As ExceptionReturn e.MessageEnd TryEnd Function#End Region#Region "DESC Encryption/DeCryption"Private Shared _symmetricType As String = String.EmptyPrivate Shared Function DecryptSymmetricDESC(ByVal stringToDecrypt As String) As StringDim inputByteArray(stringToDecrypt.Length) As ByteTryinputByteArray = Convert.FromBase64String(stringToDecrypt)Dim des As New DESCryptoServiceProvider()des.GenerateIV()_IV = des.IV_key = System.Text.Encoding.UTF8.GetBytes(SymmetricKey)Dim ms As New MemoryStream()Dim cs As New CryptoStream(ms, des.CreateDecryptor(_key, _IV), CryptoStreamMode.Write)cs.Write(inputByteArray, 0, inputByteArray.Length)cs.FlushFinalBlock()Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8Return encoding.GetString(ms.ToArray())Catch e As ExceptionReturn e.MessageEnd TryEnd FunctionPrivate Shared Function EncryptSymmetricDESC(ByVal stringToEncrypt As String) As StringTryDim des As New DESCryptoServiceProvider()des.GenerateIV()_IV = des.IV_key = System.Text.Encoding.UTF8.GetBytes(SymmetricKey)Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)Dim ms As New MemoryStream()Dim cs As New CryptoStream(ms, des.CreateEncryptor(_key, _IV), CryptoStreamMode.Write)cs.Write(inputByteArray, 0, inputByteArray.Length)cs.FlushFinalBlock()Return Convert.ToBase64String(ms.ToArray())Catch e As ExceptionReturn e.MessageEnd TryEnd Function#End Region#Region "Properties"Private Shared ReadOnly Property SymmetricType() As StringGetIf String.IsNullOrEmpty(_symmetricType) ThenTry_symmetricType = ConfigurationManager.AppSettings.Item("SymmetricType")Catch ex As Exception_symmetricType = "Rijndael"End TryIf String.IsNullOrEmpty(_symmetricType) Then _symmetricType = "Rijndael"End IfReturn _symmetricTypeEnd GetEnd PropertyPrivate Shared ReadOnly Property SymmetricKey() As StringGetIf String.IsNullOrEmpty(_symmetricKey) ThenTry_symmetricKey = ConfigurationManager.AppSettings.Item("SymmetricKey")Catch ex As Exception_symmetricKey = "DIffTkey"End TryIf String.IsNullOrEmpty(_symmetricKey) Then _symmetricKey = "DIffTkey"End IfReturn Left(_symmetricKey, 8)End GetEnd Property#End Region#End RegionEnd Class