Mornin', I'm trying to just get basic encryption working using System.Security.Cryptography.RjindaelManaged. I have google for this error and cannot find the problem, or what I am doing wrong. All I am attempting to do is encrypt a string, and then decrypt a string.
Following is my code, and any help would be appreciated.
Imports System.Security.Cryptography
Imports System.Text
Public rj As New RijndaelManaged
Try
rj.Padding = PaddingMode.None
rj.GenerateKey()
rj.GenerateIV()
Dim curProvider As New AesCryptoServiceProvider
Dim curEncryptor As ICryptoTransform
Dim memEncStream As New MemoryStream
Dim cryptoEncStream As CryptoStream
curEncryptor = curProvider.CreateEncryptor(rj.Key, rj.IV)
cryptoEncStream = New CryptoStream(memEncStream, curEncryptor, CryptoStreamMode.Write)
Dim startingBytes() As Byte = Encoding.ASCII.GetBytes("This is a test")
Debug.Print("before length: " & startingBytes.Length)
Debug.Print("before text: " & Encoding.ASCII.GetString(startingBytes))
cryptoEncStream.Write(startingBytes, 0, startingBytes.Length)
cryptoEncStream.FlushFinalBlock()
memEncStream.Position = 0
Dim theBytes(memEncStream.Length) As Byte
memEncStream.Read(theBytes, 0, memEncStream.Length)
memEncStream.Flush()
memEncStream.Close()
cryptoEncStream.Close()
Debug.Print("How long? " & theBytes.Length)
Debug.Print("Data: " & Encoding.ASCII.GetString(theBytes))
Dim curDecryptor As ICryptoTransform
curDecryptor = curProvider.CreateDecryptor(rj.Key, rj.IV)
Dim memDecStream As New MemoryStream
Dim cryptoDecStream As CryptoStream
curDecryptor = curProvider.CreateDecryptor(rj.Key, rj.IV)
cryptoDecStream = New CryptoStream(memDecStream, curDecryptor, CryptoStreamMode.Write)
Dim endingBytes() As Byte = theBytes
Debug.Print("before length: " & theBytes.Length)
Debug.Print("before text: " & Encoding.ASCII.GetString(theBytes))
cryptoDecStream.Write(theBytes, 0, theBytes.Length)
cryptoDecStream.FlushFinalBlock()
memDecStream.Position = 0
Dim endBytes(memDecStream.Length) As Byte
memDecStream.Read(theBytes, 0, memDecStream.Length)
memDecStream.Flush()
memDecStream.Close()
cryptoEncStream.Close()
Debug.Print("How long? " & endBytes.Length)
Debug.Print("Data: " & Encoding.ASCII.GetString(endBytes))
Catch ex As Exception
Debug.Print(ex.ToString)
End Try