Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I made a simple program that takes a file .dat that is encrypted in Binary Format, then I decrypt it into a byte array and then everything is rewritten again into the file decrypted.

For Example:

I've made a binary encryption algorithm. => 0100100001100101001000000110100001100101011000...

But this is very very easy to decrypt...

Is there any way, that, if it's possible does not need any sort of key, to encrypt my save progress?

share|improve this question

closed as off-topic by Alexandre Vaillancourt, Josh Petrie Mar 21 at 15:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Alexandre Vaillancourt, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

15  
Are you sure that you want to encrypt saves? There are easier ways to cheat, for example when using memory modification. I would just compress them with zlib (put them into .gz), it would reduce size and prevent some of possible cheaters from modifying it. – HolyBlackCat Jan 23 at 12:43
36  
Nitpicking: What you are doing is an encoding, not an encryption. – Philipp Jan 23 at 14:23
13  
(assuming this is for single player) Don't try to prevent cheating in a single player game, it's annoying both for you and for the player. And is ultimately useless. – user15805 Jan 23 at 15:29
6  
It is not possible to encrypt save files so that the user can't read or modify them. (Because, for example, they could just pause your game in a debugger right after it does the decryption!). However, you can make it harder (because opening a plain text file is really easy compared to finding the right moment to pause the game in a debugger). Is that what you're trying to do? – immibis Jan 24 at 9:23
6  
@DanielBejar I just told you, you can't make it impossible, you can only make it harder. – immibis Jan 24 at 11:52
up vote 78 down vote accepted

In general you should never invent your own cryptographic algorithms, unless you have at least a PhD in both mathematics and computer science. But there are many good stock algorithms which have no known attacks and have free implementations in many programming languages. For example RC5, AES or Blowfish. Depending on which technology you use to develop your game, it might even offer secure encryption out-of-the-box.

However, the question is if encrypting savegames is a good idea.

First, when you have your game executable do the encryption and decryption, you have to include both the algorithm and the key in your game executable. That means a determined hacker can find them, extract them, and use them to build a savegame editor. So it can never be 100% secure.

Second, why do you want to do this anyway? When it is an online game, you should store the gamestate online where the players can not modify it. When it is an offline game, then why bother? A cheater can only hurt their own game experience at worst. The honest players who want to enjoy your game as intended won't be affected by this at all. On the other hand, allowing players to cheat can add value to your game. It allows players to experience the game in a different way which can just increase their long-term enjoyment of your game.

share|improve this answer
5  
Though I can't imagine someone coming up with something good in a few hours time. And you'd be wasting resources on something that you probably don't need. – deadsource Jan 23 at 20:49
6  
"When it is an offline game, then why bother?" What if it's a single-player game with online highscores? – The Guy with The Hat Jan 24 at 5:01
19  
"allowing players to cheat can add value to your game" - upvoted for this statement. I am really annoyed by games that decide to lock parts of the experience that I paid for until I achieve a certain level of mastery, and I am glad that in these cases, there are cheats available to unlock the full game. For instance, I am totally lost in the racing tournaments against the AI in the older Need for Speed parts; to me, the enjoyment was rather in driving through the varied landscapes, hence I usually skip the racing and unlock all maps and cars. A game that prevents this would just annoy me. – O. R. Mapper Jan 24 at 11:44
6  
I would like "unless you have at least a PhD in both mathematics and computer science" rewritten as "unless you have at least the knowledge that would be required to gain a PhD in both mathematics and computer science" (and even then, at least in Germany, courses are often full of stuff not related to crypto). One does not inherently need a degree to outperform PhDs. – phresnel Jan 25 at 8:56
5  
@phresnel While you're technically right, a PhD is a standard metric of one's mastery of a theoretical subject, as awarded by trusted institutions. So it makes sense to use it as a shorthand for the amount of knowledge and skill needed to achieve the PhD. In other words: what the OP meant is clear enough :-) – Tobia Jan 25 at 11:59

To encrypt and decrypt locally, you will need to store the key in your program, so people will be able to crack the encryption if they disassemble your code. There are tricks to obfuscate keys, but even with those a dedicated attacker will find the key and then post it online for all those who are less dedicated to see. The only thing encryption does prevent is people who use a hex editor - you can easily foil that with compression and anything that makes the file unreadable by an unzip program. E.g. add one byte in front of the data.

Alternatively you can append the md5 of the savefile at the end of the file, so you'll notice if a file has been tampered with. You can then use this to either refuse loading the savegame, or to flag support tickets appropriately so you don't waste time on issues caused by manually modified savegames. Again, this is trivial to circumvent by anyone who can read your code.

If you want something that can't be circumvented by someone who has access to the binaries on their device, you need to run part of the program on a server, which is under your control.

To answer your last question, a key plus encryption type just identifies the exact algorithm that needs to be applied to encrypt/decrypt. You cannot encrypt/decrypt data without an algorithm that defines how to do so.

share|improve this answer
    
Your second point is nice. It could also help track problems in the case of file corruption. – knowledge_is_power Feb 12 at 9:13

Store a transformed SHA256 hash of the actual save data in the save file.

Compare the stored hash to the SHA256 value of the data as you load it.

If that doesn't match, they've cheated or corrupted the file.

Edit to clarify: this makes it more difficult to crack the anti-cheat system but still possible.

share|improve this answer
7  
Nothing stops people from recalculating the hash after they made the modifications. Also, I don't understand why there are still people who advocate the broken MD5 algorithm with all its known cryptographic weaknesses when there are far better alternatives like SHA-256 (for signatures) or bcrypt (for passwords). – Philipp Jan 23 at 14:50
1  
Simple, fast, and completely broken. – DeadMG Jan 23 at 15:30
2  
@DeadMG It's broken by design, because the user has access to the binaries which must contain the key. Using a stronger hash or encrypting the file with something like AES would be a waste of resources, and bad design, because it provides no added security, but does add cost. +1 because the downvote is undeserved. – Peter Jan 23 at 16:36
7  
@Philipp because in this case MD5 is clearly superior to SHA-256. What is required by the described approach is a cheap checksum algorithm, which is exactly what MD5 is. – Peter Jan 23 at 16:48
3  
@transistor09 if it's purely client side, we can't ensure anything and a checksum works just as well as having the client encrypt the whole thing. Either way, it can be cracked. – Lolums Jan 24 at 14:46

I would use a standard encryption algorithm (because it makes it harder to perform crypto-analysis on the saved file) but not one of the top five most used, and hide it throughout your code (because it makes it harder to understand what algorithm it is and where is the key.)

To hide the encryption algorithm into your code: take an open source version, understand its flow (which functions call which others an in what sequence), then rename all variables, struct fields and function names (just to be sure) and spread the functions all over your codebase, in different objects and modules, possibly separating their execution temporally as well (perform one piece of the encryption, then do something else, then after some ms come back to it from a seemingly unrelated piece of code and perform another phase of the encryption, and so on.) The key can be hidden in the same way, as a number of seemingly unrelated constants throughout your classes, that are accessed at runtime by the various hidden parts of the encryption algorithm.

Whether all this makes economical sense is another topic altogether.

share|improve this answer
    
I don't know... Making it that harder for just a game... But thanks for the help! Ill try my best! – Daniel Béjar Jan 25 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.