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

Started by
18 comments, last by Paclaw 17 years, 6 months ago
Quote:Original post by Hard Rock
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....


I"m not sure why the need for the base64 encoding after the encryption. If you encrypt your xml then its no longer xml so you don't need to worry about the base64 encoding.

The only exception I can think of is perhaps you meant only encrypt part of the file??

Cheers
Chris
CheersChris
Advertisement
If there is an encrypt-solution, I meant encryption of the entire file.
Quote:Original post by CodeMachine
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.


Well, that's exactly what I said , some posts above.

Load your Xml into the DOM, take the text of the parent node ( I think there was a method for getting the raw data of a parent node). Calculate some hash function of that data ( maybe use MD5 ) and store that inside a node in the Xml.

An example...

Suppose initialy you have

<?xml version="1.0"?><root><hiscores><user name="name1" score="1000"/><user name="name2" score="7000"/></hiscores><hash code="" /></root>



After loading and calculating the parent (<root> node) data, you store the hash key inside the <hash> node

<?xml version="1.0"?><root><hiscores><user name="name1" score="1000"/><user name="name2" score="7000"/></hiscores><hash code="9e107d9d372bb6826bd81d3542a419d6" /></root>



Then whithin the game, you load again the Xml document into the DOM, get all that parent (node <root>) data, and replace the <hash code=...." /> with emtpy string , so <hash code="" /> and calcualte the hash again. If it is the same as the stored value, then the user *has not* changed the file !

.

Hi Pro-Xex!

Yes, I know you mentioned that solution above. :)
That solution seems to be the easiest one.
Thank you

/ Kind Regards
One more thing - I don't know what Xml parser are you using , but
if you are using MSXML, mind that it has the bad habbit to convert
tag forms.
I mean if you have
<hash code="..." ></hash> it might become <hash code="..." />, after you load it into the DOM. I suppose this could affect the hash calculations.

I'm not exactly sure, better you check this ;)

Cheers!

.

I will :)
Thanks dude for the information.
I'll come back with a result - wheather I succeed or not. :)

/ Kind Regards
Quote:
I"m not sure why the need for the base64 encoding after the encryption. If you encrypt your xml then its no longer xml so you don't need to worry about the base64 encoding.

The only exception I can think of is perhaps you meant only encrypt part of the file??

Sorry, I guess i wasn't clear. You could do it that way, but all i recommended encrypting was the actual information inside the nodes, not the entire XML document. This way base64 is necessary,as there is a possibility that the encrpyted data has illegal characters for the XML document.

Quote:
One more thing - I don't know what Xml parser are you using , but
if you are using MSXML, mind that it has the bad habbit to convert
tag forms.
I mean if you have
<hash code="..." ></hash> it might become <hash code="..." />, after you load it into the DOM. I suppose this could affect the hash calculations.

I'm not exactly sure, better you check this ;)

Cheers!


Well um, considering it's the java forum, its probably most likely the java parser...

Allthough google shows that MSXML has indeed been ported to Java interestingly enough.
Hard Rockhttp://www.starsdev.com
Hello Pro-Xex.

Now I've implemented the MD5 security check.
I'm using JDOM (http://www.jdom.org) to save the XML-file in memory. I chose
this one instead of "org.w3c.dom" which I used before, because I think JDOM is
better? :)

I think it's easier to use XPath too with JDOM.
Is JDOM one of the best to use? I've seen "DOM4J" (http://www.dom4j.org) too.

Alrighty then.
Now I have my XML-file loaded into an org.jdom.Document object. To calculate
the MD5 hash-value I get (as you said) the text-content. First I tried with:
- document.getDocumentRoot().getText();
but it didn't return the nodes correctly. I don't know why??? I get an empty string.

Instead, I saved the document to a String using org.jdom.output.XMLOutputter:
StringWriter stringWriter = new StringWriter();new XMLOutputter().output(document, stringWriter);


Doing this I get the whole document as text.
Now I calculate the MD5-hash value as you said, and it works fine!

Btw, the parser saves the "hash" node as:
<hash value="" />

/ Kind Regards
If you just want to encrypt the file, why not use simple algorithms? I know that wikipedia also some info on some techniques (Caesarean is basic). It sounds like you're trying to protect some super-secret government thing, just gamers who want to make it look they did better than they really did.

I did some basic encryption for a tabbed-notepad-like program a couple of months ago. I used the following:

public interface Cipher{    String encrypt(String in);    String decrypt(String in, Object key);}


Then tried out several different algorithms from this. Granted, it's not very sophisticated and probably a cake-walk for dedicated professions to break. However, it would suit your purposes. Encrypt the file when writing it, and decrypt it as you parse it. I'm not sure how this would fit in with your parser, however.
you could zip the file.
Paclaw

This topic is closed to new replies.

Advertisement