Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 5534 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was wondering, how do I encryipt a file? I don''t want someone to just be able to stroll right in my files and edit stuff like damage, persay. So could anyone give me some code that encryipts a file? I''m using a Visual c++ 6.0 complier and I''m doing this in C#/C++ Thanks, Demon Realms!!!!!

Share this post

Link to post
Share on other sites
You could use the XOR encryption algorithm, which is simple, but I''ve had problems with it in the past. A quick search on Google should provide enough information about that if you want to use it.

Also, I know there is an EncryptFile() and a DecryptFile() function. Personally, I haven''t tried those yet, but you could give it a try. Here''s what I found on MSDN:


Hope this helps.

Build a man a fire, and he''ll be warm for a day. Set him on fire, and he''ll be warm for the rest of his life.

Share this post

Link to post
Share on other sites
If it''s a pretty simple game, I think even some sort of character shift would be encryption enough to throw off most people who want to edit it. Still, for a single game (presumably which you are making) I almost wouldn''t bother. I figure if they want to cheat in single, more power to them

That being said, I''ve always wanted to try some XOR encryption, after reading a post about it. Seems pretty powerful, and good enough for most cases.

Share this post

Link to post
Share on other sites

void XOR_Encrypt(void *buffer, int length, char key)
unsigned char *ptr = (unsigned char *) buffer;
int index;
for( index = 0; index < length; index++ )
ptr[index] ^= key;

call the function with a key between 0-255 to encrypt.. call it with the same function to decrypt.. XOR encryption is really easy but tends to keep the script kiddies away :-)

hmm I don''t even remember if ^ is the XOR operator.. and I am too lazy to look it up

Share this post

Link to post
Share on other sites
Kill two birds with one stone: Compress the files using something like ZLIB - to make them much harder to access/reverse engineer* you could also alter the compression ever so slightly - for example bitwise NOTing every other byte in the compressed stream.

* it depends what this is for, that approach, and indeed some of the even simpler ones already mentioned will prevent the casual intruder. But [usually depending on the value/importance of the data] a really determined cracker will _eventually_ get around ANY encryption you have, the complexity just increases the time taken.

For example if the unencrypted data exists in memory at any point in time, then it can be trivially accessed externally in a variety of ways. Likewise with decryption provided as a generic function call - a determined cracker will use your own decryption code to decrypt data.

Simon O''Connor
Creative Asylum Ltd

Share this post

Link to post
Share on other sites
Well, you can use other programs to encrypt your files or you can do it yourself, here is a simple way:

Once you have all the data, you save it in a text file unencrypted. (I assume you did that already)

Now, you make a function called Encrypt() that does:
- Gets you a long random number like "1234567890" called a key.
- Opens the original file and staring in the beginning, it will convert every character in the file in an ASCII number and add the corresponding value from the key to it.
For example: the first letter in the text file (ASCII 64) will become ASCII 65 (64+1). The second, ASCII 70 will become ASCII 72 (70+2). Etc. When you get to the 11th character (how long is your key + 1) in your file, you start the key from the start, so at the 11th you add 1, 12th you add 2, etc.
- Then it opens another file where you will eventually save the data, and after converting back from all the new ASCII codes to letters, you save the file and delete the original.

Now remember you have to hold on to the KEY, so you either save it in a separate file somewhere, in the same file or you hardcode it in your game.

To decode the file, you make a function Decode(), that does the opposite of the Encrypt() function. Using the same KEY, you load the encrypted file, and instead of adding it to the characters, you subtract it, save the data in a new file, and let your game take over from there.

Remember to delete the decrypted file if your not using it, because it will be easier to find the key if you have the original and the encrypted file, if the guy tiring to hack your game has the time, patience to figure out how to decrypt it.
(Not to hard if you have both files and you know it uses this technique)

So, basically the letter ''a'' encrypted might end up as: ''d'',''4'',''%'' or any other symbol you want including ''a''. (Something the Germans missed in ww2 )

So there you go, a simple way to encrypt files, work your way from this and you can encrypt it anyway you like.

Share this post

Link to post
Share on other sites
Wow! I just came back and checke donthis forum and its sky high with responds all ready! Thanks, I will try all of those and see whitch one''s easier.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!