Converting plain text (string) to XML format ?

Hi there !

I’m using XML files to deal with my savegames. The thing is right now, if you manage to get access to the XML file in your systme file, you can actually read and even rewrite some stuff in it.

So, I’m trying to encode the data : it won’t be perfect since people will still probably be able to decode it, but that’s enough.

Without encryption, this is what I was doing :

  1. In my game, everything is currently saved into a LUA table.
  2. Everytime I (re)write the save file, the LUA table is converted into a XML structure and saved as a XML file.
  3. Everytime I load the XML save file, I simply convert it to a LUA table and use it.

Now that I’ve added the encryption, I’ve got this :

  1. In my game, everything is currently saved in a LUA table.
  2. Everytime I write the save file, the LUA table is converted to a XML structure, then encrypted and saved as TXT file.
  3. Everytime I load the TXT save file, I try to decrypt it, convert it to a XML structure and then, convert it as a LUA table in order to use it.

I’m stuck at the 3rd point : Is there a way to actually convert a text string into XML structure without having to save it as an XML file and then loading it again ?

EDIT : If I save the decrypted data into a new temporary XML file, it works, except the data seems to be corrupted like this (looks like some text encoding issue) :

\<?xml version="1.0"?\> ¦¡]:¡ $\_ö(ºl¨üµJ ù¯V¥on\> &nbsp;

Hi.  Sorry, but why XML instead of JSON?  Is this to make your development easier? i.e. Do you edit the XML content as part of your development cycle?  Do you have another tool or product consuming the XML?

Also, if you’re amenable to a small change consider: http://github.com/GlitchGames/GGData or other similar solutions using encrypted JSON encoded tables.

-Ed

Hey roaminggamer !

The reason I’m using XML instead of Json is that when I started dealing with save files, I only had a small experience with XML files and absolutely zero experience with JSON files…

That was a long time ago. Today, I do understand how Json would be a lot more effective and easy to use with LUA and Corona, but unfortunately I can’t afford rewriting everything.

Just in case : I’ve edited my original post and added the decrypted string that I intended to use.

  1. How are you encrypting your file?

  2. Honestly, I’d say leave encryption out of the equation.  If we’re just talking score, inventory, progress, and other similar stuff.  Let people do what they want.  They bought the game.  If they want to hack it they should be allowed.  

  3. Have you considered rolling your own encryption?  It’s pretty easy and even a basic solution like rot13 (or similar) is good enough for this use case.

-Ed

  1. I’m using this Corona Labs tutorial to encrypt/decrypt my files.

  2. I agree. In the end, I’m not even sure I’ll let the encryption in my game. I mainly do it to learn how to do it :slight_smile:

  3. Way out of my league (really…)  :slight_smile:

My gut tells me you’re making a mistake following the directions.

The premise of what you want to do should be:

Encryption

  1. Convert your data to XML.  This is stored as a (long) string in memory.

  2. Encrypt that string using the tutorial technique 

  3. Save the newly encrypted string to a file.

Decryption

  1. Load the encrypted content from file into a string in memory.

  2. Decrypt it.

  3. Now you have a XML-encoded table stored in a string.  Should match original.

My suggestion is that you test the encryption and decryption code you’ve written with a simple string.  If that doesn’t work you’re making a mistake  in the process.  i.e. Make a standalone app to test this process, then copy that code to your game.

I can’t think of a reason why the encryption tutorial would fail for a XML-encoded table stored as a string.

Oh, and what xml encoder are you using?  Hand-rolled or a library and if so, where did you get it.

I’m using “XML Parser by Jon Beebe”.

The encryption / decryption seems to work : I do have a long string that I’m even able to print out in the console. But the encoding doesn’t seem right : right after the “<?xml version=“1.0”?>” line (which is fine), the text looks messy with weird characters.

I’m gonna do more tests and try to figure out where’s my mistake !

Well. I’ve seen that corrupt big tables before.  I strongly suggest you use JSON over XML.  

It should be as simple as replacing the XML encode step with a call to json.encode(), then save that to a file or encrypt it.

If it helps at all, here is a sample with my own library of JSON save/load features:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/json.zip

You can upgrade this by modifying my table extensions to incorporate encryption and you’ll be golden.

Here is the same example, modified to incorporate a simple (not efficient) encryption process:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/easy_crypt_json.zip

TIP - Even if you don’t want to convert to JSON, you can use my encryption code (see ssk_core/security.json)  and that should be less likely to corrupt the encoded XML. (My guess is the encryption you’re doing is corrupting some specific characters.  My code handles that safely with a fallback case for characters it doesn’t have in its encode/decode dictionary.)

Here is an example of just the security code in use:

http://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015%20Answer%20Packs/8_August/answers/secureStorage

-Ed

I’m going to jump on the JSON bandwagon here. If you have a function that you are using to load your file and a function to save your file then I recommend you read this tutorial:

https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

Lua tables convert to JSON very naturally. XML takes work. In XML you have tags that wrap values, so your tag could be a key and the data inside the tags, the value. But XML also supports key-value attributes. This doesn’t map well to Lua tables. The xml.lua you are using works well on most XML files, but I’m sure along the way, you might find problems with.

But all this said, if you’re just saving tables and loading tables, you don’t need to know XML or JSON. You just need to let the functions work for you. But since XML produces Lua tables with child members (to handle those attributes), JSON is much more natural.

json.encode(yourLuaTable) produces a string. You don’t care how that string is formatted or what’s in it. You know that json.decode(encodedString) will return the original Lua table to you. In your case since you want to encrypt it, take the string output by json.encode, run it through the openSSL encrypt function and write that data out.  When ready read that data into a lua string, pass it through the openSSL decrypt function and you should be back to your original JSON string and you then decode it back to your Lua table.

This is the least path to entry. @roaminggamer’s library is good tool to use.

Rob

Roger that to everything Rob said w/ one addition.  The openSSL is much more secure than my roll-your-own solution.  I merely included this for you to see it isn’t too hard to make something like that on your own.  Call it ‘educational’.

Cheers,

Ed

Hi.  Sorry, but why XML instead of JSON?  Is this to make your development easier? i.e. Do you edit the XML content as part of your development cycle?  Do you have another tool or product consuming the XML?

Also, if you’re amenable to a small change consider: http://github.com/GlitchGames/GGData or other similar solutions using encrypted JSON encoded tables.

-Ed

Hey roaminggamer !

The reason I’m using XML instead of Json is that when I started dealing with save files, I only had a small experience with XML files and absolutely zero experience with JSON files…

That was a long time ago. Today, I do understand how Json would be a lot more effective and easy to use with LUA and Corona, but unfortunately I can’t afford rewriting everything.

Just in case : I’ve edited my original post and added the decrypted string that I intended to use.

  1. How are you encrypting your file?

  2. Honestly, I’d say leave encryption out of the equation.  If we’re just talking score, inventory, progress, and other similar stuff.  Let people do what they want.  They bought the game.  If they want to hack it they should be allowed.  

  3. Have you considered rolling your own encryption?  It’s pretty easy and even a basic solution like rot13 (or similar) is good enough for this use case.

-Ed

  1. I’m using this Corona Labs tutorial to encrypt/decrypt my files.

  2. I agree. In the end, I’m not even sure I’ll let the encryption in my game. I mainly do it to learn how to do it :slight_smile:

  3. Way out of my league (really…)  :slight_smile:

My gut tells me you’re making a mistake following the directions.

The premise of what you want to do should be:

Encryption

  1. Convert your data to XML.  This is stored as a (long) string in memory.

  2. Encrypt that string using the tutorial technique 

  3. Save the newly encrypted string to a file.

Decryption

  1. Load the encrypted content from file into a string in memory.

  2. Decrypt it.

  3. Now you have a XML-encoded table stored in a string.  Should match original.

My suggestion is that you test the encryption and decryption code you’ve written with a simple string.  If that doesn’t work you’re making a mistake  in the process.  i.e. Make a standalone app to test this process, then copy that code to your game.

I can’t think of a reason why the encryption tutorial would fail for a XML-encoded table stored as a string.

Oh, and what xml encoder are you using?  Hand-rolled or a library and if so, where did you get it.

I’m using “XML Parser by Jon Beebe”.

The encryption / decryption seems to work : I do have a long string that I’m even able to print out in the console. But the encoding doesn’t seem right : right after the “<?xml version=“1.0”?>” line (which is fine), the text looks messy with weird characters.

I’m gonna do more tests and try to figure out where’s my mistake !