获取 lua 脚本中的所有字符串

我正在尝试对 lua 脚本中的一些字符串进行编码,并且由于我有一个由超过 200K 个字符组成的 lua 脚本,因此使用函数对脚本中的每个字符串查询进行加密,例如下面的示例

local string   =    "stackoverflow"
local string   =   [[stackoverflow]]
local string   = [==[stackoverflow]==]
local string   =    'stackoverflow'

to

local string=decode("jkrtbfmviwcfn",519211)

试图通过 gsub 将上述所有结果提供给线程,并让 gsub 使用随机偏移数字对字符串文本进行编码。

到目前为止,我只能通过 gsub 完整引用标记。

function encode(x,offset,a)
    for char in string.gmatch(x, "%a") do
        local encrypted = string.byte(char) + offset
        while encrypted > 122 do
            encrypted = encrypted - 26
        end
        while encrypted < 97 do
            encrypted = encrypted + 26
        end
        a[#a+1] = string.char(encrypted)
    end
    return table.concat(a)
end
luacode=[==[thatstring.Value="Encryptme!" testvalue.Value=[[string with
a linebreak]] string.Text="STOP!"]==]
luacode=luacode:gsub([=["(.-)"]=],function(s)
    print("Caught "..s)
    local offset=math.random(1,4)
    local encoded=encode(s,offset,{})
    return [[decode("]]..encoded..[[",]]..offset..[[)]]
end)
print("\n"..luacode)

其输出为

Caught Encryptme!
Caught STOP!

thatstring.Value=decode("crgvctxqi",4) testvalue.Value=[[string with
a linebreak]] string.Text=decode("opkl",2)

有更好的解决方案吗?

点赞