我过去曾使用此代码来编写和读取 xml 文件。这次我想写一些加密生成的 XML,然后在内部读取并处理它。我将发布代码,也许有人可以发现问题。
当我测试解密时,我已经能够输出一个包含连续空字符代码行的文件。加密文件似乎包含数据,并且大小随数据量的不同而变化。
请帮忙,谢谢!
加密
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();
解密
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;