I'm really not sure what's going on here. My app is encrypting files correctly and without issue, but it's throwing an IndexOutOfRangeException when trying to decrypt the same file...
Here's my code:
Public Sub EncryptDecrypt(ByVal Action As String, ByVal InFile As String, ByVal OutFile As String)
Try
Dim Buffer(4096) As Byte
Dim Stream As CryptoStream
Dim Rij As New System.Security.Cryptography.RijndaelManaged
Dim Key(), IV() As Byte
FSIn = New FileStream(InFile, FileMode.Open, FileAccess.Read)
FSOut = New FileStream(OutFile, FileMode.OpenOrCreate, FileAccess.Write)
FSOut.SetLength(0)
Key = CreateKey("p0Ju423KQY7h4D29Ml536jbX7gS2Q6Rtm87XvRttlKiZ")
IV = CreateIV("p0Ju423KQY7h4D29Ml536jbX7gS2Q6Rtm87XvRttlKiZ")
If Action = "E" Then
Stream = New CryptoStream(FSOut, Rij.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
Else
Stream = New CryptoStream(FSOut, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
End If
Stream.Close()
FSIn.Close()
FSOut.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
The error appears on the Stream.Close()
line.
I have applied the same code elsewhere and it doesn't have any issues...
Here's my stack trace:
System.IndexOutOfRangeException was caught Message="Index was outside the bounds of the array."
Source="mscorlib" StackTrace: at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at Crypt.EncryptDecrypt(String Action, String InFile, String OutFile) in D:\Development\Projects\Web\WebSite1\App_Code\Crypt.vb:line 34 InnerException:
Any help will be greatly appreciated.
EDIT 1 After aaz's comment, I revised and replaced
Stream = New CryptoStream(FSOut, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
with
Stream = New CryptoStream(FSIn, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
Here's the resulting Stack Trace:
System.IndexOutOfRangeException was caught Message="Index was outside the bounds of the array." Source="mscorlib" StackTrace: at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] > inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 > outputOffset, PaddingMode paddingMode, Boolean fLast) at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] > inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at Crypt.EncryptDecrypt(String Action, String InFile, String OutFile) in > D:\Development\Projects\Web\WebSite1\App_Code\Crypt.vb:line 34 InnerException:
Seems to me that its the same error...
END EDIT 1