以加密或不可读或二进制格式保存和加载字典?

发布于 2024-10-09 09:41:59 字数 1169 浏览 0 评论 0原文

我有一本如下的字典:

public Dictionary<int, SpawnList> spawnEntities = new Dictionary<int, SpawnList>();

正在使用的类如下:

    public class SpawnList
    {
        public int NpcID { get; set; }
        public string Name { get; set; }
        public int Level { get; set; }
        public int TitleID { get; set; }
        public int StaticID { get; set; }

        public entityType Status { get; set; }

        public int TypeA { get; set; }
        public int TypeB { get; set; }
        public int TypeC { get; set; }

        public int ZoneID { get; set; }
        public int Heading { get; set; }
        public float PosX { get; set; }
        public float PosY { get; set; }
        public float PosZ { get; set; }
    }

/// <summary>Entity type enum.</summary>
public enum entityType
{
    Ally,
    Enemy,
    SummonPet,
    NPC,
    Object,
    Monster,
    Gatherable,
    Unknown
}
  • 我怎样才能将此字典保存到 二进制或加密格式 这样我稍后可以将其再次加载到 我的申请?

    我这里最大的问题不在于如何自行保存文件或对其进行加密,而主要在于当您使用该文件进行课程时如何执行此操作以及稍后如何对其进行反序列化。

我的限制是.Net 3.5 不能使用更高的版本。

I have a dictionary like the follow:

public Dictionary<int, SpawnList> spawnEntities = new Dictionary<int, SpawnList>();

The class being used is as follow:

    public class SpawnList
    {
        public int NpcID { get; set; }
        public string Name { get; set; }
        public int Level { get; set; }
        public int TitleID { get; set; }
        public int StaticID { get; set; }

        public entityType Status { get; set; }

        public int TypeA { get; set; }
        public int TypeB { get; set; }
        public int TypeC { get; set; }

        public int ZoneID { get; set; }
        public int Heading { get; set; }
        public float PosX { get; set; }
        public float PosY { get; set; }
        public float PosZ { get; set; }
    }

/// <summary>Entity type enum.</summary>
public enum entityType
{
    Ally,
    Enemy,
    SummonPet,
    NPC,
    Object,
    Monster,
    Gatherable,
    Unknown
}
  • How could I save this Dictionary to
    either a binary or encrypted format
    so I could later Load it again into
    my application ?

    My biggest problem here is not on how to save the file it self or encrypt it but mostly on how to do it when you have a class with it and how you go about deserializing it later.

My limitation is .Net 3.5 can't use anything higher.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眼角的笑意。 2024-10-16 09:41:59

将序列化方面与加密方面分开。序列化数据有多种不同的方法:

  • 编写自己的序列化代码
  • 使用内置的二进制序列化程序
  • 使用内置的 XML 序列化程序
  • 使用第 3 方序列化框架,例如 Thrift 或 Protocol Buffers

所有这些都可能允许您对流进行序列化和反序列化。那么您应该考虑使用 CryptoStream 处理加密/解密 - 但请注意,如果您自己的代码需要能够在没有额外信息的情况下解密数据,那么它将需要拥有可用的密钥...这基本上意味着任何能够运行该代码的人都可以使用它。

Separate the serialization aspect from the encryption aspect. There are various different ways of serializing the data:

  • Write your own serialization code
  • Use the built-in binary serializers
  • Use the built-in XML serializers
  • Use a 3rd party serialization framework such as Thrift or Protocol Buffers

All of these are likely to allow you to serialize and deserialize to/from a stream. Then you should look at using CryptoStream to handle the encryption/decryption - but be aware that if your own code needs to be able to decrypt the data with no extra information, then it will need to have the key available to it... which basically means it'll be available to anyone who is able to run the code, too.

左秋 2024-10-16 09:41:59

将类标记为

[Serializable]

并将其序列化为任何 Stream 使用

FileStream stream = File.Create("binary.bin");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, spawnEntities);
stream.Flush();
stream.Close();

并使用反序列化它

stream = File.Open("binary.bin", FileMode.Open);
spawnEntities = formatter.Deserialize(stream) as Dictionary<int, SpawnList>;
stream.Close();

Mark the class as

[Serializable]

and serialize it to any Stream using

FileStream stream = File.Create("binary.bin");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, spawnEntities);
stream.Flush();
stream.Close();

And Deserialize it using

stream = File.Open("binary.bin", FileMode.Open);
spawnEntities = formatter.Deserialize(stream) as Dictionary<int, SpawnList>;
stream.Close();
夏日落 2024-10-16 09:41:59

入门的简单方法:

  1. 通过附加 [Serialized] 属性使您的类可序列化。
  2. 使用 CryptoStreamBinaryFormatter 将其序列化为文件并加密数据。

CryptoStream 的参考提供了设置加密和执行相关文件处理的完整示例。

The easy way to get you started:

  1. Make your class serializable by attaching the [Serializable] attribute.
  2. Use CryptoStream and BinaryFormatter to serialize it into a file and encrypt the data.

The reference for CryptoStream provides a complete example for setting up encryption and doing the related file handling.

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