#Region " Imports " Imports System.IO Imports System.Security.Cryptography #End Region Public Class Security #Region " Instance Fields " 'DES Key Value: Private Shared _key As Byte() = New Byte(7) {42, 16, 93, 156, 78, 4, 218, 32} 'DES Initialization Vector ("salt"): Private Shared _IV As Byte() = New Byte(7) {55, 103, 246, 79, 35, 99, 157, 3} #End Region #Region " Properties " #End Region #Region " Public Methods " 'Provides DES encryption: ' First argument "textIn" species the cleartext string to be encrypted. ' Returns an encrypted string. Public Shared Function Encrypt(ByVal textIn As String) As String Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider Dim ms As MemoryStream = New MemoryStream Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateEncryptor(_key, _IV), CryptoStreamMode.Write) Dim sw As StreamWriter = New StreamWriter(cs) sw.Write(textIn) sw.Flush() cs.FlushFinalBlock() ms.Flush() Encrypt = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length) End Function 'Provides DES decryption: ' First argument "textIn" species the encrypted string to be decrypted. ' Returns the decrypted string. Public Shared Function Decrypt(ByVal textIn As String) As String Dim buffer As Byte() = Convert.FromBase64String(textIn) Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider Dim ms As MemoryStream = New MemoryStream(buffer) Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateDecryptor(_key, _IV), CryptoStreamMode.Read) Dim sr As StreamReader = New StreamReader(cs) Decrypt = sr.ReadToEnd() End Function 'Generates MD5 hash of a text string: ' First argument "textIn" specifies the string to hash. ' Returns the hash of the string. Public Shared Function CreateHash(ByVal textIn As String) As String 'Create an encoding object to ensure the encoding standard for the source text Dim ue As New UnicodeEncoding() 'Retrieve a byte array based on the source text Dim byteSourceText() As Byte = ue.GetBytes(textIn) 'Instantiate an MD5 Provider object Dim md5 As New MD5CryptoServiceProvider() 'Compute the hash value from the source Dim byteHash() As Byte = md5.ComputeHash(byteSourceText) 'And convert it to String format for return CreateHash = Convert.ToBase64String(byteHash) End Function 'Generates MD5 hash of a ByteArray: ' First argument "bytesIn" specifies the byteArray to hash. ' Returns the hash of the string. Public Shared Function CreateHash(ByVal bytesIn As Byte()) As String 'Retrieve a byte array based on the source text Dim byteSourceText() As Byte = bytesIn 'Instantiate an MD5 Provider object Dim md5 As New MD5CryptoServiceProvider() 'Compute the hash value from the source Dim byteHash() As Byte = md5.ComputeHash(byteSourceText) 'And convert it to String format for return CreateHash = Convert.ToBase64String(byteHash) End Function #End Region #Region " Private Methods " 'Generate salt: ' Returns a high entropy random number. Private Shared Function ReturnSalt() As String Dim saltBytes() As Byte = New Byte(100) {} Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider rng.GetNonZeroBytes(saltBytes) ReturnSalt = StrConv(saltBytes(100), VbStrConv.None) End Function #End Region End Class