开发者

Receiving 'The input is not a complete block' error, tried everything I can find on google. Rjindael Encryption in .NET

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))
        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: " &am开发者_运维百科p; 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


You have overridden the PaddingMode and set it None. Why? Leave the PaddingMode to its default value of PaddingMode.PKCS7 unless you have a good reason to change it and you understand padding in a block cipher.


It appears that the problem is the length of the data that you are passing to the decryption stream. If you change the declaration of theBytes from this:

Dim theBytes(memEncStream.Length) As Byte

To this:

Dim theBytes(memEncStream.Length - 1) As Byte

Then it runs fine (at least it did for me). I'm not a VB wizard at all, but I think the array declaration is one byte longer than the given size (I think it is 0 to N). With that extra byte passed to the decryption stream, it does not work.

And I'm sure you will see it soon enough, but your printing of the final decrypted text is not quite right. It is printing theBytes instead of endBytes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜