本文介绍了读取/解密加密的XML文件,然后在内部进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经使用在过去这段代码写入和读取XML文件。这一次,我想写一些加密生成的XML,然后读取它,进行内部处理。我会后的代码,也许有人能发现的问题。 当我测试解密我已经能够输出具有空字符的连续行的文件码。加密文件似乎包含数据,并在不同的数据量大小瓦里斯。 请帮忙,谢谢! 加密 的MemoryStream毫秒=新的MemoryStream(); 的XmlTextWriter的XmlWriter =新的XmlTextWriter(MS,Encoding.UTF8); 的FileStream EncryptedFileStream =新的FileStream(文件,FileMode.Create,FileAccess.Write); DESCryptoServiceProvider DES =新DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(AAAAAAAA); DES.IV = ASCIIEncoding.ASCII.GetBytes(AAAAAAAA); ICryptoTransform的desEncrypt = DES.CreateEncryptor(); 的CryptoStream的CryptoStream =新的CryptoStream(EncryptedFileStream,desEncrypt,CryptoStreamMode.Write); / *创建工作,这里测试的XML数据* / 字节[]字节组=新的字节[ms.Length] ms.Read(字节数组,0,bytearray.Length); cryptostream.Write(字节数组,0,bytearray.Length); cryptostream.Close(); EncryptedFileStream.Close(); xmlwriter.Close(); ms.Flush(); ms.Close(); DECRYPT 的MemoryStream毫秒=新的MemoryStream(); 的StreamWriter swDecrypt =新的StreamWriter(毫秒); DESCryptoServiceProvider DES =新DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(AAAAAAAA); DES.IV = ASCIIEncoding.ASCII.GetBytes(AAAAAAAA); ICryptoTransform的desDecrypt = DES.CreateDecryptor(); 的FileStream fsDecrypt =新的FileStream(mstrIndexFile,FileMode.Open,FileAccess.Read); 的CryptoStream cryptostreamDecr =新的CryptoStream(fsDecrypt,desDecrypt,CryptoStreamMode.Read); swDecrypt.Write(新的StreamReader(cryptostreamDecr).ReadToEnd()); swDecrypt.Flush(); ms.Position = 0; 解决方案 多的反复试验后,我指出来 XML元素加密使用网页上的第一个方法。这种方法是非常容易和简单。如果有人决定使用这个只是确保你使用相同的密钥和IV上的加密和解密,如果他们发生在不同的地方。 基本上,一个复制粘贴操作,你可以通过在根元素加密整个文档! -Feelsgoodman 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!EncryptMemoryStream 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();DECRYPTMemoryStream 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; 解决方案 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 这篇关于读取/解密加密的XML文件,然后在内部进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 23:18