C# 将对象加密到 XML 文件

发布于 2024-12-11 08:43:49 字数 2076 浏览 0 评论 0原文

我正在尝试创建许可证文件,并且需要对其进行加密。

我有 License 对象和 Listlicenses。我需要先对流进行加密,然后再将其保存到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-12-18 08:43:49

实际上,您应该考虑使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文