2011-10-01 62 views
3

我使用以下Rijndael代碼多次進行加密而不失敗。但爲什麼它不能用4.2 GB加密ISO文件?事實上,我的電腦有16GB內存,它不應該是一個內存問題。我使用Windows 7旗艦版。代碼使用Visual Studio 2010(一個VB.NET項目)編譯爲winform(.Net 4)。爲什麼Rijndael加密代碼不適用於大文件?

我已檢查ISO文件是否可以掛載爲虛擬驅動器,甚至可以刻錄到DVD光盤。所以這不是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 
+2

當您嘗試錯在何處? – AakashM

+0

產生1GB ++加密文件後停止。但是我的源文件大於4GB – user774411

+0

在附註中,您應該增加緩衝區大小。 – Magnus

回答

2

嘗試從:

rdlen = Convert.ToInt32(rdlen + len) 

這樣:

rdlen = Convert.ToInt64(rdlen + len) 
+0

雖然有效,但它與'rdlen = rdlen + len'基本相同 –

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 

一個長+ INT的結果將是一個漫長的。 Long是64位有符號整數,Int是32位有符號整數。

相關問題