Results 1 to 5 of 5

Thread: [RESOLVED] "Padding is invalid and cannot be removed."

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] "Padding is invalid and cannot be removed."

    Hello,

    I'm using this class to encrypt/decrypt a file:

    vb.net Code:
    1. Imports System.Security.Cryptography
    2. Imports System.Text
    3. Imports System.IO
    4.  
    5. Public Class EncryptFile
    6.  
    7.     Private Shared Buffer(4096) As Byte
    8.     Private Shared _Rijndael As New RijndaelManaged
    9.     Private Shared _Hash As New SHA256Managed
    10.     Private Shared _Encoding As New UTF8Encoding
    11.     Private Shared IV As Byte() = {12, 4, 58, 74, 52, 33, 69, 87, 47, 8, 23, 69, 85, 47, 85, 21}
    12.     Private Shared cStream As CryptoStream
    13.     Enum Action
    14.         Encrypt = 0
    15.         Decrypt = 1
    16.     End Enum
    17.  
    18.     Public Shared Function EncryptDecrypt(ByVal source As String, ByVal destination As String, _
    19.                                           ByVal key As String, ByVal actions As Action) As Integer
    20.         Dim sourceStream As New FileStream(source, FileMode.Open, FileAccess.Read)
    21.         Dim destinationStream As New FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write)
    22.         Dim BytesProcessed As Long = 0
    23.         Dim SourceLength As Long = sourceStream.Length
    24.         Dim CurrentByteProcess As Integer = 0
    25.         Dim PercentDone As Integer = 0
    26.         Dim _key As Byte() = _Hash.ComputeHash(_Encoding.GetBytes(key))
    27.  
    28.         Select Case direction
    29.             Case EncryptFile.Action.Encrypt
    30.                 cStream = New CryptoStream(destinationStream, _
    31.                                            _Rijndael.CreateEncryptor(_key, IV), _
    32.                                         CryptoStreamMode.Write)
    33.             Case EncryptFile.Action.Decrypt
    34.                 cStream = New CryptoStream(destinationStream, _
    35.                                           _Rijndael.CreateDecryptor(_key, IV), _
    36.                                           CryptoStreamMode.Write)
    37.         End Select
    38.  
    39.         While BytesProcessed < SourceLength
    40.             CurrentByteProcess = sourceStream.Read(Buffer, 0, 4096)
    41.             cStream.Write(Buffer, 0, 4096)
    42.             BytesProcessed += CLng(CurrentByteProcess)
    43.             PercentDone = CInt((BytesProcessed / SourceLength) * 100)
    44.         End While
    45.  
    46.         cStream.Close()
    47.         destinationStream.Close()
    48.         sourceStream.Close()
    49.  
    50.         Return PercentDone
    51.     End Function
    52. End Class

    When I encrypt a file it gives me no error, but when I decrypt a file it gives me this error:

    Padding is invalid and cannot be removed.

    In this line:

    vb.net Code:
    1. cStream.Close()

    I don't understand why, because I'm using the same IV and the same key, I've even checked the generated hash, and to me, it seems the same.

    I read that this error is generated if you don't use the same IV, but in this case it's impossible, since I've declared it before even using it!

    I'm sorry, it's just driving me crazy, I don't know what's wrong.

    Thanks in advance!
    Last edited by tassa; Aug 25th, 2009 at 02:20 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: "Padding is invalid and cannot be removed."

    "ByVal action As Action" <-- It's never (generally) a good idea to use the name of the class (or enum in this case) as the name of the variable. That would be the first thing I'd check.

    Secondly, use breakpoints, make sure you're getting into the correct branch of the case statement.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: "Padding is invalid and cannot be removed."

    I'm going to rename the action. It's my bad.

    I've used breakpoints and it goes the way it's supposed to be going, but when it reaches the cStream.Close statement, it throws the exception.

    When I stop debugging, the decrypted file is alright (as it originally was).
    Last edited by tassa; Aug 25th, 2009 at 09:02 AM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: "Padding is invalid and cannot be removed."

    With Enums I usually name them the pluralized format: So your enum would be called Actions. ByVal action As Actions

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: "Padding is invalid and cannot be removed."

    /bump

    Anyone have an idea of what's wrong?
    Last edited by tassa; Aug 25th, 2009 at 11:24 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width