C# 将对象加密到 XML 文件
我正在尝试创建许可证文件,并且需要对其进行加密。
我有 License
对象和 List
。我需要先对流进行加密,然后再将其保存到 xml 文件中,以便不容易读取它。
我找到了这篇文章: MSDN 代码:编写类数据到 XML 文件 (Visual C#)
public class Book
{
public string title;
static void Main()
{
Book introToVCS = new Book();
introToVCS.title = "Intro to Visual CSharp";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\IntroToVCS.xml");
writer.Serialize(file, introToVCS);
file.Close();
}
}
以及这篇文章:CodeProject:在 C# 中使用 CryptoStream
编写 xml 文件:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.OpenOrCreate,FileAccess.Write);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateEncryptor(),CryptoStreamMode.Write);
byte[] data = ASCIIEncoding.ASCII.GetBytes(�Hello World!�);
crStream.Write(data,0,data.Length);
crStream.Close();
stream.Close();
读取 xml 文件:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.Open,FileAccess.Read);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateDecryptor(),CryptoStreamMode.Read);
StreamReader reader = new StreamReader(crStream);
string data = reader.ReadToEnd();
reader.Close();
stream.Close();
我很难尝试将两者结合起来。有人可以帮我吗?
I am trying to create a license file and I need it encrypted.
I have License
object and List<License>licenses
. I am needing to encrypt the stream prior to saving it to the xml file so that it can not be easily read.
I have found this post: MSDN Code: Writing Class Data to an XML File (Visual C#)
public class Book
{
public string title;
static void Main()
{
Book introToVCS = new Book();
introToVCS.title = "Intro to Visual CSharp";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\IntroToVCS.xml");
writer.Serialize(file, introToVCS);
file.Close();
}
}
And this post: CodeProject: Using CryptoStream in C#
Writing the xml file:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.OpenOrCreate,FileAccess.Write);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateEncryptor(),CryptoStreamMode.Write);
byte[] data = ASCIIEncoding.ASCII.GetBytes(�Hello World!�);
crStream.Write(data,0,data.Length);
crStream.Close();
stream.Close();
Reading the xml file:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.Open,FileAccess.Read);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateDecryptor(),CryptoStreamMode.Read);
StreamReader reader = new StreamReader(crStream);
string data = reader.ReadToEnd();
reader.Close();
stream.Close();
I am having a hard time trying to combine the two. Can anyone help me out here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您应该考虑使用 EncryptedXml 类。您不是加密 XML 本身,而是加密 XML 内容。
加密可能需要不同的加密强度、密钥库等方法。请遵循 MSDN 文档中的示例。这不是一个简短的实现,但效果很好。
Actually, you should consider using the EncryptedXml class. Rather than encrypting the XML itself, you encrypt the XML contents.
Encryption can entail different approaches for cryptographic strength, key bases, etc. Follow the examples in the MSDN documentation. This is not a short implementation, but it works very well.