Доброе утро, я пытаюсь заставить работать простое шифрование с использованием System.Security.Cryptography.RjindaelManaged. Я имею Google для этой ошибки и не могу найти проблему, или что я делаю неправильно. Все, что я пытаюсь сделать, это зашифровать строку, а затем расшифровать строку.
Ниже приведен мой код, и любая помощь будет оценена.
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))
EcryptoEncStream.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