|
-
Aug 25th, 2009, 08:36 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] "Padding is invalid and cannot be removed."
Hello,
I'm using this class to encrypt/decrypt a file:
vb.net Code:
Imports System.Security.Cryptography Imports System.Text Imports System.IO Public Class EncryptFile Private Shared Buffer(4096) As Byte Private Shared _Rijndael As New RijndaelManaged Private Shared _Hash As New SHA256Managed Private Shared _Encoding As New UTF8Encoding Private Shared IV As Byte() = {12, 4, 58, 74, 52, 33, 69, 87, 47, 8, 23, 69, 85, 47, 85, 21} Private Shared cStream As CryptoStream Enum Action Encrypt = 0 Decrypt = 1 End Enum Public Shared Function EncryptDecrypt(ByVal source As String, ByVal destination As String, _ ByVal key As String, ByVal actions As Action) As Integer Dim sourceStream As New FileStream(source, FileMode.Open, FileAccess.Read) Dim destinationStream As New FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write) Dim BytesProcessed As Long = 0 Dim SourceLength As Long = sourceStream.Length Dim CurrentByteProcess As Integer = 0 Dim PercentDone As Integer = 0 Dim _key As Byte() = _Hash.ComputeHash(_Encoding.GetBytes(key)) Select Case direction Case EncryptFile.Action.Encrypt cStream = New CryptoStream(destinationStream, _ _Rijndael.CreateEncryptor(_key, IV), _ CryptoStreamMode.Write) Case EncryptFile.Action.Decrypt cStream = New CryptoStream(destinationStream, _ _Rijndael.CreateDecryptor(_key, IV), _ CryptoStreamMode.Write) End Select While BytesProcessed < SourceLength CurrentByteProcess = sourceStream.Read(Buffer, 0, 4096) cStream.Write(Buffer, 0, 4096) BytesProcessed += CLng(CurrentByteProcess) PercentDone = CInt((BytesProcessed / SourceLength) * 100) End While cStream.Close() destinationStream.Close() sourceStream.Close() Return PercentDone End Function 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:
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.
-
Aug 25th, 2009, 08:41 AM
#2
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
-
Aug 25th, 2009, 08:58 AM
#3
Thread Starter
Fanatic Member
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.
-
Aug 25th, 2009, 10:05 AM
#4
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
-
Aug 25th, 2009, 09:05 PM
#5
Thread Starter
Fanatic Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|