3

我多次使用以下 Rijndael 代码进行加密而不会失败。但是为什么它不能加密 4.2 GB 的 ISO 文件呢?事实上,我的电脑有 16GB 内存,应该不是内存问题。我使用 Windows 7 旗舰版。该代码使用 Visual Studio 2010(一个 VB.NET 项目)编译为 winform (.Net 4)。

我检查了 ISO 文件是否正常,可以挂载为虚拟驱动器,甚至可以刻录到 DVD ROM。所以不是ISO文件的问题。

我的问题:为什么以下代码无法加密大小为 4.2GB 的 ISO 文件?这是由 Windows/.NET 4 实现的限制引起的吗?

Private Sub DecryptData(inName As String, outName As String, rijnKey() As Byte, rijnIV() As Byte)

    'Create the file streams to handle the input and output files.
    Dim fin As New IO.FileStream(inName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    Dim fout As New IO.FileStream(outName, System.IO.FileMode.OpenOrCreate,
       System.IO.FileAccess.Write)
    fout.SetLength(0)

    'Create variables to help with read and write.
    Dim bin(100) As Byte 'This is intermediate storage for the encryption.
    Dim rdlen As Long = 0 'This is the total number of bytes written.
    Dim totlen As Long = fin.Length 'Total length of the input file.
    Dim len As Integer 'This is the number of bytes to be written at a time.

    'Creates the default implementation, which is RijndaelManaged.
    Dim rijn As New Security.Cryptography.RijndaelManaged
    Dim encStream As New Security.Cryptography.CryptoStream(fout,
       rijn.CreateDecryptor(rijnKey, rijnIV), Security.Cryptography.CryptoStreamMode.Write)

    'Read from the input file, then encrypt and write to the output file.
    While rdlen < totlen
        len = fin.Read(bin, 0, 100)
        encStream.Write(bin, 0, len)
        rdlen = Convert.ToInt32(rdlen + len)
    End While

    encStream.Close()
    fout.Close()
    fin.Close()
End Sub
4

2 回答 2

9
rdlen = Convert.ToInt32(rdlen + len)

Int32可以表示有符号整数,其值范围从负 2,147,483,648 到正 2,147,483,647,因为 4.2GB 大约是我猜rdlen永远不会大于的两倍,totlen因此你得到了一个永无止境的循环。

如果 VB.NET 像 C# 一样工作(我怀疑它确实如此),你只需删除转换

rdlen = rdlen + len

Long+Int 的结果将是 Long。其中 Long 是 64 位有符号整数, Int 是 32 位有符号整数。

于 2011-10-01T10:24:01.537 回答
2

尝试从这里改变:

rdlen = Convert.ToInt32(rdlen + len)

对此:

rdlen = Convert.ToInt64(rdlen + len)
于 2011-10-01T10:59:27.573 回答