[Resolved] Cannot decrypt data correctly with OpenSSL plugin

I am having a problem decrypting some files via the OpenSSL plugin.

I have some JSON files that I am encrypting on my Mac using this command line in Terminal:

openssl enc -aes-256-cbc -salt -in default.json -out default.enc

In my app, I have this:

local json = require( "json" ) local openssl = require( "plugin.openssl" ) local cipher = openssl.get\_cipher ( "aes-256-cbc" ) local jsonFile = function( filename, base ) if not base then base = system.ResourceDirectory end local jsonFilePath = system.pathForFile( filename, base ) local jsonFileContents local file = io.open( jsonFilePath, "r" ) if file then jsonFileContents = file:read( "\*a" ) io.close( file ) end return jsonFileContents end dta = {} dta = json.decode(cipher:decrypt(jsonFile("Assets/default.enc", system.ResourceDirectory), "Password"))

The password is the one that I am entering on the Mac to encrypt the files.

But when I try to access the data, I get this error:
Attempt to index upvalue ‘dta’ (a nil value)

This is my first time working with encrypted data – which I am only doing so as to not make it easy for someone to steal my game’s content – so not sure if I am doing something wrong in encrypting the data or my use of the plugin.

I did break the dta = json.decode(cipher:decrypt(jsonFile(“Assets/default.enc”, system.ResourceDirectory), “Password”)) line up so that I could see the data at each step of the decoding.  And the cipher:decrypt portion is not decrypting the data correctly into the JSON structure.

Anyone have any ideas?

Make sure the JSON is valid before the decrypt.

I verify my JSON with 

https://itunes.apple.com/au/app/json-toolbox/id525015412?mt=12

Edit: I had a go and I can get ti to work either.

Yeah, the JSON data is valid.  Though it really should not matter for the decryption portion.  It should still return the invalid data.  But it is not.  It doesn’t appear to be returning any decrypted data at all.

Thanks for trying.  I think you were saying that you couldn’t get it to work either? If I decrypt using the Mac command line, it does decrypt successfully.  So it seems like the issue is on the plugin end.

I went ahead and tried to use the b64 encoding as well, along with the mime.unb64 call, but that did not change anything.  Still cannot get my JSON structure decrypted correctly.

local obscuredString = mime.b64( json.encode( mytable ) )

Should produce a string that you can write to a file, then when you read it back in, you would do:

mytable = json.decode( mime.unb64( obscuredString ) )

This is of course assuming you read the file back into the variable obscuredString.   You could also base64 encode the strings before you add them to the table to be saved, but then you’re going to have to convert numbers to strings and that would be a real pain.

Rob

The problem seems to be, Rob, that you cannot encrypt the data on OS X using the built-in openssl, then decrypt it successfully using the OpenSSL plugin in Corona.  Even if you specify the correct cipher type and password.

I was able to solve this by writing a Corona app that encrypts the JSON files through the OpenSSL plugin, writing them to new files.  Then I moved those new files to my main app and use the OpenSSL plugin to decrypt them.  They successfully decrypt in this case.  Not ideal, but it works.   :slight_smile:

Make sure the JSON is valid before the decrypt.

I verify my JSON with 

https://itunes.apple.com/au/app/json-toolbox/id525015412?mt=12

Edit: I had a go and I can get ti to work either.

Yeah, the JSON data is valid.  Though it really should not matter for the decryption portion.  It should still return the invalid data.  But it is not.  It doesn’t appear to be returning any decrypted data at all.

Thanks for trying.  I think you were saying that you couldn’t get it to work either? If I decrypt using the Mac command line, it does decrypt successfully.  So it seems like the issue is on the plugin end.

I went ahead and tried to use the b64 encoding as well, along with the mime.unb64 call, but that did not change anything.  Still cannot get my JSON structure decrypted correctly.

local obscuredString = mime.b64( json.encode( mytable ) )

Should produce a string that you can write to a file, then when you read it back in, you would do:

mytable = json.decode( mime.unb64( obscuredString ) )

This is of course assuming you read the file back into the variable obscuredString.   You could also base64 encode the strings before you add them to the table to be saved, but then you’re going to have to convert numbers to strings and that would be a real pain.

Rob

The problem seems to be, Rob, that you cannot encrypt the data on OS X using the built-in openssl, then decrypt it successfully using the OpenSSL plugin in Corona.  Even if you specify the correct cipher type and password.

I was able to solve this by writing a Corona app that encrypts the JSON files through the OpenSSL plugin, writing them to new files.  Then I moved those new files to my main app and use the OpenSSL plugin to decrypt them.  They successfully decrypt in this case.  Not ideal, but it works.   :slight_smile:

I have that problem too. why I can’t decrypt the file that I encrypt on OS X that using openssl? Is cause the version of the openssl or what?

I have that problem too. why I can’t decrypt the file that I encrypt on OS X that using openssl? Is cause the version of the openssl or what?