I have constructed the following class from examples on MSDN and elsewhere, for some reason when decrypting I seem to end up with extra bytes and cannot for the life of me figure out where they are coming from.
I know I am using a block cipher, and my resulting encrypted data can end up with a different byte count, however after decrypting it should be a byte for byte match of the original.
I have to be overlooking something, but it is all blending together and I need another set of eyes to have a look.
I know I am using a block cipher, and my resulting encrypted data can end up with a different byte count, however after decrypting it should be a byte for byte match of the original.
I have to be overlooking something, but it is all blending together and I need another set of eyes to have a look.
Code:
#Region " Imports "
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
#End Region
Public Class Encryption
Private Key() As Byte
Private CryptoEngine As New AesManaged
' ----- Create initialization vector.
Private IV() As Byte = Encoding.ASCII.GetBytes(Guid.NewGuid.ToString.Replace("-", Nothing).Substring(16))
''' <summary>
''' Create a new instance of the encryption class using specified passphrase to en/decrypt.
''' </summary>
''' <param name="PassPhrase">Passphrase to use when en/decrypting data.</param>
Public Sub New(ByVal PassPhrase As String)
Dim HashEngine As New MD5CryptoServiceProvider
Key = HashEngine.ComputeHash(Encoding.ASCII.GetBytes(PassPhrase))
End Sub
''' <summary>
''' Converts plain text into base64 encoded 128bit Rijndael(AES) encrypted string.
''' </summary>
''' <param name="PlainText">Text to be encrypted</param>
''' <returns>String</returns>
Public Overloads Function Encrypt(ByVal PlainText As String) As String
Return Convert.ToBase64String(Encrypt(Encoding.ASCII.GetBytes(PlainText)))
End Function
''' <summary>
''' Converts base64 encoded 128bit Rijndael(AES) encrypted string into plain text.
''' </summary>
''' <param name="Encrypted">Text to be decrypted</param>
''' <returns>String</returns>
Public Overloads Function Decrypt(ByVal Encrypted As String) As String
Return Encoding.ASCII.GetString(Decrypt(Convert.FromBase64String(Encrypted))).Replace(vbNullChar, Nothing)
End Function
''' <summary>
''' Encrypts array of Byte() using 128bit Rijndael(AES).
''' </summary>
''' <param name="Data">Byte array to encrypt</param>
''' <returns>Encrypted byte array</returns>
Public Overloads Function Encrypt(ByVal Data() As Byte) As Byte()
Try
Dim Tmp_Stream As New MemoryStream()
Dim Transform As ICryptoTransform
Transform = CryptoEngine.CreateEncryptor(Key, IV)
Dim EncryptionStream As CryptoStream
EncryptionStream = New CryptoStream(Tmp_Stream, Transform, CryptoStreamMode.Write)
With EncryptionStream
.Write(Data, 0, Data.Length)
.FlushFinalBlock()
End With
Tmp_Stream.Close()
EncryptionStream.Close()
Return Tmp_Stream.ToArray
Catch ex As Exception
Debug.WriteLine(ex.Message)
Return Nothing
End Try
End Function
''' <summary>
''' Decrypts array of Byte() using 128bit Rijndael(AES).
''' </summary>
''' <param name="Data">Byte array to decrypt</param>
''' <returns>decrypted byte array</returns>
Public Overloads Function Decrypt(ByVal Data() As Byte) As Byte()
Try
Dim Tmp_Bytes(Data.Length - 1) As Byte
Dim Tmp_Stream As New MemoryStream(Data)
Dim Transform As ICryptoTransform
Transform = CryptoEngine.CreateDecryptor(Key, IV)
Dim EncryptionStream As CryptoStream
EncryptionStream = New CryptoStream(Tmp_Stream, Transform, CryptoStreamMode.Read)
EncryptionStream.Read(Tmp_Bytes, 0, Tmp_Bytes.Length)
Tmp_Stream.Close()
EncryptionStream.Close()
Return Tmp_Bytes
Catch ex As Exception
Debug.WriteLine(ex.Message)
Return Nothing
End Try
End Function
End Class
Last edited: