-2

I have a path for a file specified as a string in my code, and I don't want to be visible after luac conversion. Is it possible obfuscate somehow the line? My code is:

DIR1 = '../../../files/file1.txt'

Thank you!

1 Answers1

1

Yes.

Example:

local Key53 = 8186484168865098
local Key14 = 4887

function decode(str)
   local K, F = Key53, 16384 + Key14
   return (str:gsub('%x%x',
      function(c)
         local L = K % 274877906944   -- 2^38
         local H = (K - L) / 274877906944
         local M = H % 128
         c = tonumber(c, 16)
         local m = (c + (H - M) / 128) * (2*M + 1) % 256
         K = L * F + H + c + m
         return string.char(m)
      end
   ))
end

local path = decode"beb81858c47a5fc7e11721921fb7f58ceeb530c4e74034df"
print(path)  -->  ../../../files/file1.txt

How to encode your own text

Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
  • Thank you, but unfortunately the code doesn't work for me because I am limited to a 5.0.2 lua version.. I get a modulo error when I try to compile. (unexpected symbol near `%') Is there any other way to encode a string on my lua version? – blackleader Mar 12 '18 at 22:18
  • Nice design in that the number type should support 53 bits of precision. @negru13 Try [math.mod](http://www.lua.org/manual/5.0/manual.html#5.5). – Tom Blodget Mar 13 '18 at 02:50
  • @negru13 - I've never used Lua 5.0, but as Tom Blodget said, you must write `math.mod(a,b)` instead of `a % b` in Lua 5.0. For example, `local m = math.mod((c + (H - M) / 128) * (2*M + 1), 256)` – Egor Skriptunoff Mar 13 '18 at 07:03
  • @negru13 - In encoder you should replace `%` too: `until math.mod(inv * (2*M + 1), 256) == 1` – Egor Skriptunoff Mar 13 '18 at 07:08
  • I have made the modifications regarding math.mod, but now when I local s = 'hello world' encode(s) I get: attempt to index a string value – blackleader Mar 13 '18 at 11:46
  • @negru13 - Probably, in Lua 5.0 strings don't have their metatable? Try to replace `str:gsub(...)` with `string.gsub(str,...)`, `m:byte()` with `string.byte(m)` and `('%02x'):format(c)` with `string.format('%02x', c)` – Egor Skriptunoff Mar 13 '18 at 12:13
  • Yes you were right @Egor Skriptunoff , now it manages to encode the string, but the decoder now prints only half of the string right: Hello$Mûô†@ We are close here.. – blackleader Mar 13 '18 at 13:26
  • @negru13 - I've posted a link to Lua 5.0 version under [that answer](https://stackoverflow.com/a/35735615/6834680). It does work for me in Lua 5.0. Please check it. – Egor Skriptunoff Mar 13 '18 at 14:27
  • Thank you very much for the effort, that is the same code I use, but I don’t know the reason my debug output is different..I’ve noticed that with only one word works fine, but when it comes to a path or more than one word the output is scrambled.. – blackleader Mar 13 '18 at 21:11
  • @negru13 - What is the output of `print(encode('Hello world'))` on your system? There is one space between "Hello" and "world". – Egor Skriptunoff Mar 14 '18 at 07:16
  • The encoded output for Hello world is: 80891c4c87fffffffb951b5143d7 and the decoded is : Hello$Mûô†@ Maybe is a console output problem? If I test the code online on codepad the result is correct.. – blackleader Mar 14 '18 at 07:46
  • But the coded output is also different on codepad.org – blackleader Mar 14 '18 at 07:57
  • @negru13 - What is the output of the following Lua program on your system: `string.gsub('Hello world', '.', function(m) m = string.byte(m) print(m) end)` ? – Egor Skriptunoff Mar 14 '18 at 08:04
  • The output is: 72 101 108 108 111 32 119 111 114 108 100 – blackleader Mar 14 '18 at 08:18
  • @negru13 - What is the output of `x=1 n=1 while (x*2+1)>x and (x*2+1)-(x*2)==1 do x,n=x*2+1,n+1 end print(n)` ? – Egor Skriptunoff Mar 14 '18 at 09:19
  • @Egor Skriptunoff The output is: 24 – blackleader Mar 14 '18 at 09:38
  • Also if I encode: local path = "../../../files/abcdefghiklmnopqrstvxyz.txt" the decoded is: ../../../files/WA?ghiklmnopqrstvxyz.txt And if I encode: local path = "../../../files/a b c d e f g h i k l m n o p q r s t v x y z.txt" the decoded is OK: ../../../files/a b c d e f g h i k l m n o p q r s t v x y z.txt It seems that has a problem with "abcdef" – blackleader Mar 14 '18 at 10:21
  • @negru13 - Your implementation of Lua uses `float` Lua numbers instead of `double` (24-bit precision instead of 53-bit). I've posted version for Lua 5.0 with "float" numbers under [that answer](https://stackoverflow.com/a/35735615/6834680). – Egor Skriptunoff Mar 14 '18 at 11:04
  • @Egor Skriptunoff Yes, you are right! It's working now as it should. Thank you very very much and keep up the good work! – blackleader Mar 14 '18 at 11:53
  • @Egor Skriptunoff Sad to return to the same problem.. but it still scrambles some of my paths.. :( for example: encoded "5ed3f762a0de7ab24e56a165777cd06a937df72641d62cffffffbc098575626a74b2be71bf29003ed9ced41252d2f68595ee6808" returns decoded: ../../../userdata/80273z%xB>LðNH»a(GÆÞÝY=•¿2zÇ" do you know this time what could it be? – blackleader Mar 15 '18 at 01:28
  • @negru13 - Interesting. Could you post output of [this program](https://pastebin.com/L1DdCdGq) with your string that fails? The output will be large (it contains debug info for each step of algorithm). – Egor Skriptunoff Mar 15 '18 at 07:24
  • First: 5ed3f762a0de7ab24e56a165777cd06a937df72641d62cffffffbc098575626a74b2be71bf29003ed9ced41252d2f68595ee6808 Second: 299203f9d56c5f3b55bedc9a7240f5063ce3063b17a75fac88fb039a11136e5aab4e5ae182b61149850a8d6ff9276afc546fb160b1dc7fd03ccb0262c73f30ca2f86bf50a46e38c44d8f797eb5ef2f2cee1ccaba645cf7271515d738b76e829b52190ab907108857 Third: 5ed3f762a0de7ab24e56a165777cd06a937df72641d62cffffffbc098575626a74b2be71bf29003ed9ced41252d2f68595ee6808 – blackleader Mar 15 '18 at 08:25
  • @negru13 - This data is not enough. Please give whole output. – Egor Skriptunoff Mar 15 '18 at 10:10
  • @EgorSkriptunoff Sorry about that: http://textuploader.com/dgan2 Updated. – blackleader Mar 15 '18 at 10:45
  • @negru13 - Thanks for bugreport. It appears that `math.mod` is not equivalent to `%` operator: `math.mod(-68,256) == -68` but `-68%256 == 188`. I've updated links under [that answer](https://stackoverflow.com/a/35735615/6834680). Please check new version. – Egor Skriptunoff Mar 15 '18 at 12:10
  • @EgorSkriptunoff Thank you again, I hope now will be no more problems with the paths, I will also do some tests with something dynamic to see if it decodes them all correctly. I have noticed also an incorrect output if the function is called in the shape of: decode(path) instead of decode(encode(path)). Is this the normal way? because I was thinking at first that I do not have to include in the same file both the encoder and decoder. – blackleader Mar 15 '18 at 12:54
  • @negru13 - decoder and encoder may exist separately, but they must have the same constants `Key24` and `Key14` (`Key24` must be less than 2^24, `Key14` less than 2^14) You can `decode(encoded_path)` without encoder as shown in this answer. – Egor Skriptunoff Mar 15 '18 at 13:49