简体   繁体   English

保存和加载数据C ++

[英]Saving and Loading Data C++

I know of a crude way of saving program data, keeping it in a text file. 我知道保存程序数据的粗略方法,将其保存在文本文件中。 The problem is that anyone has access to the text file and can manipulate it. 问题是任何人都可以访问文本文件并可以操作它。 For example I want to save and load game data which changes as the game progresses. 例如,我想保存并加载随游戏进行而变化的游戏数据。 What methods are there to store such data and keep it accessible only within the game program so it isn't manipulated by others? 有哪些方法可以存储这些数据,并且只能在游戏程序中访问它,以免被其他人操纵?

The idea is to format your data in such a way so that it is not readable by humans. 我们的想法是以这样的方式格式化您的数据,使其不被人类阅读。 This can by done using a number of methods. 这可以使用许多方法完成。

  • Save data as binary. 将数据保存为二进制。 This makes the file difficult to read by humans, so a hex editor or similar program needs to be used. 这使得文件难以被人阅读,因此需要使用十六进制编辑器或类似程序。 It also makes it difficult to see where one value ends, and another begins (in a text file this is shown by white space). 它还使得很难看到一个值结束的位置,另一个值开始(在文本文件中,这由空白显示)。 The other advantage this has is that your files will be much smaller. 另一个优点是你的文件会小很多。
  • Data encryption. 数据加密。 Data can be encrypted using various methods. 可以使用各种方法加密数据。 This is a fairly secure option, however encryption can be slow, so for very large amount of data, this may not be optimal depending on how fast the data needs to be saved/loaded. 这是一个相当安全的选项,但加密速度可能很慢,因此对于非常大量的数据,这可能不是最佳的,具体取决于需要保存/加载数据的速度。 There is also the issue of storing the encryption/decryption key somewhere (possibly in the file itself, or as a constant one in the program). 还存在将加密/解密密钥存储在某处(可能在文件本身中,或作为程序中的常量密钥)的问题。
  • Obfuscation. 混淆。 This is a broad term which refers to making things difficult to read. 这是一个广义的术语,指的是使事情难以阅读。 For example, two strings could be interlaced in a attempt to make both unreadable. 例如,两个字符串可以交错以试图使两者都不可读。 Obfuscation often involves inserting allot of junk data, information which is not used by the program, and only serves to confuse would-be hackers. 混淆通常涉及插入大量垃圾数据,该程序未使用的信息,并且仅用于混淆潜在的黑客。
  • Validity checks. 有效性检查。 In the case that somebody does modify the file, and they don't know what they are doing, it's highly likely that they will have some visible effect on the program. 如果有人修改了文件,并且他们不知道他们在做什么,那么他们很可能会对程序产生一些明显的影响。 In this way, they can simply fiddle with the file to see how your program reacts. 通过这种方式,他们可以简单地摆弄文件,看看你的程序如何反应。 One way to counteract this is to check whether all data is within a logical range. 抵消这种情况的一种方法是检查所有数据是否在逻辑范围内。 Data can also be stored multiple times in the file, obfuscated or encrypted in different ways. 数据也可以在文件中多次存储,以不同方式进行模糊处理或加密。 If the data differs, you know the file has been tampered with. 如果数据不同,则表示文件已被篡改。

You should store your data in binary files. 您应该将数据存储在二进制文件中。 But people still can change it anyway (like I often change skill point or stat point of my character in Diablo2 :D) so you should consider find a algorithm to calculate hash sum and store it within your data. 但人们仍然可以改变它(就像我经常在Diablo2:D中更改我的角色的技能点或统计点)所以你应该考虑找到一个算法来计算哈希值并将其存储在你的数据中。

You could save the data in a binary file. 您可以将数据保存在二进制文件中。 Just write and read the bits from and to the c++ object. 只需从c ++对象写入和读取位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM