0

I have an issue when trying to switch between 256 and 128 bit encryption with my code. If I keep it at 256bit, it works fine, but when I try it again using a 128 bit key, I get an error of "Padding is invalid and cannot be removed." Any help is much appreciated. I am no pro at encryption. The error is thrown when the "DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)" part of the AES_Decrypt function is called.

    Public Shared Function GenerateEncryptionKey(Optional ByVal keySizeIndex As Integer = 1) As Byte()
        'Generate a Key.
        Dim rm As RijndaelManaged = New RijndaelManaged()

        Select Case keySizeIndex
            Case 0
                rm.KeySize = 128

            Case 1
                rm.KeySize = 256

            Case Else
                rm.KeySize = 256
        End Select

        rm.GenerateKey()
        Return rm.Key
    End Function

    Public Shared Function AES_Encrypt(ByVal input As String, ByVal pass As Byte()) As String

        Dim AES As New RijndaelManaged
        Dim encrypted As String = ""

        Dim hash As Byte()
        Dim byteLength As Integer

        Try
            byteLength = pass.Length
            ReDim hash(byteLength - 1)

            Array.Copy(pass, 0, hash, 0, 16)
            If (byteLength > 16) Then
                Array.Copy(pass, 0, hash, 15, 16)
            End If

            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.Encoding.Unicode.GetBytes(input)
            'Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            Dim testBuffer As Byte() = DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

        Catch ex As Exception
            encrypted = ""
        End Try

        Return encrypted

    End Function


    Public Shared Function AES_Decrypt(ByVal input As String, ByVal pass As Byte()) As String

        Dim AES As New RijndaelManaged
        Dim decrypted As String = ""

        Dim hash As Byte()
        Dim byteLength As Integer

        Try
            byteLength = pass.Length
            ReDim hash(byteLength - 1)

            Array.Copy(pass, 0, hash, 0, 16)
            If (byteLength > 16) Then
                Array.Copy(pass, 0, hash, 15, 16)
            End If

            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            Dim testBuffer As Byte() = DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
            decrypted = System.Text.Encoding.Unicode.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            'decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
            decrypted = ""
        End Try

        Return decrypted

    End Function
  • Possible duplicate of [Padding is invalid and cannot be removed?](http://stackoverflow.com/questions/8583112/padding-is-invalid-and-cannot-be-removed) – Andrew Morton Mar 04 '16 at 20:40
  • Yah, I've looked into that, and it didn't work – Jason Chang Mar 04 '16 at 20:47
  • The code posted doesnt show the padding being set: `AES.Padding = PaddingMode.ISO10126` or `AES.Padding = PaddingMode.PKCS7` How do you use `GenerateEncryptionKey`? and there are lots of things there not being disposed – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 20:53
  • This seems wrong too: `Array.Copy(pass, 0, hash, 0, 16)` – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 20:54
  • @Plutonix, I am using the Generate Encryption Key to get a Byte Array and save/use it, then when I encrypyt/decrypt, I pass the same key into the functions. I am sorry people, I am rewriting someone else's code, and I am not the best at encryption. Any input is greatly appreciated, as you all are doing now. – Jason Chang Mar 04 '16 at 20:58
  • But I cant see where it is being used or passed. I guess that is generating the IV? – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 21:00
  • I'd go back to the drawing board. the [MSDN example](https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(System.Security.Cryptography.RijndaelManaged);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-VB)&rd=true) is pretty clear – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 21:03
  • When using RijndaelManaged for AES set the block size to 128-bits, don't depend on the default. The pading defaults to PKCS#7. ECB mode (which is not secure) does not use an iv. – zaph Mar 04 '16 at 21:07
  • I just might redo it. I added the AES.Padding = PaddingMode.PKCS7, and it works when I set it to 256, but then when start it over and set it to 128 I get the padding error. and vice versa. – Jason Chang Mar 04 '16 at 21:15
  • @Plutonix, is there a way to not need the IV? Essentially I just want to store the AES Key and use that to encode and decode – Jason Chang Mar 04 '16 at 21:17
  • When you set "it" what are you setting to 256 or 128? De set `BlockSize` to 128. – zaph Mar 04 '16 at 21:41
  • I am setting the RijndaelManaged keySieze, in the GenerateEncryptionKey function here : Dim rm As RijndaelManaged = New RijndaelManaged() Select Case keySizeIndex Case 0 rm.KeySize = 128 Case 1 rm.KeySize = 256 Case Else rm.KeySize = 256 End Select rm.GenerateKey() Return rm.Key De set the Blocksize?? – Jason Chang Mar 04 '16 at 23:29
  • @zaph, thank you, Sorry, I misread your post, and didn't realize you had already answered my question about not needing an IV. Thank you – Jason Chang Mar 04 '16 at 23:47

0 Answers0