[java] How to "encrypt" a XML-file?

Started by
18 comments, last by Paclaw 17 years, 6 months ago
Hi I have an XML-file which holds highscores for a game of mine. I now want the user to be unable to changes this higscore list manually. How do you best solve this problem? 1. "Encrypt" the XML-file? 2. Save a check-sum in the XML-file? / Kind Regards [Edited by - CodeMachine on October 5, 2006 3:09:53 PM]
Advertisement
I would go with option 2), maybe like this :


1. create your empty .xml high-score file, and calculate a md5() of the empty contents (just the nodes , default hiscore names, maybe without the processing instruction).

2. Store the md5() hash inside a node in the .xml file once you calculate it.

3. When it comes time to save the highscore list within your game

3.1. open the .xml and read the md5() hash node
3.2. replace the md5() hash node content with empty string and precalc the md5() of the xml file
3.3. compare the two hash codes, and resest the high-score files if they don't mach.
3.4. if they match then just save the new scores, precalc the md5() hash , refresh the md5 node content and save the .xml file

This should do it ! :)

.

Alternatively, you can use any encryption scheme you want on the xml data, and then base64 encode the encrytped data.

To read it back, simply convert from base64 to the encrypted data and unencrypt.

The reason for the base 64 encoding is that XML forbids characters greater then a certain value, so that is the easiet way around the problem. Keep in mind that your files will more or less double in size....
Hard Rockhttp://www.starsdev.com
More simply, why bother. The only person that will see the high scores is the person owning the computer. Maybe a couple of friends. Unless you are having some sort of competition, then it is really a waste of time. And if you are having a competition, you should use a high score server instead.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quote:Original post by CaptainJester
More simply, why bother.


Now I DO want this kind of security, else I would NOT have posted my question.
Thanks for your answers Pro-XeX & Hard Rock.

Hard Rock, is there a good tutorial out there?
Or maybe you have time to construct an example? :)

/ Kind Regards
Well, there are a lot of places you can get info about base64, not so hard really ;)

.

The thing is that I want to:
1. Read XML file (to a Document object).
2. Decrypt the Document object.
3. Make changes (XPath/DOM).
4. Encrypt it.
5. Save it to file.

/ Kind Regards
If you do not want the user to be able to edit the file, you might consider just not using a file format that is so easy to edit. It seems that this is a case where XML is not well suited to the job. You could just as easily use a binary file, and not have to worry overmuch about the user changing the highscores. In fact, it seems like it would be far easier than encrypting the file or mucking about with base64.
The reason I am using XML is because it's very easy to retrieve/sort/add/edit data using XPath/DOM.
I think I have to calculate a CRC32 or something for all the nodes in the XML-file, and store this CRC-value in a separate node.

This topic is closed to new replies.

Advertisement