I use the following Rijndael code to do encryption without fail for many times. But why it can not encrypt an ISO file with 4.2 GB? In fact my computer has 16GB memory and it should not be a memory problem. I use Windows 7 Ultimate. The code is compiled as winform (.Net 4) using Visual Studio 2010 (a VB.NET project).
I have checked that the ISO file is OK and can be mounted as virtual drive and even can be burnt to DVD rom. So it is not the ISO file problem.
My question: Why the following code cannot encrypt an ISO file with size of 4.2GB? Is that caused by the limitation of Windows/.NET 4 implementation?
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
Try to change from this:
rdlen = Convert.ToInt32(rdlen + len)
to this:
rdlen = Convert.ToInt64(rdlen + len)