|
|
Imports System.Text
|
|
|
Imports System.Security.Cryptography
|
|
|
Imports System.IO
|
|
|
Imports System.Linq
|
|
|
|
|
|
Namespace EncryptString
|
|
|
|
|
|
Public NotInheritable Class StringCipher
|
|
|
Private Sub New()
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
Private Const Keysize As Integer = 256
|
|
|
|
|
|
|
|
|
Private Const DerivationIterations As Integer = 1000
|
|
|
|
|
|
Public Shared Function Encrypt(plainText As String, passPhrase As String, Optional ByVal encoding As Encoding = Nothing) As String
|
|
|
|
|
|
|
|
|
Dim saltStringBytes = Generate256BitsOfRandomEntropy()
|
|
|
Dim ivStringBytes = Generate256BitsOfRandomEntropy()
|
|
|
Dim plainTextBytes As Byte()
|
|
|
If encoding Is Nothing Then
|
|
|
plainTextBytes = Encoding.UTF8.GetBytes(plainText)
|
|
|
Else
|
|
|
plainTextBytes = encoding.GetBytes(plainText)
|
|
|
End If
|
|
|
Using password = New Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)
|
|
|
Dim keyBytes = password.GetBytes(Keysize / 8)
|
|
|
Using symmetricKey = New RijndaelManaged()
|
|
|
symmetricKey.BlockSize = 256
|
|
|
symmetricKey.Mode = CipherMode.CBC
|
|
|
symmetricKey.Padding = PaddingMode.PKCS7
|
|
|
Using encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)
|
|
|
Using memoryStream = New MemoryStream()
|
|
|
Using cryptoStream = New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
|
|
|
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
|
|
|
cryptoStream.FlushFinalBlock()
|
|
|
|
|
|
Dim cipherTextBytes = saltStringBytes
|
|
|
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray()
|
|
|
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray()
|
|
|
memoryStream.Close()
|
|
|
cryptoStream.Close()
|
|
|
Return Convert.ToBase64String(cipherTextBytes)
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Function
|
|
|
|
|
|
Public Shared Function Decrypt(cipherText As String, passPhrase As String, Optional ByVal encoding As Encoding = Nothing) As String
|
|
|
|
|
|
|
|
|
Dim cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText)
|
|
|
|
|
|
Dim saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray()
|
|
|
|
|
|
Dim ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray()
|
|
|
|
|
|
Dim cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray()
|
|
|
|
|
|
Using password = New Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)
|
|
|
Dim keyBytes = password.GetBytes(Keysize / 8)
|
|
|
Using symmetricKey = New RijndaelManaged()
|
|
|
symmetricKey.BlockSize = 256
|
|
|
symmetricKey.Mode = CipherMode.CBC
|
|
|
symmetricKey.Padding = PaddingMode.PKCS7
|
|
|
Using decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)
|
|
|
Using memoryStream = New MemoryStream(cipherTextBytes)
|
|
|
Using cryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
|
|
|
Dim plainTextBytes = New Byte(cipherTextBytes.Length - 1) {}
|
|
|
Dim decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
|
|
|
memoryStream.Close()
|
|
|
cryptoStream.Close()
|
|
|
If encoding Is Nothing Then
|
|
|
Return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
|
|
|
Else
|
|
|
Return encoding.GetString(plainTextBytes, 0, decryptedByteCount)
|
|
|
End If
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Using
|
|
|
End Function
|
|
|
|
|
|
Private Shared Function Generate256BitsOfRandomEntropy() As Byte()
|
|
|
Dim randomBytes = New Byte(31) {}
|
|
|
|
|
|
Using rngCsp = New RNGCryptoServiceProvider()
|
|
|
|
|
|
rngCsp.GetBytes(randomBytes)
|
|
|
End Using
|
|
|
Return randomBytes
|
|
|
End Function
|
|
|
End Class
|
|
|
End Namespace
|
|
|
|
|
|
|