简体   繁体   中英

Read/decrypt encrypted XML file and then process internally

I have used this code in the past for writing and reading xml files. This time I want to write some encrypted generated XML and then read it and process it internally. I will post the code and perhaps someone can spot the problem.

When I am testing the decrypt I have been able to output a file that has a continuous line of null character codes. The encrypted file seems to contain data and varys in size with different amounts of data.

Please help, thanks!

Encrypt

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlwriter = new XmlTextWriter(ms,Encoding.UTF8);
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 working and tested XML data here*/


byte[] bytearray = new byte[ms.Length];


ms.Read(bytearray, 0, bytearray.Length);
cryptostream.Write(bytearray, 0, bytearray.Length);

cryptostream.Close();

EncryptedFileStream.Close();

xmlwriter.Close();
ms.Flush();
ms.Close();

DECRYPT

MemoryStream ms = new MemoryStream();
StreamWriter swDecrypt = new StreamWriter(ms);

DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes("AAAAAAAA");
DES.IV = ASCIIEncoding.ASCII.GetBytes("AAAAAAAA");

ICryptoTransform desDecrypt = DES.CreateDecryptor();

FileStream fsDecrypt = new FileStream(mstrIndexFile, FileMode.Open, FileAccess.Read);

CryptoStream cryptostreamDecr = new CryptoStream(fsDecrypt, desDecrypt, CryptoStreamMode.Read);

swDecrypt.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
swDecrypt.Flush();
ms.Position = 0;

Using your current architecture, you need to use the MemoryStream that you just filled with data (not forgetting to reset its position to zero and to flush any pending writes)

//I am currently stuck on this point.
swDecrypt.Flush();
ms.Position=0;
XmlTextReader lxmlReader = new XmlTextReader(ms);

however, my feeling is that you don't need a MemoryStream here. Instead, just supply the CryptoStream to the XmlTextReader:

CryptoStream cryptostreamDecr = new CryptoStream(.....
XmlTextReader lxmlReader = new XmlTextReader(cryptostreamDecr);

After much trial and error I was pointed to XML element encryption using the first method on the page. This method was much easier and straight forward. If anyone decides to use this just make sure you use the same KEY and IV on your encryption and decryption if they are taking place in separate places.

Basically, a copy paste operation and you can encrypt your entire document by passing in the root element!

-Feelsgoodman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM