反转解码功能 [英] Reversing an decode function

查看:128
本文介绍了反转解码功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反转解码功能.此函数接受一个字符串和一个密钥,并使用该密钥对字符串进行编码.这是代码:

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, code)
  return (code:gsub("..", function(h)
    return string.char((tonumber(h, 16) + 256 - 13 - key + 255999744) % 256)
  end))
end

如果我输入7A作为code9990作为key,它将返回g

If I input 7A as code and 9990 as key, it returns g

我尝试反转运算符并反馈了解码功能的输出,但由于出现错误,因此tonumber()返回nil.如何取消此功能?

I tried reversing the operators and fed back the output of the decode function but I get an error becauase tonumber() returns nil. How can I reverse this function?

推荐答案

使用对此 Lua基础掩盖版的答案并翻转解码函数的运算符,就可以将输入转换回去.

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.

这是完整的代码:

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

这篇关于反转解码功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆