6

我真的不确定这里发生了什么。我的应用程序正在正确加密文件并且没有问题,但是在尝试解密同一文件时抛出了 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="Index is outside the bounds of the array."
Source="mscorlib" StackTrace:在 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) 在 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 () 在 D:\Development\Projects\Web\WebSite1\App_Code\Crypt.vb:line 34 InnerException 中的 Crypt.EncryptDecrypt(String Action, String InFile, String OutFile):

任何帮助将不胜感激。

编辑 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="Index is outside the bounds of the array." Source="mscorlib" StackTrace:在 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] > inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 > outputOffset, PaddingMode paddingMode, Boolean fLast) 在 System.Security .Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] > inputBuffer, Int32 inputOffset, Int32 inputCount) 在 System.Security.Cryptography.CryptoStream.FlushFinalBlock() 在 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) 在 System.IO。 Stream.Close() at Crypt.EncryptDecrypt(String Action, String InFile, String OutFile) in > D:

在我看来,这是同样的错误......

结束编辑 1

4

2 回答 2

1

好吧,我认为有几件事需要解决。一方面,由于您已将 FSOut 更改为 FSIn,因此似乎从未实际使用过 FSOut。您似乎正在使用其中一个,但从代码的结构来看,我认为您的意图是获取一个文件并将数据加密或解密到另一个文件。

考虑从头开始使用http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx作为起点,如果您的意图是从一个文件中读取它并写入另一个修改它,如果你认为合适,或者考虑有一个帮助方法,在内存中创建文件的副本,加密文件,移动它并将内存中的文件替换到起始位置,这样做可以让你在这两种情况下都可以利用此代码,并且不会产生太多额外的开销。

于 2011-02-17T00:17:26.940 回答
0

CryptoStream在数据末尾发出 PKCS#7 样式的填充,它可能包括从一个字节到一个完整密码块的任何地方,但绝不是零长度;这确保了加密流的长度是块大小的倍数,并且可以明确删除填充。您是否有可能试图解密一些无效的加密数据?

于 2011-02-17T00:41:28.293 回答