以加密或不可读或二进制格式保存和加载字典?
我有一本如下的字典:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将序列化方面与加密方面分开。序列化数据有多种不同的方法:
所有这些都可能允许您对流进行序列化和反序列化。那么您应该考虑使用
CryptoStream
处理加密/解密 - 但请注意,如果您自己的代码需要能够在没有额外信息的情况下解密数据,那么它将需要拥有可用的密钥...这基本上意味着任何能够运行该代码的人都可以使用它。Separate the serialization aspect from the encryption aspect. There are various different ways of serializing the data:
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.将类标记为
并将其序列化为任何 Stream 使用
并使用反序列化它
Mark the class as
and serialize it to any Stream using
And Deserialize it using
入门的简单方法:
[Serialized]
属性使您的类可序列化。CryptoStream
和BinaryFormatter
将其序列化为文件并加密数据。CryptoStream
的参考提供了设置加密和执行相关文件处理的完整示例。The easy way to get you started:
[Serializable]
attribute.CryptoStream
andBinaryFormatter
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.