Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Encrypting and decrypting Lua files

I have a program (a small game) written in C++ that gets its configuration from Lua files (they are basically modules for the program). For instance, the program gets its name, version and what it is and isnt allowed to do and what the player can do from the Lua files. The problem is when I start distributing this small game to a few people they can configure the Lua files which I dont want to happen so I have thought of encrypting them and then decrypting them when the program starts but I just cannot grasp the concept of how to actually do it and in what way. All in all, is this a rather simple task as I imagine it to be?

How I see it is like this: Encrypt the lua files with some program in a certain encryption method. The write C++ code into the program that first decrypts the Lua files and then starts reading them. Is this concept correct? The encryption itself could be as weak as possible, as long as it works.

like image 784
hennessy Avatar asked Nov 07 '13 15:11

hennessy


3 Answers

By using luac I was finally able to make it all work.

I used this phrase to compile it with luac (in there, tester.lua is name of output file and test.lua is the file that is compiled):

luac -o tester.lua test.lua

It all works automatically, whether its compiled or not. Now the problem is, anyone could place the compiled lua file with a noncompiled version and it would still work because dofile reads both, normal and compiled lua. What would you fellas suggest as the solution so that dofile would only read compiled lua files and not uncompiled ones?

like image 179
hennessy Avatar answered Nov 20 '22 18:11

hennessy


Yes, that's basically it.

Assuming at the moment you have something like

runLuaFromFile("config.lua");

you'd want to do

  • load text file "config.lua" into memory (e.g. as a string)
  • decrypt your string
  • runLuaFromMemory(myString);

obviously the runLuaFromFile and runLuaFromMemory aren't real functions, they're just placeholders for whatever Lua system you're using

like image 37
benjymous Avatar answered Nov 20 '22 17:11

benjymous


Distributing precompiled Lua files instead of source will probably suffice for your purposes. See luac.

like image 3
lhf Avatar answered Nov 20 '22 19:11

lhf