Problem decrypting data

From: Gordon (Gordon_at_discussions.microsoft.com)
Date: 05/24/05


Date: Mon, 23 May 2005 18:20:05 -0700

I'm having a problem with the decryption part of the code below. An
exception is generated when creating the CryptoStream for decryption. It's
the error "Stream does not support seeking". I can't figure out what's
causing the error...

ASCIIEncoding textConverter = new ASCIIEncoding();
byte[] bin = textConverter.GetBytes("some text to encrypt");

RijndaelManaged tdes = new RijndaelManaged();

tdes.GenerateKey();
tdes.GenerateIV();
byte[] bkey = tdes.Key;
byte[] biv = tdes.IV;

byte[] bout = new byte[100];
byte[] bres = new byte[100];
int count;
                        
try
{
        using (MemoryStream msout = new MemoryStream(100))
        {
                CryptoStream encStream = new CryptoStream(msout,
tdes.CreateEncryptor(bkey,biv), CryptoStreamMode.Write);
                encStream.Write(bin, 0, bin.GetLength(0));
                encStream.FlushFinalBlock();
                msout.Seek(0, SeekOrigin.Begin);
                count = msout.Read(bout, 0, (int)msout.Length);
        }
        
        using (MemoryStream msin = new MemoryStream(bout))
        {
                CryptoStream encStream = new CryptoStream(msin,
tdes.CreateDecryptor(bkey,biv), CryptoStreamMode.Read);
                count = encStream.Read(bres, 0, (int)encStream.Length);
        }
}
catch (Exception e)
{
        //HandleException(e);
}