## [SOLVED] How to create a simple encryption for a lua table?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
cazura
Prole
Posts: 12
Joined: Fri Jan 03, 2020 4:17 pm
Location: Brazil

### [SOLVED] How to create a simple encryption for a lua table?

Hello!

I'm not sure if "encryption" is the thing I'm looking for, but I think it may be.

Let me explain: I played a browser game that had a import/export save file. When you export, it gives you a big weird code to copy. And, of course, the load is just to paste the code.

I liked that, it's simple and would be nice have something like that for my little game. Since it is browser, some people would feel more safe having a external save.

Now... My save file is one table of variables, that also has tables inside. It is possible, maybe by a simple encryption system or something, turn the table into a big weird code that the person can copy to save and load?

It would also not be a problem if someone could break this encryption and hack the game.
Last edited by cazura on Mon Jan 13, 2020 1:57 pm, edited 1 time in total.

raidho36
Party member
Posts: 2031
Joined: Mon Jun 17, 2013 12:00 pm

### Re: How to create a simple encryption for a lua table?

Unless your game is multiplayer you shouldn't worry about people hacking save files - it wouldn't hurt anyone. That, and they'll do it anyway - security through obscurity is not a sustainable approach. Just about the only reliable way to avoid data hacking is never giving it to the players - store everything on your server (and validate every player action).

You can look up wikipedia articles about cyphering and cryptography. Something as simple as character substitution will already make it unreadable, but know in advance that any simple cipher can be cracked using mathematical methods. Actual cryptography will allow you to store data that's very difficult to edit, but that's a complicated topic and doing it right is very hard and otherwise it's immediately crackable, and frankly it's simlpy not worth the effort.

SyntheticDreamCorp
Prole
Posts: 3
Joined: Sun Nov 26, 2017 3:55 am

### Re: How to create a simple encryption for a lua table?

I think serialization is probably more the word you're looking for, than encryption.

What you want is definitely possible, but if your save data contains nested tables, you're going to have to write a more involved program that can serialize and deserialize recursively. It would probably be easier to just modify how save data is stored/loaded with the mindset that it's going to be serialized/deserialized.

That being said, you're gonna want to write a function that goes through the save-table and translates each element into text (or whatever format you want). this text will have to convey 1. the type of data (e.g. number, string, table, etc) and 2. the data itself. Creating a deserialization function should be relatively simple, as it will effectivelythe process of the first function, but in reverse (read the data type, convert the data, assign it to the save-data table).

cazura
Prole
Posts: 12
Joined: Fri Jan 03, 2020 4:17 pm
Location: Brazil

### Re: How to create a simple encryption for a lua table?

@raidho36
As for now, I don't mind at all they hacking the save file. This will be important though, if I was to let them share highscores and stuff for online rankings, then I would want to have some control and protection (with a server, I imagine). But that is not the case.

I was searching and... Oh boy, it is a very rich and difficult subject. Hopefully, the pure lua implemations I found will work out.

@SyntheticDreamCorp
OH! Serialization, of course. Table becomes a string. I used TSerial for my saves tests before and it can serializate nested tables. I see... I make the table a string and then apply some ciphering or encryption.

I gonna try something with https://github.com/somesocks/lua-lockbox, but with my limited skills, I imagine if I can make it work (Also, I hope it doesn't causes lua versions incompatibilities). I just need to serialize my table, so it becomes a string that can be used in an AES128 system for example. In the end, I just want to have the encrypted code so the player can personally save it, and keep a minimum barrier where he doesn't see a "power = 10" waiting to become a 999999999 that easily.

I will keep working on it, thank you all!

grump
Party member
Posts: 602
Joined: Sat Jul 22, 2017 7:43 pm

### Re: How to create a simple encryption for a lua table?

I wouldn't mess with "real" encryption for this. You don't even care about hacks so just go with obfuscation and keep things as small as possible.

- Serialize the data into a string. Use a binary serializer rather than one that outputs text for smaller and more obfuscated results
- Compress the serialized string using love.data.compress
- Optional: add a checksum for quick validation, so you can reject invalid pastes as early as possible
- Encode the resulting binary data as printable characters. A safe and simple option is Base64 (love.data.encode), but it increases output size by 25%

You now have a sufficiently obfuscated (but not securely encrypted), reversible, space-optimized (kinda) text representation of your data that is not easily manipulated. The decoding process is the same steps in reverse order, using decode and decompress, resp.

Example: (you have to provide a (de-)serializer of your choosing)

Code: Select all

local data = { score = 9001, lives = 42 } -- the data you want to encode

local serialized = serialize(data)
local compressed = love.data.compress('string', 'zlib', serialized, 9)
local encoded = love.data.encode('string', 'base64', compressed)

This results in an obfuscated string like this:

Code: Select all

eJyrVihOzi9KVbBVsDQwMNRRyMksSy0G8kyMFGoBeN8H7g==
Which can be decoded back to the original data:

Code: Select all

local decoded = love.data.decode('string', 'base64', encoded)
local decompressed = love.data.decompress('string', 'zlib', decoded)
local data = deserialize(decompressed)


cazura
Prole
Posts: 12
Joined: Fri Jan 03, 2020 4:17 pm
Location: Brazil

### Re: How to create a simple encryption for a lua table?

@grump

Wow, thank you a lot! That worked perfectly!
I used your moonblob for binary serialization and then applied the encode/decode. Unfortunely, love.js does not port it (Something about FFI), so I will need to find other binary system or stick with Tserial, but that is a minor issue.

grump
Party member
Posts: 602
Joined: Sat Jul 22, 2017 7:43 pm

### Re: How to create a simple encryption for a lua table?

cazura wrote:
Mon Jan 13, 2020 1:56 pm
I used your moonblob for binary serialization and then applied the encode/decode. Unfortunely, love.js does not port it (Something about FFI), so I will need to find other binary system or stick with Tserial, but that is a minor issue.
moonblob is not the best choice for this anyways, because it favors speed over size, and you definitely want small outputs. Try binser, it might work without ffi. Not sure though.

cazura
Prole
Posts: 12
Joined: Fri Jan 03, 2020 4:17 pm
Location: Brazil

### Re: [SOLVED] How to create a simple encryption for a lua table?

Binser worked!

Also, love.data is not supported in love.js (It's version 11). So, I went to find some external libraries. I ended up with binser for serialization, http://lua-users.org/wiki/BaseSixtyFour to encode and https://github.com/SafeteeWoW/LibDeflate for compression/decompression. Again, thank you for helping!

### Who is online

Users browsing this forum: No registered users and 146 guests