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
19 comments:
Hi!
The use of cryptography with the help of a secure string is a very useful post its important for all the people of working online.
i am a Web Designing expert and i like you blog and post as well!
Thanks for sharing this information!
cheers!
Hi
Good knowledge and I have found some useful information about Web Solutions.
I'm the sort of guy who passions to seek unused things. Presently I am building my personalized photovoltaic panels. I am doing it all alone without the aid of my men. I am using the internet as the only way to acheive this. I ran across a truly brilliant site that explains how to make solar panels and wind generators. The web site explains all the steps needed for building photovoltaic panels.
I'm not sure about how accurate the info given there iz. If some people over here who have experience with these works can have a look and give your feedback in the site it will be grand and I'd highly treasure it, cause I really lav [URL=http://solar-panel-construction.com/diy-solar-panel-construction-is-it-really-possible]solar panel construction[/URL].
Thanks for reading this. U people are great.
愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。.........................
these query string have strong relation with basic computer programming.
- Tanya
Web Design Firm
A great blog post for sure, enjoyed reading all the useful content shared.... Well done keep up the good work....
Magento Themes
Thanks for the tutorial about Symmetric Cryptography
Chines spammer alround. ;)
Keep updating you blog.
Wow an awesome post writing on the topic of web security, enjoyed reading the information shared. Well done keep posting posts like these....
Hair Transplant Peshawar
Some of the great ways mentioned as to how one can go about and secure his network from attacks and threats of being hacked. Well done great posting..
Shedsafe
I agree totally.
It’s a Good blog and congrats on having good Information, Keep it up! Thanks for sharing your knowledge with us!
Thanks a lot for sharing the tips in going for development.
Nice post! Your content is very valuable to me and just make it as my reference. Keep blogging with new post! Unique and useful to follower.
Wow. Fantastic article, it’s so nice and your blog is very good.
I've learned a lot from your blog here, you’re provided blog is very super perfect i really like it. Keep it up and Thanks you for sharing......!
thanks for sharing these development tips with us
Party Poker Review
That is amazing work you have done keep doing like that and bring some new posts we are waiting for your new posts....
Post a Comment