# How can I write data to a file that users can't easily edit? [duplicate]

Possible Duplicate:
How can I protect my save data from casual hacking?

While working on game saving and loading I figured I could just write all the variable values to a file and then load that file from it's default location anytime.

However from the very beginning it sounded like an odd job. I know about serialization and boost, but that seems so complicated, I figured I'd keep it simple, but I've ran across this huge issue: No matter what file I can write with C++, the user can get their hands on it, they can edit their position, they can remove a boss, or add new weapons for themselves.

My question here is: How can I create a file in C++ which cannot be editted or openned with a text editor such as Notepad (I'm not trying to make a file which is impossible to open, but a file which will give the user a headache if he tries to edit it through usual methods.)

• Can you use binary files? – wolfdawn Oct 13 '12 at 18:06
• Also whats wrong with letting the user mod the game? – ClassicThunder Oct 13 '12 at 18:07
• I've already mentioned I don't want to use boost, I've got a lot of game dependencies already, why create more dependencies for my game when I can find alternative methods? – Bugster Oct 13 '12 at 18:07
• @ClassicThunder He wants to make cheating less accessible. Cheating can make a game boring very quickly. – wolfdawn Oct 13 '12 at 18:09
• @ClassicThunder - modding is a very different concept from hacking save files. Hacking save files is a clear case of cheating; modding is creation of new game content, and the two are not the same thing. – Maximus Minimus Oct 13 '12 at 23:45

## 5 Answers

Encrypt it. It's really that simple. Since you're trying to discourage casual editing (rather than a dedicated hacker), the encryption algorithm could be fairly simple. There's no need for PGP or something. You could use ROT13. Or develop a substitution cypher of your own.

• That example has actually provided an excellent answer, I will do that. Thanks! – Bugster Oct 13 '12 at 18:23
• +1 Simple encryption is the way to go. ROT13 indeed, or a simple XOR. – Laurent Couvidou Oct 14 '12 at 20:41

You can just use a binary format and write the data structure to the file itself. To verify you are opening a proper and uncorrupted file, add a "magic" string to the data structure and check the string each time it's loaded (this is just a simple way to do it). An example of how to do this can be found here. Encryption can also be added with little hassle.

• If you're going to vote this down, please explain why. – Blue Wizard Oct 13 '12 at 20:49
• I have no idea why this got a -1; writing as a binary file is a perfectly valid suggestion. Also +1 for "If you're going to vote this down, please explain why" - rep is useless, and downvotes are even more useless, without an explanation of why it was voted so. That way it can be of benefit to everybody. – Maximus Minimus Oct 13 '12 at 23:43
• By the time I saw this answer it didn't have a -1 anymore, and it's relevant to my question so you'll get a +1 from me :) – Bugster Oct 14 '12 at 8:00

One last solution that I didn't see in other's answers is to add check some to your save data. For example you can calculate sum of all characters in your save file (excluding checksum) and print it at the end of file. this way if someone changes some characters in your file you can easily notice them. you can also spread checksum bytes over your save file to make them less noticeable. maybe if you are using MD5 checksum and you expect your save file to be 256 characters length, you can save checksum in bytes [2,4,6,....,512]

• This is called Message-Digest, as MD5's "MD" suggests, and is primarily intended to guarantee the file has not been altered. Note however that it's effectiveness is debatable in an local scope, as any serious hacker can simply modify the file and generate a new verification-code (AKA "hash" and "checksum"), successfully modifying the file without a problem. --- Still, message-digest is very suitable for the question here, and, if well-applied (coupled with encryption) makes the files almost invulnerable to casual hackers, so a +1 is well deserved. – XenoRo Oct 15 '12 at 17:39

From the comments I'm guessing you are currently using plain text as your saving method. If you want a quick fix to make things less accessible to the average user, instead of saving your text as it is, edit each character to be 255 - charval of what it currently is and then when reading the file switch it back.

You could also do this: char = (char + 100)%256 for writing and char = (char + 156)%256 for reading.

If you want something more complex, please feel welcome to comment and I will suggest a stronger form of protection.

For example if you wish to continue using text you can randomize an Injective function that receives a character and returns another character. An injective function has an Inverse function, which you can then use to read the text.

To create an injective function randomly, simply shuffle an array of integers from 0 to 255 and use the value in the index i to represent 'i' in your files.

To create the inverse function, create another array and run over the array like this:

for(int i = 0; i <= 255; i++)
{
B[A[i]] = i;
}


Where A is the original array and B is the new one.

You can save these arrays in your code.

While this is not impossible to crack, I suspect I would not bother trying. I imagine most gamers (99% +) would avoid trying to edit a file like this.

If you really want to go the extra mile, there are some encryption techniques like RSA that are not very complicated to implement.

• That's quite an interesting alternative, if you do know more (no matter how complex, as long as they don't provide huge dependencies) post them too in case someone in the future may find this question :) – Bugster Oct 13 '12 at 18:15
• (-1) - This solution only achieves making the text unreadable to the common user, which rarely bother going to the game's install directory after install in the first place. --- Such masking methods are completely useless against anyone seriously intending to modify the file, as they are easily figured out. --- If you only need such level of protection, saving as binary is the way to go. Not only it makes the files unreadable to humans, it makes them easier for the machine; Effectively reducing processing instead of increasing it. --- No offence, but @Arthur's solution is garbage. – XenoRo Oct 15 '12 at 16:58
• @TheLima RSA "only achieves making the text unreadable to the common users" I want to see proof to support your comment against RSA and randomized Injective function protection. I would otherwise suspect that your criticism is baseless nincompoopery. Back that up. Explain how you would crack it? I am willing to provide test code and see how long it takes you to crack it and get the original data decrypted without a key. I mean with only 256 integers in that range, it leaves you with 256! that is not an exclamation mark. That is a factorial. Which is about 8 * 10^506 – wolfdawn Oct 16 '12 at 8:11
• Technically all methods only achieve making the text unreadable to the common user (because the program itself has to be able to read it, therefore an expert user can always get at the unencrypted data too). It's just different degrees of "unreadable". I think this simple method is very reasonable. – Ricket Oct 16 '12 at 14:03
• @ArthurWulfWhite With 255 - charval you have a [Caesar's cipher][1] method, and char = (char + 100)%256 ain't much better; In the first case, the worst-case-scenario takes only 255 attempts to crack! Anyone with the bare minimum experience can easily find the cipher for such weak methods, subsequently posting them on the internet for your cheating-users pleasure. Nevermind automatic cracking made by hackers, any user could crack the files by hand, probably with less effort than cheating with [Cheat Engine][2]. [1]: en.wikipedia.org/wiki/Caesar_cipher [2]: cheatengine.org – XenoRo Oct 16 '12 at 16:47

A method I used was to save the file with the extention of .csx instead of .txt. It's visible, but a user can't readily figure out how to open it, unless they change the extension to .txt again.

• File extension does not prevent people from viewing the contents. – Justin Skiles Oct 15 '12 at 2:06
• @JustinSkiles For the very novice user it does :) Since it doesn't have a default program to open it, some users may give up. Though the users that would go looking for such files likely wouldn't be turned away by such methods. – MichaelHouse Oct 15 '12 at 7:04
• @Byte56 I don't think so. As you mention yourself, users who would even bother visiting the game's install directory in the first place would not be turned away by a file-extension. Even on the best-case-scenario of them not already knowing how to deal with it, most of them would be all-set with less than 30 seconds of google-searching; Rendering such protection methods completely useless unless paired with something else. – XenoRo Oct 15 '12 at 17:24
• @TheLima I'm not saying it's a good answer. Not even close. My comment was in response to a "not an answer" flag. My comment is to establish my opinion that it is an answer, though not a good one. We're in agreement. – MichaelHouse Oct 15 '12 at 17:28
• I didn't think you were saying it was a good answer, I just said I disagree with "For the very novice user it does" and that, for the reasons I talked about, and that you mention yourself ("Though the users that would go looking for such files likely wouldn't be turned away by such methods."), no users who would bother modifying the files in the first place would be turned away. --- Basically, I consider the first contradictory to the second. ;) – XenoRo Oct 15 '12 at 17:51