CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    CryptoStream To MemoryStream problem

    Hi, I am trying to write data to a CryptoStream (which writes an encrypted version of the data to the underlying MemoryStream), then store that data in a byte array.

    After that I want to write the data to a MemoryStream and then decrypt that data using a CryptoStream and the underlying MemoryStream that was just written to.

    This does not seem to work. I don't get an error, it simply reads Zeros when reading using the CryptoStream.

    Here is my code,

    Code:
     public List<byte[]> LoadAsByteArray(IEnumerable<SomeClass> Objects)
            {
    
                List<byte[]> ObjectBytes = new List<byte[]>(Objects.Count());
    
                MemoryStream ms = new MemoryStream();
                //opens a cryptostream for encryption
                Rijndael rijndael = new RijndaelManaged();
                rijndael.Padding = PaddingMode.None;
                CryptoStream cStream = new CryptoStream(ms, rijndael.CreateEncryptor(Messages.Key, Messages.IV)
                                                        , CryptoStreamMode.Write);
    
                foreach (var object in Objects)
                {
                    object.Send(cStream);//writes data using the cryptostream
                    byte[] data = ms.ToArray();
                    ObjectsBytes.Add(data);
                    ms.Seek(0,0);
                }
    
                return ObjectsBytes;
            }
    
    public List<SomeObject> LoadAsObject(IEnumerable<byte[]> ObjectsBytes)
            {
    
                List<SomeObject> Objects = new List<SomeObject>(ObjectsBytes.Count());
    
                //encryption stuff ... using AES encryption
                RijndaelManaged rijndael = new RijndaelManaged();
                rijndael.Padding = PaddingMode.None;
                ICryptoTransform transform = rijndael.CreateDecryptor(Messages.Key, Messages.IV);
    
                MemoryStream ms = new MemoryStream();
                //opens the cryptostream
                CryptoStream cStream = new CryptoStream(ms, transform,
                                                    CryptoStreamMode.Read);
                int i = 0;
                foreach (var objectbytes in ObjectsBytes)
                {
                    i++;
                    ms.Write(objectbytes, 0, objectbytes.Length);
                    ms.Seek(0, 0);
                    SomeObject object = new SomeObject();
                    object.Receive(cStream); //reads data from cryptostream
                    Objects.Add(object);
                    ms.Seek(0, 0);
                }
    
                return Objects;
            }
    Don't worry about object.send and receive. Think of them as serialize and deserializing the objects to a stream.

    Also if I remove the cryptostream and just write and read from the memory stream it works.

    If you need more detail on exactly how its behaving I can provide this. Otherwise if you see some problem with the encryption/decription logic let me know.

    Thanks in advance,
    Last edited by nik88; June 29th, 2009 at 03:13 PM. Reason: code tags

  2. #2
    Join Date
    Jul 2008
    Posts
    10

    Re: CryptoStream To MemoryStream problem

    Update:

    So it works when I open a new memorystream and cryptostream within each iteration of the Object List loop.

    I would still like to know why the version I posted above doesn't work, since I am working with Lists of 100, 000 items and open two new streams every iteration is a performance hit.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: CryptoStream To MemoryStream problem

    Please format your code by surrounding it with [ code][ /code] tags.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: CryptoStream To MemoryStream problem

    I think that it is because you cannot simply reset crypto streams by seeking to begining of the underlying memory stream, because bytes in cryptostream are not independant. As far as I know, byte on position n-1 is part of computation of byte on position n, so I think by spliting the stream to chunks and decrypting each chunk separately as I do it, you lost infromation vital for decryption.

    In other words, the solution with opening the streams in each iteration is the only one. Or you could use a single stream for all encrypted object and maintain separated metada which would describe where each object in the stream starts and where ends.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured