gpt4 book ai didi

c# - 读取/解密加密的 XML 文件,然后在内部处理

转载 作者:行者123 更新时间:2023-11-30 21:07:15 28 4
gpt4 key购买 nike

我过去曾使用此代码编写和读取 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;

最佳答案

使用您当前的体系结构,您需要使用刚刚填充数据的 MemoryStream(不要忘记将其位置重置为零并刷新任何待处理的写入)

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

但是,我的感觉是您在这里不需要 MemoryStream。相反,只需将 CryptoStream 提供给 XmlTextReader:

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

关于c# - 读取/解密加密的 XML 文件,然后在内部处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com