Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Shared Function GetLogFilePath() As String
- Return IO.Path.Combine(Environment.CurrentDirectory, "Log.txt")
- End Function
- Public Shared Function DecryptString(EncryptedString As String) As String
- If String.IsNullOrEmpty(EncryptedString) Then
- Return String.Empty
- Else
- Return Decrypt(EncryptedString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
- End If
- End Function
- Public Shared Function EncryptString(PlainString As String) As String
- If String.IsNullOrEmpty(PlainString) Then
- Return String.Empty
- Else
- Return Encrypt(PlainString, "N3st22", "88552299", 2, "464R5DFA5DL6LE28", 256)
- End If
- End Function
- Public Shared Function Encrypt(ByVal plainText As String, _
- ByVal passPhrase As String, _
- ByVal saltValue As String, _
- ByVal passwordIterations As Integer, _
- ByVal initVector As String, _
- ByVal keySize As Integer) _
- As String
- Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
- Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)
- Dim plainTextBytes As Byte() = Encoding.ASCII.GetBytes(plainText)
- Dim password As New Rfc2898DeriveBytes(passPhrase, _
- saltValueBytes, _
- passwordIterations)
- Dim keyBytes As Byte() = password.GetBytes(CInt(keySize / 8))
- Dim symmetricKey As New AesCryptoServiceProvider
- symmetricKey.Mode = CipherMode.CBC
- Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
- Using memoryStream As New IO.MemoryStream()
- Using cryptoStream As New CryptoStream(memoryStream, _
- encryptor, _
- CryptoStreamMode.Write)
- cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
- cryptoStream.FlushFinalBlock()
- Dim cipherTextBytes As Byte() = memoryStream.ToArray()
- memoryStream.Close()
- cryptoStream.Close()
- Return Convert.ToBase64String(cipherTextBytes)
- End Using
- End Using
- End Function
- Public Shared Function Decrypt(ByVal cipherText As String, _
- ByVal passPhrase As String, _
- ByVal saltValue As String, _
- ByVal passwordIterations As Integer, _
- ByVal initVector As String, _
- ByVal keySize As Integer) _
- As String
- Dim initVectorBytes As Byte()
- initVectorBytes = Encoding.ASCII.GetBytes(initVector)
- Dim saltValueBytes As Byte()
- saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
- Dim cipherTextBytes As Byte()
- cipherTextBytes = Convert.FromBase64String(cipherText)
- Dim password As New Rfc2898DeriveBytes(passPhrase, _
- saltValueBytes, _
- passwordIterations)
- Dim keyBytes As Byte()
- keyBytes = password.GetBytes(CInt(keySize / 8))
- Dim symmetricKey As New AesCryptoServiceProvider
- symmetricKey.Mode = CipherMode.CBC
- Dim decryptor As ICryptoTransform
- decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
- Dim memoryStream As IO.MemoryStream
- memoryStream = New IO.MemoryStream(cipherTextBytes)
- Dim cryptoStream As CryptoStream
- cryptoStream = New CryptoStream(memoryStream, _
- decryptor, _
- CryptoStreamMode.Read)
- Dim plainTextBytes As Byte()
- ReDim plainTextBytes(cipherTextBytes.Length)
- Dim decryptedByteCount As Integer
- decryptedByteCount = cryptoStream.Read(plainTextBytes, _
- 0, _
- plainTextBytes.Length)
- memoryStream.Close()
- cryptoStream.Close()
- Dim plainText As String
- plainText = Encoding.ASCII.GetString(plainTextBytes, _
- 0, _
- decryptedByteCount)
- Return plainText
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement