我真的不确定这里发生了什么。我的应用程序可以正确地加密文件,没有问题,但是尝试解密相同的文件时,它会抛出IndexOutOfRangeException异常...
这是我的代码:
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
错误出现在
Stream.Close()
行。我已经在其他地方应用了相同的代码,没有任何问题...
以下是我的堆栈跟踪:
任何帮助将不胜感激。System.IndexOutOfRangeException被捕获 Message="索引超出了数组界限。"
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:
编辑1:在aaz的评论后,我进行了修订和替换。
Stream = New CryptoStream(FSOut, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
使用
Stream = New CryptoStream(FSIn, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
以下是产生的堆栈跟踪:
捕获到 System.IndexOutOfRangeException 异常 Message="索引超出了数组界限。" 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:
对我来说,这似乎是同样的错误...
编辑1 结束