Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Collections.Generic
- Imports System.Text
- Imports System.Text.RegularExpressions
- ' This is for password validation
- Imports System.Security.Cryptography
- Imports System.Configuration
- ' This is where the hash functions reside
- Namespace BullyTracker.Common
- Public Class HashEncryption
- 'public string GenerateHashvalue(string thisPassword)
- '{
- ' MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- ' byte[] tmpSource;
- ' byte[] tmpHash;
- ' tmpSource = ASCIIEncoding.ASCII.GetBytes(thisPassword); // Turn password into byte array
- ' tmpHash = md5.ComputeHash(tmpSource);
- ' StringBuilder sOutput = new StringBuilder(tmpHash.Length);
- ' for (int i = 0; i < tmpHash.Length; i++)
- ' {
- ' sOutput.Append(tmpHash[i].ToString("X2")); // X2 formats to hexadecimal
- ' }
- ' return sOutput.ToString();
- '}
- 'public Boolean VerifyHashPassword(string thisPassword, string thisHash)
- '{
- ' Boolean IsValid = false;
- ' string tmpHash = GenerateHashvalue(thisPassword); // Call the routine on user input
- ' if (tmpHash == thisHash) IsValid = true; // Compare to previously generated hash
- ' return IsValid;
- '}
- Public Function GenerateHashvalue(ByVal toEncrypt As String, ByVal useHashing As Boolean) As String
- Dim keyArray() As Byte
- Dim toEncryptArray() As Byte = UTF8Encoding.UTF8.GetBytes(toEncrypt)
- Dim settingsReader As System.Configuration.AppSettingsReader = New AppSettingsReader
- ' Get the key from config file
- Dim key As String = CType(settingsReader.GetValue("SecurityKey", GetType(String)),String)
- 'System.Windows.Forms.MessageBox.Show(key);
- If useHashing Then
- Dim hashmd5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
- keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
- hashmd5.Clear
- Else
- keyArray = UTF8Encoding.UTF8.GetBytes(key)
- End If
- Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
- tdes.Key = keyArray
- tdes.Mode = CipherMode.ECB
- tdes.Padding = PaddingMode.PKCS7
- Dim cTransform As ICryptoTransform = tdes.CreateEncryptor
- Dim resultArray() As Byte = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
- tdes.Clear
- Return Convert.ToBase64String(resultArray, 0, resultArray.Length)
- End Function
- ''' <summary>
- ''' DeCrypt a string using dual encryption method. Return a DeCrypted clear string
- ''' </summary>
- ''' <param name="cipherString">encrypted string</param>
- ''' <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
- ''' <returns></returns>
- Public Function Decrypt(ByVal cipherString As String, ByVal useHashing As Boolean) As String
- Dim keyArray() As Byte
- Dim toEncryptArray() As Byte = Convert.FromBase64String(cipherString)
- Dim settingsReader As System.Configuration.AppSettingsReader = New AppSettingsReader
- 'Get your key from config file to open the lock!
- Dim key As String = CType(settingsReader.GetValue("SecurityKey", GetType(String)),String)
- If useHashing Then
- Dim hashmd5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
- keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
- hashmd5.Clear
- Else
- keyArray = UTF8Encoding.UTF8.GetBytes(key)
- End If
- Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
- tdes.Key = keyArray
- tdes.Mode = CipherMode.ECB
- tdes.Padding = PaddingMode.PKCS7
- Dim cTransform As ICryptoTransform = tdes.CreateDecryptor
- Dim resultArray() As Byte = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
- tdes.Clear
- Return UTF8Encoding.UTF8.GetString(resultArray)
- End Function
- End Class
- End Namespac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement