Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving and Loading Data C++

Tags:

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?

like image 776
lambda Avatar asked Jul 04 '13 01:07

lambda


2 Answers

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.
like image 158
DXsmiley Avatar answered Oct 04 '22 17:10

DXsmiley


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.

like image 20
D.Pham Avatar answered Oct 04 '22 18:10

D.Pham