-1

I have come across several web-based games (i.e. http://adarkroom.doublespeakgames.com/ or http://minmaxia.com/c2/) or i.e. Clicker Heroes on Kongregate that enable you to export a save game as something that looks like a random string of text. This can then be imported again to load the game.

I would like to program something in javascript that converts what I would normally store in localStorage:

var saveObject = {
level: 5,
xp: 3742
};

to

eyJ2ZXJzaW9uIjoxLjMsImZlYXR1cmVzIjp7ImxvY2F0aW9uIjp7InJvb etc.

I have the feeling there is some pre-existing method or function for that, which is commonly used, but I have not yet been able to find out what it is these websites use or what it is called. If anyone has any knowledge about that (or is clever enough to dig it up from the games' code), that would be most welcome.

Thorondale
  • 63
  • 2
  • Unless you plan on putting the coding / encoding function in a backend, anybody will be able to see how you encode/decode. So, why don't you simply output a json string and that's it. – yBrodsky Mar 22 '17 at 19:03
  • I considered that, but I would like it to be less obvious how the data is formatted. If you aren't into programming, or like me are just starting, figuring out the encode/decode part is not very intuitive at all. For me that is sufficient protection. JSON is an option though. – Thorondale Mar 22 '17 at 20:48

1 Answers1

1

What I thought about, when I read this question was Base64 and indeed, one of the websites linked by you that I checked, uses it.

If you would like to minimize length of output of this function, you could gzip it's input first, as shown in this question.

I also noticed that you tagged this question “encryption”. This has nothing to do with it, those strings can be converted back into plain text by anyone without any problem. If you would like to encrypt user's state and settings with a password of his choice, do it with AES. There is implementation of it in JS called JSAES.

Community
  • 1
  • 1
Przemek
  • 3,855
  • 2
  • 25
  • 33
  • 2
    Thanks a lot, that is exactly what I was looking for. Sometimes, finding the right term for something you don't know can be quite hard. Now I can start implementing this. :-) – Thorondale Mar 22 '17 at 19:26