local function Obfuscate(code) local oCode = '' for i = 1,code:len() do oCode = oCode .. string.byte(string.sub(code,i,i)) .. ',' end oCode = string.sub(oCode,1,oCode:len() - 1) return oCode end local function Deobfuscate(code) local dCode = '' for i = 1,code:len(),2 do --if string.sub(code,i,i + 1) ~= ',' then dCode = dCode .. string.char(tonumber(string.sub(code,i,i + 1))) print(dCode) --else --end end return dCode end local Code = Obfuscate('Print') print(Code) local oCode = Deobfuscate(Code) print(oCode)
Errors:
14: bad argument #1 to 'char' (number expected, got nil)
Obfuscate
doesn't result in a string of alternating commas and digits like you seem to be assuming it does.
It doesn't form any nice pattern, and since Lua doesn't provide a split
method, you won't want to implement it the way you are.
One option is to use the :gmatch
string method as an iterator:
for numerals in code:gmatch("%d+") do dCode = dCode .. string.char(tonumber(numerals)) end
While it's not related directly to your question, I would like to point out that doing obfuscation in this form is futile. Since everything has to be piped through some function like Deobfuscate
to run it, whoever wants to discover the original just has to change loadstring
(or whatever other evaluation) to print
.
Successful code obfuscation requires that the code runs as it is, without having to do some post-processing on it.
E.g., consider this:
function g(a, b, u, v) return b > 0 and u(b - 1, a * b, u, v) or a; end function f(x, y, u, v) return x == 0 and y or v(y * x,x - 1, u, v) end function e(n) return f(n, (n + 1) / (n + 1), f, g); end
any idea what e
does? Any idea how it works?