Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Text
- Imports System.Security.Cryptography
- Imports System.IO
- Namespace My
- Public Class strCrypto
- ' This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
- ' This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
- ' 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
- Private Const initVector As String = "r5dm5fgm24mfhfku"
- Private Const passPhrase As String = "yourpassphrase"
- ' email password encryption password
- ' This constant is used to determine the keysize of the encryption algorithm.
- Private Const keysize As Integer = 256
- Public Shared Function encryptString(ByVal plainText As String) As String
- 'if the plaintext is empty or null string just return an empty string
- If ((plainText = "") _
- OrElse (plainText Is Nothing)) Then
- Return ""
- End If
- Dim initVectorBytes() As Byte = Encoding.UTF8.GetBytes(initVector)
- Dim plainTextBytes() As Byte = Encoding.UTF8.GetBytes(plainText)
- Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, Nothing)
- Dim keyBytes() As Byte = password.GetBytes((keysize / 8))
- Dim symmetricKey As RijndaelManaged = New RijndaelManaged
- symmetricKey.Mode = CipherMode.CBC
- Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
- Dim memoryStream As MemoryStream = New MemoryStream
- Dim cryptoStream As CryptoStream = 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 Function
- Public Shared Function decryptString(ByVal cipherText As String) As String
- 'if the ciphertext is empty or null string just return an empty string
- If ((cipherText = "") _
- OrElse (cipherText Is Nothing)) Then
- Return ""
- End If
- Dim initVectorBytes() As Byte = Encoding.ASCII.GetBytes(initVector)
- Dim cipherTextBytes() As Byte = Convert.FromBase64String(cipherText)
- Dim password As PasswordDeriveBytes = New PasswordDeriveBytes(passPhrase, Nothing)
- Dim keyBytes() As Byte = password.GetBytes((keysize / 8))
- Dim symmetricKey As RijndaelManaged = New RijndaelManaged
- symmetricKey.Mode = CipherMode.CBC
- Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
- Dim memoryStream As MemoryStream = New MemoryStream(cipherTextBytes)
- Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
- Dim plainTextBytes() As Byte = New Byte((cipherTextBytes.Length) - 1) {}
- Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
- memoryStream.Close
- cryptoStream.Close
- Return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement