Saving an encrypted thing and loading it does not work!

I am making a game. I am trying to save the data encrypted and then decrypt it. My code works fine without the encryption and decryption. The problem is that it does not save/load it when I am using encryption in it.

I have looked trough the code many times and tested it many times but it just does not work with encryption.

Here is the code (I am using loadsave library):

local M = {} local openssl = require( "plugin.openssl" ) local cipher = openssl.get\_cipher ( "aes-256-cbc" ) local myPassword = "example" local json = require("json") local \_defaultLocation = system.DocumentsDirectory local \_realDefaultLocation = \_defaultLocation local \_validLocations = { [system.DocumentsDirectory] = true, [system.CachesDirectory] = true, [system.TemporaryDirectory] = true } function M.saveTable(t, filename, location) if location and (not \_validLocations[location]) then error("Attempted to save a table to an invalid location", 2) elseif not location then location = \_defaultLocation end local path = system.pathForFile( filename, location) local file = io.open(path, "w") if file then local contents = json.encode(t) --encrypt that json string local encryptedData = cipher:encrypt( contents, myPassword ) file:write( encryptedData ) io.close( file ) return true else return false end end function M.loadTable(filename, location) if location and (not \_validLocations[location]) then error("Attempted to load a table from an invalid location", 2) elseif not location then location = \_defaultLocation end local path = system.pathForFile( filename, location) local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) --decrypt the string that we have retrieved local decrypted = cipher:decrypt ( contents, myPassword ) local myTable = json.decode( decrypted ) print( decrypted ) io.close( file ) return myTable end return nil end function M.changeDefault(location) if location and (not location) then error("Attempted to change the default location to an invalid location", 2) elseif not location then location = \_realDefaultLocation end \_defaultLocation = location return true end function M.print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end M.printTable = M.print\_r return M

Best regards

Coder101

Hi.  what Documentation are you referring to in order to do this.

I’m sure I can help, but I want to see all the docs you’re using first so I can be on the same page as you.

Please provide links.

I am referring to this tutorial https://forums.coronalabs.com/topic/66630-how-can-i-encrypt-my-saved-data/

Also, have you tested encryption and decryption without all the saving and restoring from file and tables parts?

i.e. Have you successfully:

  • encrypted a string
  • decrypted a string
  • printed the string to verify the decryption?

I’m asking because you do that, you can’t be sure the rest of your code will work.

Also, not doing this step wise makes the whole thing way more complicated to debug.

Yes I have. I did something like

local variable = “test”
print( variable )

local encryptedVariable = cipher:encrypt( variable, myPassword )
print(encryptedVariable) – came out nonsense

local decryptedVariable = cipher:decrypt( encryptedVariable, myPassword )
print( decryptedVariable ) – came out as “test”

I see you’re referring to an old thread.  I noticed the link in that thread is now gone.

OK.  Let me see what I can resolve.

Warning.  My answer will use SSK.  I simply don’t do long hand stuff like this. :frowning:

It sure would be nice if you attached a zip file with your test project as that would save folks the effort of remembering the build.settings settings, etc.

Don’t forget.  Always format code posts, no matter how short.

formatyourcode.jpg

Continuing to resolve issue.

I can attach the zip tomorrow.

And btw I know how to format the code snippets on PC but I am on mobile (can you do tuhat on mobile?).

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/encryptedPersistentData.zip

function tests.three( myPassword ) local myData = { name = "billy", age = 10, loves = "Corona and Games" } print( "DUMP OF ORIGINAL TABLE" ) for k,v in pairs( myData ) do print(k,v) end local encoded = json.encode( myData ) print( "JSON ENCODED: ", encoded ) local out = cipher:encrypt( encoded, myPassword ) print( "ENCRYPTED:", out ) -- Save and restore io.writeFile( out, "mydata.json" ) local restored = io.readFile( "mydata.json") print( "RESTORED: ", restored ) local out2 = cipher:decrypt( restored, myPassword ) print( "DECRYPTED:", out2 ) local decoded = json.decode( out2 ) print( "JSON DECODED: ", decoded ) print( "DUMP OF SAVED & RESTORED & DECODED TABLE" ) for k,v in pairs( decoded ) do print(k,v) end end

I never use mobile for posting to forums.  

Posts from mobile tend to be terribly formatted and I consider it rude not to make my posts super legible, but then I’m old and a stick in the mud.

I also find mobile useless for reading anything.  Again, old an stick in the mud.

Well, for now the only noticable difference between your code and mine is that you use io.writeFile and io.readFile and I use file:write and file:read… What are main differences in them?

Apples and oranges.  My io.writeFile() function uses file:write(), but it also does the opening, closing, and other stuff.

… or maybe that’s Oranges and Orange Seeds?

You can read the code to find out more:
https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/extensions/io.lua

I don’t know what to tell you other than my example works? 

I wanted to give you something that would solve the end problem, but I have to admit I was averse to using the code provided. 

I’m more of a problem solver and code writer, than a code debugger. 

Sorry!

OK. I have tested your code and implemented it in mine. Now it works flawlessly. Thank you so much för helping me.

Can I use your ssk2 library in my game that is in play store and has advertisement and might get iap purchases?

You may use SSK2 in any game or app, no credit statement, no cost, no strings, just use it.  May it speed up and simplify your dev process.  

Cheers,

Ed

Thanks a lot for everything.

Btw I must link the game for you when it is ready.

Actually, you should totally share a link here with everyone.  Good luck on the game.

If you need extra motivation while working, you can also post pics in the screenshot saturday thread:

https://forums.coronalabs.com/topic/45387-screenshotsaturday-post-your-screen-shots-here

It gets good feedback.

Yes, I will share it for everyone.

Hi.  what Documentation are you referring to in order to do this.

I’m sure I can help, but I want to see all the docs you’re using first so I can be on the same page as you.

Please provide links.

I am referring to this tutorial https://forums.coronalabs.com/topic/66630-how-can-i-encrypt-my-saved-data/