Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Text
- Imports System.Security.Cryptography
- Public Class Form1
- Private Sub cmdEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEncrypt.Click
- txtCipherText.Text = Encrypt(txtPlainText.Text, txtPassPhrase.Text, "s@1tValue", "SHA1", 2, "@1B2c3D4e5F6g7H8", 256)
- End Sub
- Private Sub cmdDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecrypt.Click
- txtPlainText.Text = Decrypt(txtCipherText.Text, txtPassPhrase.Text, "s@1tValue", "SHA1", 2, "@1B2c3D4e5F6g7H8", 256)
- End Sub
- Private Function Encrypt(ByVal plainText As String, ByVal passPhrase As String, ByVal saltValue As String, ByVal hashAlgorithm 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 plainTextBytes As Byte()
- plainTextBytes = Encoding.UTF8.GetBytes(plainText)
- Dim password As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations)
- Dim keyBytes As Byte()
- keyBytes = password.GetBytes(CInt(keySize / 8))
- Dim symmetricKey As RijndaelManaged
- symmetricKey = New RijndaelManaged()
- symmetricKey.Mode = CipherMode.CBC
- Dim encryptor As ICryptoTransform
- encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
- Dim memoryStream As MemoryStream
- memoryStream = New MemoryStream()
- Dim cryptoStream As CryptoStream
- cryptoStream = New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
- cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
- cryptoStream.FlushFinalBlock()
- Dim cipherTextBytes As Byte()
- cipherTextBytes = memoryStream.ToArray()
- memoryStream.Close()
- cryptoStream.Close()
- Dim cipherText As String
- cipherText = Convert.ToBase64String(cipherTextBytes)
- Encrypt = cipherText
- End Function
- Private Function Decrypt(ByVal cipherText As String, ByVal passPhrase As String, ByVal saltValue As String, ByVal hashAlgorithm As String, ByVal passwordIterations As Integer, ByVal initVector As String, ByVal keySize As Integer) As String
- On Error Resume Next
- 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 Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations)
- Dim keyBytes As Byte()
- keyBytes = password.GetBytes(CInt(keySize / 8))
- Dim symmetricKey As RijndaelManaged
- symmetricKey = New RijndaelManaged()
- symmetricKey.Mode = CipherMode.CBC
- Dim decryptor As ICryptoTransform
- decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
- Dim memoryStream As MemoryStream
- memoryStream = New 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.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
- Decrypt = plainText
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement