Rijndael解密错误 - 要解密的数据长度无效

时间:2009-09-09 00:06:11

标签: .net encryption rijndaelmanaged

我能够使用rijndeal加密zip文件,但是当我解密时,我收到一条错误,上面写着“要解密的数据长度无效”我得到的字节数组要从文件中解密。这是我如何得到字节数组。

Dim FStream As FileStream = File.OpenRead("<Filepath>")
EncData = New Byte(FStream.Length) {}
FStream.Read(EncData, 0, EncData.Length)
Dim DecryptedBytes As Byte() = DataVault.RijndealController.Decrypt(EncData, Password)

一旦我将字节数组传递给Decrypt方法,当我尝试使用cryptostream读取时会出现错误。

Public Function Decrypt(ByVal Input As Byte(), ByVal Password As String) As Byte()

 Try
   Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password)
   Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2")
   Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("S@ltVa|u<")


   Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes,SaltValue, 4)
   Dim keyBytes As Byte() = DerivedBytes.GetBytes(32)

 Dim symmetricKey As RijndaelManaged
 symmetricKey = New RijndaelManaged()
 symmetricKey.Mode = CipherMode.CBC

 Dim decryptor As ICryptoTransform
 decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

 Dim memoryStream As MemoryStream
 memoryStream = New MemoryStream(Input)

 Dim cryptoStream As CryptoStream
 cryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)

 Dim plainTextBytes As Byte()
 ReDim plainTextBytes(Input.Length)


 Dim decryptedByteCount As Integer
While ((decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)) > 0)

 End While

memoryStream.Close()
cryptoStream.Close()

Return plainTextBytes

Catch ex As Exception
 Return Nothing
End Try

End Function

任何想法我做错了什么?

这里是他们加密的代码:

        Public Function EncryptBytes(ByVal Input As Byte(), ByVal Password As String) As Byte()

        Try
            Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password)
            Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2")
            Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("S@ltVa|u<")
            Dim InputStringBytes As Byte() = Input

            Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes, SaltValue, 4)
            Dim keyBytes As Byte() = DerivedBytes.GetBytes(32)


            Dim symmetricKey As RijndaelManaged
            symmetricKey = New RijndaelManaged
            symmetricKey.Mode = CipherMode.CBC

            Dim encryptor As ICryptoTransform
            encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
            Dim MStream As New MemoryStream()

            Dim cryptoStream As CryptoStream
            cryptoStream = New CryptoStream(MStream, encryptor, CryptoStreamMode.Write)
            cryptoStream.Write(InputStringBytes, 0, InputStringBytes.Length)
            cryptoStream.FlushFinalBlock()

            Dim cipherBytes As Byte() = MStream.ToArray()
            MStream.Close()
            cryptoStream.Close()

            Return cipherBytes

        Catch ex As Exception

        End Try
        Return Encoding.UTF8.GetBytes("0")
    End Function

3 个答案:

答案 0 :(得分:2)

如何保存加密数据?您是使用纯文本(即ASCIi,UTF-8等)对其进行编码还是使用Base-64编码?尝试将其加密为字节数组,然后立即对其进行解密。如果它成功并且解密成功,则会出现编码问题。

答案 1 :(得分:2)

使用System.IO.File.ReadAllBytes,System.IO.File.WriteAllBytes替换文件流代码。如果这样可行,那么您就知道文件流导致了问题。

答案 2 :(得分:0)

这解决了它:

    Public Function EncryptBytes(ByVal Input As Byte(), ByVal Password As String) As Byte()

        Try
            Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password)
            Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2")
            Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("S@ltVa|u<")
            Dim InputStringBytes As Byte() = Input

            Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes, SaltValue, 4)
            Dim keyBytes As Byte() = DerivedBytes.GetBytes(32)

            'Generate Rijndael Manager
            Dim symmetricKey As RijndaelManaged
            symmetricKey = New RijndaelManaged
            symmetricKey.Mode = CipherMode.CBC

            Dim encryptor As ICryptoTransform
            encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
            Dim MStream As New MemoryStream()

            Dim cryptoStream As CryptoStream
            cryptoStream = New CryptoStream(MStream, encryptor, CryptoStreamMode.Write)
            cryptoStream.Write(InputStringBytes, 0, InputStringBytes.Length)
            cryptoStream.FlushFinalBlock()

            Dim cipherBytes As Byte() = MStream.ToArray()
            MStream.Close()
            cryptoStream.Close()

            Return cipherBytes

        Catch ex As Exception

        End Try
        Return Encoding.UTF8.GetBytes("0")
    End Function