You could simply pick a value between 0 and 255. Add this to each character in the file to encrypt it, subtract it from each character to decrypt... Simple, but not all that safe...
Hi Guys,
I would like to hide my game's file contents (INI file, dat files, etc.) and show garbage characters when opened in notepad so that the average user cannot edit it, does this mean I have to encrypt them?
Anyone has some simple encryption code to share?
Thanks!
You could simply pick a value between 0 and 255. Add this to each character in the file to encrypt it, subtract it from each character to decrypt... Simple, but not all that safe...
Nah... not add. You've to xor it.Originally Posted by MiCo Games
encrypt:
byte crypt=o_byte^key_byte;
decrypt:
byte decrypt=c_byte^key_byte;
However, I wouldn't bother doing stuff like that. Use some kind of pak format, because it makes life easier... the "security" you get there is about the same.
I used to use an XOR on each byte. You can make it more secure by picking a random number for each byte. I can go into more detail if you wish
That should be secure enough for most people, and will at least stop people poking around and changing values.
I can assure you, it works just as well if you addOriginally Posted by oNyx
just make sure that it wraps properly (will do if you use an unsigned char for the calculation).
I agree that it is not necessary though... Why would you not want people to be able to edit the files?
1) Add and XOR effectively work and have similar results.
2) XORing or adding to a string of zeros will give you the value. Beware. Probably not a problem with ASCII files
3) Use a "key" to XOR or to add instead of just a value. Pseudocode example :
Again, beware of encrypting a string of zeros, you will give away the key.Code:sKey = "thisismykey" for i = 0 to len(sData): sData[ i ] = sData[ i ] XOR sKey[ i % len(sKey) ]
4) To solve the "encoding zeros" problem, add the position i to the key (or better yet, some f(i)).
Note that this is trivial encryption as you requested, and therefore trivial to crack too.Code:sData[i] = sData[i] XOR (sKey[i % len(sKey)] + i)
Last edited by ggambett; 08-24-2004 at 05:42 AM.
Gabriel Gambetta
Google Zürich - Formerly Mystery Studio
You might just compress before saving and and uncompress on load, using zlib. Not encrypted, but your casual user wont be able to mess with it.
-Jeff
www.evertt.com
Thanks for the tip guys! I just opted for a simple XOR encryption since I just need a simple INI file to be encrypted. Compression maybe great but I felt that its too much for my needs.Just in case some of you may need some sample code, the link below has been very helpful for me.
http://download.cdsoft.co.uk/tutorials/xorencryption/