Initializing Byte Array, IndexOutOfRange Exception
-
Good Day, I'm working with the AES Decryptor (using RijndaelManaged), but when I try to initialize a byte[] array to store my ciphertext data, it throws an IndexOutOfRange exception.
using (FileStream Ciphertext = new FileStream(\_InputPath, FileMode.Open, FileAccess.Read, FileShare.None)) //For Plaintext Stream { using (FileStream PlainText = new FileStream(\_OutputPath, FileMode.Create, FileAccess.Write, FileShare.None)) //For Ciphertext Stream { using (CryptoStream CStreamWriter = new CryptoStream(PlainText, Decryptor, CryptoStreamMode.Write)) //For CryptoStream Writer { byte\[\] CTextData = new byte\[Ciphertext.Length\]; //EXCEPTION THROWN HERE
The exact error message is: "Index was outside the bounds of the array." The weird thing is, this only happens for file > 200MB. But for a 150mb file, I have no problem. Please advice thanks!
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
-
Good Day, I'm working with the AES Decryptor (using RijndaelManaged), but when I try to initialize a byte[] array to store my ciphertext data, it throws an IndexOutOfRange exception.
using (FileStream Ciphertext = new FileStream(\_InputPath, FileMode.Open, FileAccess.Read, FileShare.None)) //For Plaintext Stream { using (FileStream PlainText = new FileStream(\_OutputPath, FileMode.Create, FileAccess.Write, FileShare.None)) //For Ciphertext Stream { using (CryptoStream CStreamWriter = new CryptoStream(PlainText, Decryptor, CryptoStreamMode.Write)) //For CryptoStream Writer { byte\[\] CTextData = new byte\[Ciphertext.Length\]; //EXCEPTION THROWN HERE
The exact error message is: "Index was outside the bounds of the array." The weird thing is, this only happens for file > 200MB. But for a 150mb file, I have no problem. Please advice thanks!
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
Perhaps because the system is unable to allocate an array that large. Don't read the entire file at once, there is no reason to have all the data in memory at the same time. Create a buffer and read only a small part of the file each time.
Despite everything, the person most likely to be fooling you next is yourself.