1

I get the Cryptographic Exception Padding is invalid and cannot be removed when trying to decrypt data. There are many questions on the net and on StackOverflow about this error, but I could not find the solution. More particularly, setting the Padding to None or explicitly defining the BlockSize did not seem to help.

I have a sub that encrypts and decrypts a little XML file placed on my hard drive. As parameters, the sub receives the location of the file and whether it should encrypt or decrypt. Here is the code:

Private Sub LicenceEncryptOrDecrypt(LizenzDatei As String, EncryptOrDecrypt As String) 

    Dim Rijndael As RijndaelManaged = New RijndaelManaged

    Dim passPhrase As String = "SuperPassword"
    Dim hashAlgorithm As String = "SHA1"
    Dim passwordIterations As Integer = 3
    Dim keySize As Integer = 128

    Dim initVector As String = "16charLongString"
    Rijndael.IV = Encoding.ASCII.GetBytes(initVector)

    Dim saltValue As String = "DoYouWantSomeSalt"
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue)

    Dim password As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPhrase, saltValueBytes)

    Rijndael.Key = password.GetBytes(keySize / 8)

    Rijndael.Padding = PaddingMode.None


    Dim transform As ICryptoTransform
    Dim tempFile As String


        Select Case EncryptOrDecrypt
            Case "Encrypt"
                transform = Rijndael.CreateEncryptor(Rijndael.Key, Rijndael.IV)
                tempFile = LizenzDatei + ".enc"
            Case "Decrypt"
                transform = Rijndael.CreateDecryptor(Rijndael.Key, Rijndael.IV)
                tempFile = LizenzDatei + ".dec"
            Case Else
                Debug.Print(">< EncryptOrDecrypt: Falshes parameter. Ende Sub.")
                Success = False
        End Select


        Using inFS As FileStream = New FileStream(LizenzDatei, FileMode.Open)
            Dim data() As Byte = New Byte(inFS.Length - 1) {}

            Using outFS As FileStream = New FileStream(tempFile, FileMode.Create)
                Using outStreamEncrypted As CryptoStream = New CryptoStream(outFS, transform, CryptoStreamMode.Write)
                    outStreamEncrypted.Write(data, 0, data.Length)
                    outStreamEncrypted.FlushFinalBlock()
                    outStreamEncrypted.Close()
                End Using
                outFS.Close()
            End Using
            inFS.Close()
        End Using

    File.Delete(LizenzDatei)
    File.Move(tempFile, LizenzDatei)

End Sub

The error occurs at the line outStreamEncrypted.FlushFinalBlock(). I noted that the length of data was different during encryption (156) and decryption (160).

Nicolas
  • 1,151
  • 3
  • 15
  • 28
  • possible duplicate of [CryptographicException: Padding is invalid and cannot be removed](http://stackoverflow.com/questions/11762/cryptographicexception-padding-is-invalid-and-cannot-be-removed) – jbtule Mar 25 '13 at 14:25
  • This code is oddly incomplete, it never actually reads from the input file. So we are probably not looking at the real cause of the problem. – Hans Passant Mar 25 '13 at 14:31
  • A Padding exception will likely occur at decryption if your key and iv don't match your ciphertext, in this case you are trying to decrypt a zero byte array the length of your ciphertext, thus they don't match up. – jbtule Mar 25 '13 at 14:38
  • Thanks @jbtule and @HansPassant! Based on your comments, I used 'Dim data() As Byte = System.IO.File.ReadAllBytes(LizenzDatei)´. Since then, it works. – Nicolas Mar 25 '13 at 15:19

1 Answers1

0

Based on the comments and on the question Rijndael Decryption error - Length of the data to decrypt is invalid, I realized that my error came from the input FileStream. The input file was actually never read.

Replacing the lines

Using inFS As FileStream = New FileStream(LizenzDatei, FileMode.Open)
Dim data() As Byte = New Byte(inFS.Length - 1) {}

by

Dim data() As Byte = System.IO.File.ReadAllBytes(LizenzDatei)

solved my problem.

Thanks everybody!

Community
  • 1
  • 1
Nicolas
  • 1,151
  • 3
  • 15
  • 28