Something like the below should work.
Of course the risk is that someone clever opens the DB when they have loads of coins, and discovers that “fidjf3289389sl!r434k_w” in the DB field “coinsDefinitelyNotHereGoAway” is what represents that amount of coins, and copies that value back in after they spent them.
A way round that is to change the encryption key each time the database is saved, and try and hide a value somewhere else in the database (or maybe a little text file) that allows you to lookup the correct key.
But basically if someone r** eally** wants free coins, they will find a way.
[lua]
– encryption.lua
local openssl = require( “plugin.openssl” )
local cipher = openssl.get_cipher ( “aes-256-cbc” )
local mime = require ( “mime” )
local m = {}
m.encryptionKey = “MyEncryptionKey”
m.encryptString = function (str)
str = mime.b64 ( cipher:encrypt ( tostring(str), m.encryptionKey ))
return str
end
m.unencryptString = function (str)
str = mime.unb64 (cipher:decrypt ( str,m.encryptionKey))
return str
end
return m
[/lua]
[lua]
– from anywhere else
local encrypt = require(“encryption.lua”);
local coins = 50 ;
local coinsEncrypt = encrypt.encryptString(coins); – send this to database as string.
— to load back in
local coins = tonumber(encrypt.uncryptString(coinsEncrypt)); – load value from DB into coinsEncrypt as string.
[/lua]