From: Tony Johansson on 20 Apr 2010 14:17 Hi! This is complete code that is a console application. I encrypt a string test using the symmetric algoritm RijndaelManaged and write the encrypting string to a file using a FileStream. I then read the decryped file and put the contents in a byte array If I now use a MemoryStream as the first parameter of CryptoStream it works perfect. If I instead use the FileStream refering to the encrypted file as the first parameter of CryptoStream it cause an IndexOutOfRangeException saying "The index laid outside the limit for the matrix" I mean that because fsEncrypt is a FileStream which is a stream it should work to have a FileStream instead of a MemoryStream. The contents of the MemoryStream is the same as the FileStream so it should work. The actual row that cause IndexOutOfRangeException is this row csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); So can anybody give an explanation what cause the IndexOutOfRangeException to appear when I have a FileStream as the first parameter in the csDecrypt using System; using System.Text; using System.IO; using System.Security.Cryptography; class Program { static void Main(string[] args) { string original = "test"; RijndaelManaged myRijndael = new RijndaelManaged(); myRijndael.GenerateKey(); myRijndael.GenerateIV(); ICryptoTransform encryptor = myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.IV); FileStream fsEncrypt = new FileStream("encrypt.txt", FileMode.OpenOrCreate, FileAccess.Write); CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(original); csEncrypt.Write(dataToEncrypt, 0, dataToEncrypt.Length); csEncrypt.FlushFinalBlock(); fsEncrypt.Close(); fsEncrypt = new FileStream("encrypt.txt", FileMode.Open, FileAccess.Read); byte[] encrypted = new byte[fsEncrypt.Length]; fsEncrypt.Read(encrypted, 0, (int)fsEncrypt.Length); ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV); MemoryStream msDecrypt = new MemoryStream(encrypted); // CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); CryptoStream csDecrypt = new CryptoStream(fsEncrypt, decryptor, CryptoStreamMode.Read); byte[] fromEncrypt = new byte[encrypted.Length]; csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); Console.WriteLine("Original: {0}", original); Console.WriteLine("Round Trip: {0}", Encoding.UTF8.GetString(fromEncrypt)); } } //Tony
From: Tony Johansson on 20 Apr 2010 17:26 "Tony Johansson" <johansson.andersson(a)telia.com> skrev i meddelandet news:%232B%239WL4KHA.4952(a)TK2MSFTNGP02.phx.gbl... > Hi! > > This is complete code that is a console application. > > I encrypt a string test using the symmetric algoritm RijndaelManaged and > write the encrypting string to a file using a FileStream. > I then read the decryped file and put the contents in a byte array > If I now use a MemoryStream as the first parameter of CryptoStream it > works > perfect. > If I instead use the FileStream refering to the encrypted file as the > first > parameter of CryptoStream it cause > an IndexOutOfRangeException saying "The index laid outside the limit for > the > matrix" > > I mean that because fsEncrypt is a FileStream which is a stream it should > work to have a FileStream instead of a MemoryStream. The contents of the > MemoryStream is the same as the FileStream so it should work. > > The actual row that cause IndexOutOfRangeException is this row > csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); > > So can anybody give an explanation what cause the IndexOutOfRangeException > to appear when I have a FileStream as > the first parameter in the csDecrypt > > using System; > using System.Text; > using System.IO; > using System.Security.Cryptography; > > class Program > { > static void Main(string[] args) > { > string original = "test"; > RijndaelManaged myRijndael = new RijndaelManaged(); > > myRijndael.GenerateKey(); > myRijndael.GenerateIV(); > ICryptoTransform encryptor = > myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.IV); > > FileStream fsEncrypt = new FileStream("encrypt.txt", > FileMode.OpenOrCreate, FileAccess.Write); > CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, > CryptoStreamMode.Write); > > byte[] dataToEncrypt = Encoding.UTF8.GetBytes(original); > > csEncrypt.Write(dataToEncrypt, 0, dataToEncrypt.Length); > csEncrypt.FlushFinalBlock(); > fsEncrypt.Close(); > > fsEncrypt = new FileStream("encrypt.txt", FileMode.Open, > FileAccess.Read); > byte[] encrypted = new byte[fsEncrypt.Length]; > fsEncrypt.Read(encrypted, 0, (int)fsEncrypt.Length); > ICryptoTransform decryptor = > myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV); > > MemoryStream msDecrypt = new MemoryStream(encrypted); > // CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, > CryptoStreamMode.Read); > CryptoStream csDecrypt = new CryptoStream(fsEncrypt, decryptor, > CryptoStreamMode.Read); > byte[] fromEncrypt = new byte[encrypted.Length]; > > csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); > > Console.WriteLine("Original: {0}", original); > Console.WriteLine("Round Trip: {0}", > Encoding.UTF8.GetString(fromEncrypt)); > } > } > > //Tony I found the problem I just has to reset the file pointer to start. //Tony
|
Pages: 1 Prev: Installing 3rd party controls (TreeViewAdv) Next: GZip and Deflate streams... |