Decrypting saved table file problem

Hi,  I have a weird problem.

I am encrypting a table and saving it in my documents directory:

local openssl = require “plugin.openssl”
local cipher = openssl.get_cipher ( “aes-256-cbc” )
local mime = require ( “mime” )
local deviceID = system.getInfo( “deviceID” ) or ( _G.iOSappID )

   local contents = json.encode( t )
   local encryptedData = mime.b64 ( cipher:encrypt ( contents, deviceID ) )
   if _G.encryptedSaveData then

        file:write( encryptedData )  

   else

   file:write( contents )

I read it back in with:

   local contents = file:read( “*a” )
        local decryptedData = cipher:decrypt ( mime.unb64 ( contents ), deviceID )
        local t = {}
        if _G.encryptedSaveData then
            t = json.decode( decryptedData );
        else
            t = json.decode( contents );

My problem is when encrypting is on, I get an error "attempt to index global ‘tablename’ (a nil value), but only in a function that I call that uses thetable.  If I use the table directly in the code right after loading it, it works fine.

When I turn encryption off and run the same code, there is no error.   When I print out the table created with encryption and without encryption they are identical. 

So somehow the scope of my table is getting blown away when I turn encryption on! 

Any help is greatly appreciated as I don’t want to release my game with clear text settings!, Greg

Here are my load and save functions:

_G.encryptedSaveData =false – set to false when debugging
_G.iOSappID = “1234567890” – your App Store app ID

local openssl = require “plugin.openssl”
local cipher = openssl.get_cipher ( “aes-256-cbc” )
local mime = require ( “mime” )
local deviceID = system.getInfo( “deviceID” ) or ( _G.iOSappID )

– save json table to data file
function saveTable2( t, filename  )
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local results = os.remove( path )
    
    local file = io.open( path, “w” )
     if( file ~= nil ) then
        local contents = json.encode( t )
        local encryptedData = mime.b64 ( cipher:encrypt ( contents, deviceID ) )
        if _G.encryptedSaveData then
            file:write( encryptedData )      
        else
            file:write( contents )
        end
        io.close( file )
        return true
    else
        – print( "Error: could not read " … filename )
        return false
    end
end

– load json table from data file
function loadTable2( filename )
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local contents = “”
    file = io.open( path, “r” )
    if ( file ) then
        – read all contents of file into a string
        local contents = file:read( “*a” )
        local decryptedData = cipher:decrypt ( mime.unb64 ( contents ), deviceID )
        local t = {}
        if _G.encryptedSaveData then
            t = json.decode( decryptedData );
        else
            t = json.decode( contents );
        end
            io.close( file )
        return t
    else
        – print( "Error: could not read from " … filename )
    end
    return nil
end

Here are my load and save functions:

_G.encryptedSaveData =false – set to false when debugging
_G.iOSappID = “1234567890” – your App Store app ID

local openssl = require “plugin.openssl”
local cipher = openssl.get_cipher ( “aes-256-cbc” )
local mime = require ( “mime” )
local deviceID = system.getInfo( “deviceID” ) or ( _G.iOSappID )

– save json table to data file
function saveTable2( t, filename  )
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local results = os.remove( path )
    
    local file = io.open( path, “w” )
     if( file ~= nil ) then
        local contents = json.encode( t )
        local encryptedData = mime.b64 ( cipher:encrypt ( contents, deviceID ) )
        if _G.encryptedSaveData then
            file:write( encryptedData )      
        else
            file:write( contents )
        end
        io.close( file )
        return true
    else
        – print( "Error: could not read " … filename )
        return false
    end
end

– load json table from data file
function loadTable2( filename )
    local path = system.pathForFile( filename, system.DocumentsDirectory )
    local contents = “”
    file = io.open( path, “r” )
    if ( file ) then
        – read all contents of file into a string
        local contents = file:read( “*a” )
        local decryptedData = cipher:decrypt ( mime.unb64 ( contents ), deviceID )
        local t = {}
        if _G.encryptedSaveData then
            t = json.decode( decryptedData );
        else
            t = json.decode( contents );
        end
            io.close( file )
        return t
    else
        – print( "Error: could not read from " … filename )
    end
    return nil
end