解密文件时索引超出范围

发布于 2024-10-17 21:43:53 字数 3166 浏览 8 评论 0原文

我真的不确定这里发生了什么。我的应用正确加密文件,没有问题,但在尝试解密同一文件时抛出 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 是 捕获消息=“索引位于外部” 数组的边界。”
来源=“mscorlib”堆栈跟踪: 在 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] 输入缓冲区,Int32 输入偏移,Int32 inputCount、Byte[]&输出缓冲区, Int32 输出偏移、PaddingMode paddingMode,布尔值 fLast) 在 System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] 输入缓冲区,Int32 输入偏移,Int32 输入计数) 在 System.Security.Cryptography.CryptoStream.FlushFinalBlock() 在 System.Security.Cryptography.CryptoStream.Dispose(布尔值 处置) 在 System.IO.Stream.Close() 在 Crypt.EncryptDecrypt(字符串操作,字符串 InFile,字符串 OutFile) 在 D:\Development\Projects\Web\WebSite1\App_Code\Crypt.vb:line 34 内部异常:

任何帮助将不胜感激。

编辑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="索引超出了数组范围。" 来源=“mscorlib” 堆栈跟踪: 在System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte []> inputBuffer,Int32 inputOffset,Int32 inputCount,Byte []& outputBuffer,Int32> outputOffset,PaddingMode paddingMode,布尔fLast) 在 System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] > inputBuffer,Int32 inputOffset,Int32 inputCount) 在 System.Security.Cryptography.CryptoStream.FlushFinalBlock() 在 System.Security.Cryptography.CryptoStream.Dispose(布尔处置) 在 System.IO.Stream.Close() 在 Crypt.EncryptDecrypt(String Action, String InFile, String OutFile) 中 > D:\Development\Projects\Web\WebSite1\App_Code\Crypt.vb:第 34 行 内部异常:

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

END EDIT 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-10-24 21:43:53

嗯,我认为有一些事情需要解决。首先,既然您已将 FSOut 更改为 FSIn,则 FSOut 似乎从未实际使用过。您似乎正在使用其中之一,但从代码的结构来看,我认为您的意图是获取一个文件并将数据加密或解密到另一个文件。

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

Well I think there are a couple things that need to be fixed. For one It does not appear that the FSOut is ever actually used now that you have changed the FSOut to FSIn. You seem to be using one or the other but from the structure of the code I would think your intent was to take a file In and encrypt or decrypt the data to another file.

Consider starting from scratch on this one using http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx as a jumping off point and if your intent is for it to read from one file and write to another modify it as you see fit or consider having a helper method that creates a copy of the file in memory, encrypts the file, moves it and replaces the in memory file to the starting location, doing it this way allows u to leverage this code for either case and doesn't really incur much extra overhead.

梦中的蝴蝶 2024-10-24 21:43:53

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

CryptoStream emits PKCS#7-style padding at the end of the data, which may include anywhere from one byte to one complete cipher block but is never zero-length; this ensures both that the encrypted stream is a multiple of the block size in length, and that padding can be unambiguously removed. Is it possible you are attempting to decrypt something that is not valid encrypted data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文