CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  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

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