Encryption Lua加密算法

Encryption Lua加密算法,encryption,lua,Encryption,Lua,所以我正在lua中研究一种叫做S22的加密算法。我已经让加密部分工作,但解密要困难得多。有什么想法吗 我的加密算法: function S22Encrypt(MSG, bit) local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "

所以我正在lua中研究一种叫做S22的加密算法。我已经让加密部分工作,但解密要困难得多。有什么想法吗

我的加密算法:

function S22Encrypt(MSG, bit)
  local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  local finalstring = ""
  local count = 0
  MSG:gsub(".", function(c)
  for i,v in pairs(resources) do
            count = count + 1
             if v == c then
               finalstring = finalstring .. count * bit /69
               count = 0
           end
       end
  end)
  return finalstring
end
我的解密尝试:

function S22Decrypt(MSG, bit)
  local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    local finalstring = ""
    local count = 0
    MSG:gsub(".", function(c)
    for i,v in pairs(resources) do
          count = count + 1
          if v == c then
            local nmr = count / bit *69
            local count2 = 0
            for a,b in pairs(resources) do
                count2 = count2 + 1
                if count2 == nmr then
                  finalstring = finalstring .. count2 / bit * 69
                end
            end
            count = 0
          end
       end
    end)
    return finalstring
end

解密是通过尝试
count
的每个可能值并比较相应的代码来完成的:

function S22Decrypt(MSG, bit)
   local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
   local finalstring = ""
   local count = 0
   local resource_idx = 0
   while MSG ~= "" do
      count = count + 1
      if count > 1000 then
         return nil, "Can not decrypt"
      end
      resource_idx = resource_idx + 1
      if resource_idx > #resources then
         resource_idx = resource_idx - #resources
         finalstring = finalstring .. " "
      end
      local code = "" .. count * bit /69
      if MSG:sub(1, #code) == code then
         MSG = MSG:sub(#code + 1)
         finalstring = finalstring .. resources[resource_idx]
         count = #resources - resource_idx
         resource_idx = 0
      end
   end
   return finalstring
end
请注意,如果您在一个系统上加密,在另一个系统上解密,则加密脚本的Lua版本和解密脚本的Lua版本必须同时为Lua 5.3+或Lua 5.2-

例如,由于整数与字符串之间的差异(
42
vs
42.0
),在Lua5.1上加密的单词“hello”在Lua5.4上无法解密(反之亦然)

什么是S22,您能提供一个参考吗?您有什么问题吗?这不是调试服务。您至少应该缩小问题的范围。另请参见Maarten Bodewes,这是我的加密算法您的Lua版本(5.3+或旧版本)是什么?Egor Skriptunoff,谢谢,但当我运行您的解密代码时,它只是冻结了我的脚本并拒绝解密。我正在使用lua53,有没有关于如何修复此问题的想法?如何重现此问题?你的MSG和bit是什么(加密前)?我的MSG是“Hello World!”,bit对我来说很好。您的系统加密的结果是什么?我的结果是:15.304347826087 15.942028985507 13.710144927536 14.6666666667 17.536231884058 23.594202898551 14.666666666667 11.797101449275 11.159420289855