I am porting a substitution cipher function which unscrambles (decodes) a given string in Lua.
function unscramble(str)
local res = ""
local dtable = "7,Jsg(E<\fIvBT@3_{|k\005Ww0#P\000\015\031rmG]~\030}\"xut\017X\004\016\006`+\t\001)l*\aq%ULh.6 \b\025;OQ\003\\\002\029ZN\0235\014[$e1K\027d\v4Y!^\rVi8fMc'>b:RjHA-CznS\021\028a\026\022F9o\n\018\019?yp\020=/&D2\024"
for i = 1, #str do
local b = str:byte(i)
if b > 0 and b <= 127 then
res = res .. string.char(dtable:byte(b))
else
res = res .. string.char(b)
end
end
return res
end
The idea seems pretty straightforward - to replace all ASCII symbols with their positional counterparts in dtable variable.
I used https://www.tutorialspoint.com/execute_lua_online.php to verify
print(unscramble("dM22r"))
Hello
My best shot at it in Python is
def unscramble(str):
res = ''
dtable = b"7,Jsg(E<\fIvBT@3_{|k\005Ww0#P\000\015\031rmG]~\030}\"xut\017X\004\016\006`+\t\001)l*\aq%ULh.6 \b\025;OQ\003\\\002\029ZN\0235\014[$e1K\027d\v4Y!^\rVi8fMc'>b:RjHA-CznS\021\028a\026\022F9o\n\018\019?yp\020=/&D2\024"
for b in str:
res += chr(dtable[b-1]) if 0 < b < 128 else chr(b)
return res
and the result
print(unscramble(b"dM22r"))
j$llF
Well, it does not work. The problem is that I don't know Lua at all and am just making educated guesses below based on general knowledge. In this specific case the interpretation of bytestrings seems to be off.
Could somebody point me in the right direction?