Reversing an decode function

前端 未结 1 456
时光取名叫无心
时光取名叫无心 2021-01-23 09:03

I\'m trying to reverse a decode function. This function takes a string and a key and encodes the string with that key. This is the code:

function decode(key, c         


        
相关标签:
1条回答
  • 2021-01-23 09:42

    By using the answer to this Lua base coverter and flipping the operators of the decode function, I was able to convert back the input.

    This is the whole code:

    function encodes(key, code)
      return (code:gsub("..", function(h)
        return string.char((tonumber(h, 16) + 256 - 13 - key + 255999744) % 256)
      end))
    end
    
    local floor,insert = math.floor, table.insert
    function basen(n,b)
        n = floor(n)
        if not b or b == 10 then return tostring(n) end
        local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        local t = {}
        local sign = ""
        if n < 0 then
            sign = "-"
        n = -n
        end
        repeat
            local d = (n % b) + 1
            n = floor(n / b)
            insert(t, 1, digits:sub(d,d))
        until n == 0
        return sign .. table.concat(t,"")
    end
    
    function decodes(key, code)
      return (code:gsub(".", function(h)
        out = (string.byte(h) - 256 + 13 + key - 255999744) % 256
        return basen(out,16)
      end))
    end
    
    a = encodes(9999, "7c7A")
    print(a) --prints: `^
    print("----------")
    b = decodes(9999, a)
    print(b) --prints: 7C7A
    
    0 讨论(0)
提交回复
热议问题