Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Simple savegame encryption — Gideros Forum

Simple savegame encryption

HolonistHolonist Member
edited April 2015 in Code snippets
Hi, I wrote a litte encryption algorithm for savegames.

Background: Until now I always stored savegames in plain text. Which didn't really matter because I haven't published anything yet. But as always, I'm working on a game and in the case I get to the point of a release I want at least some sort of encryption that prevents change of the player's stats or unlocking of achievements.

I searched for some simple encryption algorithms in lua but I could only find two or so, which were pretty messy, and I don't like to use code I don't understand. So I started writing my own little functions and until now they seem to work nicely.


What type of encryption is used:
I started with the simplest form of encryption: rotation. Because that's very easy to hack I added the feature to change the rotation interval with each character.

With ROT1, the encryption would look like this:
hello -> ifmmp

With my encryption, there is an array of rotation-values.
for example {1,2,3}
hello -> igomq

For my understanding, this is still easy to hack for a real hacker, but it's already MUCH better than simple rotation, and people won't be able to simply go into document folder and change values. You can use intervals between 0 and 255 and the chain can be as long as you like.

Miscellaneous:
I tried to comment the code in much detail.
You can store json strings.


If you like you can use the code freely.
Please let me know what you think. Criticism and improvement suggestions are very welcome, as I want to have a decent encryption for my savegames.

Greetings
zip
zip
EncryptionTest.zip
2K

Likes: pie, hgy29, GiderosFan

+1 -1 (+3 / -0 )Share on Facebook

Comments

  • HolonistHolonist Member
    edited June 2015
    In case you want to use this with dataSaver, even though it's a bit silly, because of double JSON encoding, here is the syntax:

    local mydata = {}
    mydata.somestring = "mystring"
    mydata.somevalue = 42

    //save
    dataSaver.save("|D|mydata", encrypt(json.encode(mydata)))

    print(mydata.somestring)
    print(mydata.somevalue)

    //load
    local mydata = json.decode(decrypt(dataSaver.load("|D|mydata")))

    print(mydata.somestring)
    print(mydata.somevalue)
    +1 -1 (+3 / -0 )Share on Facebook
  • In my latest experiment I am using a modified version of your code to encrypt data for transmission over networks. My version encrypts the data as yours does but returns the output as a hexstring. Check the code, its not too hard to see what is happening :)
     
    -- Simple string encrypt_encode/decode_decrypt for network transmission
     
      local keys = {
      0x39,0x1c,0x0c,0xb3,0x4e,0xd8,0xaa,0x4a,0x5b,0x9c,0xca,0x4f,0x68,0x2e,0x6f,0xf3,
    }
     
    local function encrypt(input)
      local k = 1 -- KEY TO START WITH
      local output = ""
      for i=1, input:len() do
        local strb, strf = string.byte, string.format
        local char = strb(input, i) + keys[k] -- GET NEXT CHAR AND ADD NEXT KEY
        if char > 255 then char = char - 255 end -- KEEP WITHIN RANGE 0-255)
        if k < #keys then k = k + 1  else k = 1 end -- NEXT KEY
        output = output .. strf('%02X', char) -- CONVERT TO HEXSTRING AND APPEND TO OUTPUT
      end
      return output
    end
     
    local function decrypt(input)
      local k = 1
      local output = ""
      for i=1, input:len(), 2 do
        local strs, strc = string.sub, string.char
        local char = tonumber( strs(input, i, i + 1), 16) - keys[k] -- CONVERT HEXSTRING TO CHAR AND SUB NEXT KEY
        if char < 0 then char = char + 255  end
        if k < #keys then k = k + 1  else k = 1 end
        output = output .. strc(char) -- CONVERT TO ASCII CHAR AND APPEND TO OUTPUT
      end
      return output
    end
     
    local data = 'The quick brown fox jumps over the lazy dog.'
     
    data = encrypt(data)
    print(data)
    print("")
    data = decrypt(data)
    print(data)
    Thanks for your initial code, and I hope somebody finds my version handy somewhere ~:>
    +1 -1 (+3 / -0 )Share on Facebook
Sign In or Register to comment.