I would like to read/write encrypted XML files using LINQ to XML. Does anyone know how to use encryption algorithms built into the .NET Framework to encrypt the Stream used by the XDocument object?
I did try it, but you can’t set the CryptoStream to Read/Write access. It only support Read or Write, which causes LINQ to XML to throw an exception.
Update: It would be nice to read/write the document ‘on the fly’, but I am only required to read the encrypted xml file, manipulate it, then write it back out encrypted again.
The easiest approach is probably an XDocument.Load(), Linq around, then XDocument.Save(). From a quick test app (go easy on non-disposed resources):
Swap your favorite ICryptoTransform into the CryptoStream.