Code:
Module Data
Private Key() As Byte = {26, 15, 5, 46, 98, 21, 2, 36, 19, 4, 22, 50, 42, 14, 24, 18, 34, 16, 82, 76, 65, 91, 54, 78}
Private InitializationVector() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
Public Function Encrypt(ByVal Text As String) As Byte()
'Create UTF8 encoder to convert text into byte array'
Dim UTF8Encoder As New System.Text.UTF8Encoding
Dim TextBytes As Byte() = UTF8Encoder.GetBytes(Text)
'Create triple DES service provider'
Dim TDESProvider As New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateEncryptor(Key, InitializationVector)
'Create memory stream to store encrypted data'
Dim EncryptionStream As New System.IO.MemoryStream
Dim CryptoStream As New System.Security.Cryptography.CryptoStream(EncryptionStream, CryptoTransform, Security.Cryptography.CryptoStreamMode.Write)
'Write the encrypted information to the stream. Flush the information when done to ensure everything is out of the buffer'
CryptoStream.Write(TextBytes, 0, TextBytes.Length)
CryptoStream.FlushFinalBlock()
EncryptionStream.Position = 0
'Read the stream back into a Byte array and return it to the calling method'
Dim Result(EncryptionStream.Length - 1) As Byte
EncryptionStream.Read(Result, 0, EncryptionStream.Length)
CryptoStream.Close()
Encrypt = Result
End Function
Public Function Decrypt(ByVal TextBytes() As Byte) As String
'Create UTF8 Encoder to convert bytes back to string'
Dim UTF8Encoder As New System.Text.UTF8Encoding()
'Create triple DES service provider'
Dim TDESProvider As New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateEncryptor(Key, InitializationVector)
'Create memory stream to store decrypted data'
Dim DecryptionStream As New System.IO.MemoryStream
Dim CryptoStream As New System.Security.Cryptography.CryptoStream(DecryptionStream, CryptoTransform, Security.Cryptography.CryptoStreamMode.Write)
CryptoStream.Write(TextBytes, 0, TextBytes.Length)
CryptoStream.FlushFinalBlock()
DecryptionStream.Position = 0
'Read contents of memory stream, convert back to a string and return result'
Dim result(DecryptionStream.Length - 1) As Byte
DecryptionStream.Read(result, 0, DecryptionStream.Length)
DecryptionStream.Close()
Return UTF8Encoder.GetString(result)
End Function
End Module
The problem im having is it is not decrypting the data correctly. For example if i encrypt the word "Hello" it is encrypted into a byte array correctly. Then when i decrypt it it returns the following string: ":��q��;&S���o��"