Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Code to encypt, decrypt json data — Gideros Forum

Code to encypt, decrypt json data

edited December 2012 in Code snippets
We use this to hide some sensitive data (level, score ..). Hope it will be useful to you!
local function convert(chars,dist,inv)
    local charInt = string.byte(chars);
    for i=1,dist do
        if(inv)then charInt = charInt - 1; else charInt = charInt + 1; end
        if(charInt<32)then
            if(inv)then charInt = 126; else charInt = 126; end
        elseif(charInt>126)then
            if(inv)then charInt = 32; else charInt = 32; end
        end
    end
    return string.char(charInt);
end
--Change this key, value from 0 to 255
local zendiosKey = {0,123,123,255,101};
local function crypt(str,inv)
    local enc= "";
    for i=1,#str do
        if(#str-zendiosKey[5] >= i or not inv)then
            for inc=0,3 do
                if(i%4 == inc)then
                    enc = enc .. convert(string.sub(str,i,i),zendiosKey[inc+1],inv);
                    break;
                end
            end
        end
    end
    if(not inv)then
        for i=1,zendiosKey[5] do
            enc = enc .. string.char(math.random(32,126));
        end
    end
    return enc;
end
 
-- Sample usage:
local data_json_sample = "{framework:'Gideros'}";
local encrypt_string = crypt(data_json_sample,false);
print (encrypt_string)
local decrypt_string = crypt(encrypt_string,true);
print (decrypt_string)
Output:
Uploading finished.
8#Ta*"Yo/({'c&Fe/,U':s^>)O5;wT^*NDOkPDaRYE c)[rNfODjTAz[0C,fzZ"?%\,prXdq!,cZ4&0[<a href="https://forum.gideros.rocks/profile/%26gt" rel="nofollow">@&gt</a>;B^llRZku-[F8,q&HV<T5=*l,;jukfZBq5r57S}%'
 
{framework:'Gideros'}
Coming soon
+1 -1 (+4 / -0 )Share on Facebook

Comments

Sign In or Register to comment.