Lua加密算法

所以我正在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
点赞
用户1847592
用户1847592

解密是通过尝试每个可能的 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, "无法解密"
      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-。

例如,在 Lua 5.1 上加密的单词“hello”将无法在 Lua 5.4 上解密(反之亦然),原因是由于整数到字符串之间的差异( 42 vs 42.0)。

2021-05-07 15:07:32