Advertisement
ZeekoSec

String Encrypt/Decrypt

Mar 27th, 2015
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.36 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Text
  4. Imports System.Text.RegularExpressions
  5. ' This is for password validation
  6. Imports System.Security.Cryptography
  7. Imports System.Configuration
  8. ' This is where the hash functions reside
  9.  
  10. Namespace BullyTracker.Common
  11.    
  12.     Public Class HashEncryption
  13.        
  14.         'public string GenerateHashvalue(string thisPassword)
  15.         '{
  16.         '    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  17.         '    byte[] tmpSource;
  18.         '    byte[] tmpHash;
  19.         '    tmpSource = ASCIIEncoding.ASCII.GetBytes(thisPassword); // Turn password into byte array
  20.         '    tmpHash = md5.ComputeHash(tmpSource);
  21.         '    StringBuilder sOutput = new StringBuilder(tmpHash.Length);
  22.         '    for (int i = 0; i < tmpHash.Length; i++)
  23.         '    {
  24.         '        sOutput.Append(tmpHash[i].ToString("X2"));  // X2 formats to hexadecimal
  25.         '    }
  26.         '    return sOutput.ToString();
  27.         '}
  28.         'public Boolean VerifyHashPassword(string thisPassword, string thisHash)
  29.         '{
  30.         '    Boolean IsValid = false;
  31.         '    string tmpHash = GenerateHashvalue(thisPassword); // Call the routine on user input
  32.         '    if (tmpHash == thisHash) IsValid = true;  // Compare to previously generated hash
  33.         '    return IsValid;
  34.         '}
  35.         Public Function GenerateHashvalue(ByVal toEncrypt As String, ByVal useHashing As Boolean) As String
  36.             Dim keyArray() As Byte
  37.             Dim toEncryptArray() As Byte = UTF8Encoding.UTF8.GetBytes(toEncrypt)
  38.             Dim settingsReader As System.Configuration.AppSettingsReader = New AppSettingsReader
  39.             ' Get the key from config file
  40.             Dim key As String = CType(settingsReader.GetValue("SecurityKey", GetType(String)),String)
  41.             'System.Windows.Forms.MessageBox.Show(key);
  42.             If useHashing Then
  43.                 Dim hashmd5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
  44.                 keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
  45.                 hashmd5.Clear
  46.             Else
  47.                 keyArray = UTF8Encoding.UTF8.GetBytes(key)
  48.             End If
  49.             Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
  50.             tdes.Key = keyArray
  51.             tdes.Mode = CipherMode.ECB
  52.             tdes.Padding = PaddingMode.PKCS7
  53.             Dim cTransform As ICryptoTransform = tdes.CreateEncryptor
  54.             Dim resultArray() As Byte = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
  55.             tdes.Clear
  56.             Return Convert.ToBase64String(resultArray, 0, resultArray.Length)
  57.         End Function
  58.        
  59.         ''' <summary>
  60.         ''' DeCrypt a string using dual encryption method. Return a DeCrypted clear string
  61.         ''' </summary>
  62.         ''' <param name="cipherString">encrypted string</param>
  63.         ''' <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
  64.         ''' <returns></returns>
  65.         Public Function Decrypt(ByVal cipherString As String, ByVal useHashing As Boolean) As String
  66.             Dim keyArray() As Byte
  67.             Dim toEncryptArray() As Byte = Convert.FromBase64String(cipherString)
  68.             Dim settingsReader As System.Configuration.AppSettingsReader = New AppSettingsReader
  69.             'Get your key from config file to open the lock!
  70.             Dim key As String = CType(settingsReader.GetValue("SecurityKey", GetType(String)),String)
  71.             If useHashing Then
  72.                 Dim hashmd5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
  73.                 keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key))
  74.                 hashmd5.Clear
  75.             Else
  76.                 keyArray = UTF8Encoding.UTF8.GetBytes(key)
  77.             End If
  78.             Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
  79.             tdes.Key = keyArray
  80.             tdes.Mode = CipherMode.ECB
  81.             tdes.Padding = PaddingMode.PKCS7
  82.             Dim cTransform As ICryptoTransform = tdes.CreateDecryptor
  83.             Dim resultArray() As Byte = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
  84.             tdes.Clear
  85.             Return UTF8Encoding.UTF8.GetString(resultArray)
  86.         End Function
  87.     End Class
  88. End Namespac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement