How can I JSON encode a table, encrypt it, save it, and restore it?

I have data json file in that json file I want to store player settings as a table and later on I will need to store has player characters or no.

1 Like

Maybe this can help you: https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Is there any way to encrypt data, so it would be harder to cheat?

I don’t think you need to worry about people cheating until you have enough players to see that it actually is a problem. At least I’m not spending any time on encryption etcetera.

But if you still want to do it, I think you should just search the forums for an answer first…

2 Likes

Ok, thanks!

Every time you use json.encode(), it will turn whatever table you input into a JSON string, so adding extra tables is as simple as making that new table a subtable or adding multiple tables to the function inside brackers, i.e.

local t = { myValue=1 }
t.newTable = { newValue=2 }

json.encode( t )

-- or

local t1 = { myValue=1 }
local t2 = { newValue=2 }

json.encode( { t1, t2 } )


1 Like

thank you!

how can I get open ssl plugin now?
do I need to upgrade to solar2D?
because I am still on 2018 corona SDK

I don’t know about the ssl plugin, but you should definitely update to latest Solar2D build asap. I believe that the Corona build servers will be permanently shut down pretty soon from what I’ve read. Moving to Solar2D offline builds shouldn’t be much of an issue either. I don’t think I had to change anything to migrate if I remember correctly.

So to migrate I just need to install solar 2D and thats it?

Probably that’s all you need to do. Depends on what plugins you are using I guess. Just give it a try and see what happens. :slight_smile:

1 Like

The opensll is added to build.settings file. Then use it like that :

local openssl = require "plugin.openssl"
local cipher = openssl.get_cipher ( "aes-256-cbc" )
local iv = ...
local key = ....
local yourEncryptedData = mime.b64( cipher:encrypt ( rawData, key, iv ) )

build.settings :

plugins =
    {
		["plugin.openssl"] = { publisherId = "com.coronalabs", },
....

how do I encrypt tables?

You convert the table to a string using JSON first, then only you do the encryption.

i did it, but I get strange error.
Looks like that error it is from plugin itself

local testString = {
  version = version,
  playerLevel = playerLevel,
  music = music,
  sfx = sfx,
}
local pass = "SomePass"

testString = json.encode( testString)

local encryptedData = mime.b64( cipher:encrypt( testString, pass ) )

testString = json.encode( encryptedData )

local path = system.pathForFile( "data.json", system.ResourseDirectory )

local file = io.open( path, "w" )

if file then
  file:write( testString )
  io.close( file )
end

function LoadData(fileName)
  local path = system.pathForFile(fileName, system.ResourseDirectory)
  local contents = ""
  local myTable  = {}
  local file = io.open(path, "r")
  if(file) then
    contents = file:read("*a")
    contents = json.decode( myTable )
    myTable = cipher:decrypt( mime.unb64( myTable ), pass )
    myTable = json.decode( myTable )

    io.close(file)
    return myTable
  end
  return nil
end

2020-09-26 (2)

Any help?
I tried this, but same issue:

local testString = {
  version = version,
  playerLevel = playerLevel,
  music = music,
  sfx = sfx,
}
local pass = "somePass"

testString = json.encode( testString )

local encryptedData = mime.b64( cipher:encrypt( testString, pass ) )

local path = system.pathForFile( "data.json", system.ResourseDirectory )

local file = io.open( path, "w" )

if file then
  file:write( encryptedData )
  io.close( file )
end

function LoadData(fileName)
  local path = system.pathForFile(fileName, system.ResourseDirectory)
  local contents = ""
  local myTable  = {}
  local file = io.open(path, "r")
  if(file) then
    contents = file:read("*a")
    contents = json.decode( myTable )
    myTable = cipher:decrypt( mime.unb64( myTable ), pass )

    io.close(file)
    return myTable
  end
  return nil
end

@ConveyedRex7592 You aren’t providing enough information.

For instance, your crash is with pegmatch. What’s that? Which line is #693? etc.

Also, the code that you’ve posted should crash before you ever get to do anything. For instance, you are using JSON, mime, cipher, etc. without ever actually requiring them (or at least showing us how you require them and set things up).

Cryptography isn’t a particularly easy topic. For now, I would suggest that you go take a look at the examples that can be found from the documentation page that @yosu provided for you.

There isn’t any error in the encrpytion. it is the other parts of your code.

Try not to use system.ResourceDirectory (read here)

Sample code below works.

local testString = {
  version = 1,
}
local pass = "somePass"

testString = json.encode( testString )

local encryptedData = mime.b64( cipher:encrypt( testString, pass ) )
print( encryptedData )
local path = system.pathForFile( "data.json", system.TemporaryDirectory )	--system.ResourseDirectory )
print(path)
local file = io.open( path, "w" )
print( file )

if file then
  file:write( encryptedData )
  io.close( file )
end

Did you try to decrypt data ? Because I think I get error in that part.