How to write XML to an encrypted file?

I need to write XML data to an encrypted file. I can read / write encrypted files, but it's hard for me to figure out how to skip part of the file input and do this from a MemoryStream object.

This is what I still have. Basically, I just need to get the byte [] to fix its standard encryption.

I appreciate the amazing contribution. I'll check it out soon.

EDIT: after testing, I get the exception “Can't access the closed stream” when I try to close the memystream 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();
+3
source share
2 answers

Write to a MemoryStream and use MemoryStream.ToArray ()

+2
source

, StreamWriter, . CryptoStream, .

: " ". .NET- ? , ICryptoTransform.

+1

All Articles