简体   繁体   English

读取/解密加密的XML文件,然后在内部进行处理

[英]Read/decrypt encrypted XML file and then process internally

I have used this code in the past for writing and reading xml files. 我过去曾使用此代码来编写和读取xml文件。 This time I want to write some encrypted generated XML and then read it and process it internally. 这次,我想编写一些加密的生成的XML,然后读取它并在内部对其进行处理。 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 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) 使用当前的体系结构,您需要使用刚刚填充了数据的MemoryStream(不要忘记将其位置重置为零并刷新所有未完成的写操作)

//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. 但是,我的感觉是您在这里不需要MemoryStream。 Instead, just supply the CryptoStream to the XmlTextReader: 相反,只需将CryptoStream提供给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. 经过多次尝试和错误之后,我指向使用页面上第一种方法的XML元素加密 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. 如果有人决定使用它,只要确保它们在单独的位置进行加密和解密,请确保使用相同的KEY和IV。

Basically, a copy paste operation and you can encrypt your entire document by passing in the root element! 基本上,是复制粘贴操作,您可以通过传递根元素来加密整个文档!

-Feelsgoodman -Feelsgoodman

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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