I need to write XML data to an encrypted file. I can read/write encrypted files, but I am having trouble figuring out how to skip the file input part and do it from a MemoryStream object.
This is what I have so far. Basically I just need to get a Byte[] to preform my standard encryption on it.
I appreciate the awesome input. I will be testing this shortly.
EDIT: After testing I am getting a “Cannot access a closed stream” exception when I try and close the memorystream object.
MemoryStream ms = new MemoryStream();
XmlTextWriter xmlwriter = new XmlTextWriter(ms,Encoding.ASCII);
FileStream EncryptedFileStream = new FileStream(file, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes("AAAAAAAA");
DES.IV = ASCIIEncoding.ASCII.GetBytes("AAAAAAAA");
ICryptoTransform desEncrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(EncryptedFileStream, desEncrypt, CryptoStreamMode.Write);
//create xml document
byte[] bytearray = new byte[ms.Length];
ms.Read(bytearray, 0, bytearray.Length);
cryptostream.Write(bytearray, 0, bytearray.Length);
cryptostream.Close();
ms.Close();
EncryptedFileStream.Close();
Write into a MemoryStream and use MemoryStream.ToArray()