Search code examples
.netencryptionrijndaelmanaged

Rijndael Decryption error - Length of the data to decrypt is invalid


I am able to encrypt a zip file using rijndeal but when i decrypt I get an error that says "Length of the data to decrypt is invalid" Im getting the byte array to decrypt from a file. Here is how i get the byte array.

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)

Once i pass the byte array into the Decrypt method I get the error when I try to read with the 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

Any ideas what im doing wrong?

Also here is the code they encrypts:

        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

Solution

  • Replace your filestream code with System.IO.File.ReadAllBytes, System.IO.File.WriteAllBytes. If this works then you know filestream is causing the issue.