Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class SimpleAes
- Implements IDisposable
- ''' <summary>
- ''' Initialization vector length in bytes.
- ''' </summary>
- Private Const IvBytes As Integer = 16
- ''' <summary>
- ''' Must be exactly 16, 24 or 32 characters long.
- ''' </summary>
- Private Shared ReadOnly Key As Byte() = Convert.FromBase64String("FILL ME WITH 16, 24 OR 32 CHARS")
- Private ReadOnly _encoder As UTF8Encoding
- Private ReadOnly _encryptor As ICryptoTransform
- Private ReadOnly _rijndael As RijndaelManaged
- Public Sub New()
- _rijndael = New RijndaelManaged() With { _
- Key .Key = Key _
- }
- _rijndael.GenerateIV()
- _encryptor = _rijndael.CreateEncryptor()
- _encoder = New UTF8Encoding()
- End Sub
- Public Function Decrypt(encrypted As String) As String
- Return _encoder.GetString(Decrypt(Convert.FromBase64String(encrypted)))
- End Function
- Public Sub Dispose()
- _rijndael.Dispose()
- _encryptor.Dispose()
- End Sub
- Public Function Encrypt(unencrypted As String) As String
- Return Convert.ToBase64String(Encrypt(_encoder.GetBytes(unencrypted)))
- End Function
- Private Function Decrypt(buffer As Byte()) As Byte()
- ' IV is prepended to cryptotext
- Dim iv As Byte() = buffer.Take(IvBytes).ToArray()
- Using decryptor As ICryptoTransform = _rijndael.CreateDecryptor(_rijndael.Key, iv)
- Return decryptor.TransformFinalBlock(buffer, IvBytes, buffer.Length - IvBytes)
- End Using
- End Function
- Private Function Encrypt(buffer As Byte()) As Byte()
- ' Prepend cryptotext with IV
- Dim inputBuffer As Byte() = _rijndael.IV.Concat(buffer).ToArray()
- Return _encryptor.TransformFinalBlock(inputBuffer, IvBytes, buffer.Length)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement