Complete code that works with MemoryStream but not with FileStream


T

Tony Johansson

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
 
Ad

Advertisements

T

Tony Johansson

Tony Johansson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top