|
-
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.
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
|