Read/decrypt encrypted XML file and then process internally

喜欢而已 提交于 2019-12-12 19:25:14

问题


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;

回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/10539670/read-decrypt-encrypted-xml-file-and-then-process-internally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!