SHARE
TWEET

Untitled

a guest Jan 26th, 2020 220 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Text
  2. Imports System.Security.Cryptography
  3. Public Class Utils
  4.  
  5.     Public Shared Function GetLogFilePath() As String
  6.         Return IO.Path.Combine(Environment.CurrentDirectory, "Log.txt")
  7.     End Function
  8.  
  9.  
  10.  
  11.  
  12.     Public Shared Function DecryptString(EncryptedString As String) As String
  13.         If String.IsNullOrEmpty(EncryptedString) Then
  14.             Return String.Empty
  15.         Else
  16.             Return Decrypt(EncryptedString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
  17.         End If
  18.     End Function
  19.  
  20.     Public Shared Function EncryptString(PlainString As String) As String
  21.         If String.IsNullOrEmpty(PlainString) Then
  22.             Return String.Empty
  23.         Else
  24.             Return Encrypt(PlainString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
  25.         End If
  26.     End Function
  27.  
  28.     Public Shared Function Encrypt(ByVal plainText As String, _
  29.                                    ByVal passPhrase As String, _
  30.                                    ByVal saltValue As String, _
  31.                                     ByVal passwordIterations As Integer, _
  32.                                    ByVal initVector As String, _
  33.                                    ByVal keySize As Integer) _
  34.                            As String
  35.  
  36.         Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
  37.         Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
  38.         Dim plainTextBytes As Byte() = Encoding.ASCII.GetBytes(plainText)
  39.         Dim password As New Rfc2898DeriveBytes(passPhrase, _
  40.                                            saltValueBytes, _
  41.                                            passwordIterations)
  42.         Dim keyBytes As Byte() = password.GetBytes(CInt(keySize / 8))
  43.         Dim symmetricKey As New AesCryptoServiceProvider
  44.         symmetricKey.Mode = CipherMode.CBC
  45.         Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
  46.         Using memoryStream As New IO.MemoryStream()
  47.             Using cryptoStream As New CryptoStream(memoryStream, _
  48.                                             encryptor, _
  49.                                             CryptoStreamMode.Write)
  50.                 cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
  51.                 cryptoStream.FlushFinalBlock()
  52.                 Dim cipherTextBytes As Byte() = memoryStream.ToArray()
  53.                 memoryStream.Close()
  54.                 cryptoStream.Close()
  55.                 Return Convert.ToBase64String(cipherTextBytes)
  56.             End Using
  57.         End Using
  58.     End Function
  59.  
  60.     Public Shared Function Decrypt(ByVal cipherText As String, _
  61.                                    ByVal passPhrase As String, _
  62.                                    ByVal saltValue As String, _
  63.                                     ByVal passwordIterations As Integer, _
  64.                                    ByVal initVector As String, _
  65.                                    ByVal keySize As Integer) _
  66.                            As String
  67.  
  68.         Dim initVectorBytes As Byte()
  69.         initVectorBytes = Encoding.ASCII.GetBytes(initVector)
  70.  
  71.         Dim saltValueBytes As Byte()
  72.         saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
  73.  
  74.         Dim cipherTextBytes As Byte()
  75.         cipherTextBytes = Convert.FromBase64String(cipherText)
  76.  
  77.         Dim password As New Rfc2898DeriveBytes(passPhrase, _
  78.                                            saltValueBytes, _
  79.                                            passwordIterations)
  80.  
  81.         Dim keyBytes As Byte()
  82.         keyBytes = password.GetBytes(CInt(keySize / 8))
  83.  
  84.         Dim symmetricKey As New AesCryptoServiceProvider
  85.         symmetricKey.Mode = CipherMode.CBC
  86.  
  87.         Dim decryptor As ICryptoTransform
  88.         decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
  89.  
  90.         Dim memoryStream As IO.MemoryStream
  91.         memoryStream = New IO.MemoryStream(cipherTextBytes)
  92.  
  93.         Dim cryptoStream As CryptoStream
  94.         cryptoStream = New CryptoStream(memoryStream, _
  95.                                         decryptor, _
  96.                                         CryptoStreamMode.Read)
  97.  
  98.         Dim plainTextBytes As Byte()
  99.         ReDim plainTextBytes(cipherTextBytes.Length)
  100.  
  101.         Dim decryptedByteCount As Integer
  102.         decryptedByteCount = cryptoStream.Read(plainTextBytes, _
  103.                                                0, _
  104.                                                plainTextBytes.Length)
  105.  
  106.         memoryStream.Close()
  107.         cryptoStream.Close()
  108.  
  109.         Dim plainText As String
  110.         plainText = Encoding.ASCII.GetString(plainTextBytes, _
  111.                                             0, _
  112.                                             decryptedByteCount)
  113.  
  114.         Return plainText
  115.     End Function
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top