Lua获得XOR密钥和解码

时间:2016-07-07 13:11:21

标签: file lua key xor decoding

我正在尝试获取xor文件的密钥并使用所述密钥对其进行解码。

function GetKey(data)
    local base = {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',
    'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}
    local key = ""
    for i=0, 100 do
        key = key .. string.char(bit32.bxor(data:sub(i+1,i+1):byte(), base[(i%(#base))+1]:byte()))
    end
    return key:sub(65,65) .. key:sub(34,64)
end

function Decode(s, key)
    local i = 1
    s = s:gsub("(.)", function(t)
        if (i > key:len()) then
            i = 1
        end
        k = bit32.bxor(t:byte(), key:sub(i,i):byte())
        i = i + 1
        return string.char(k)
    end)
    return s
end

local s = io.open(select(1,...), "rb"):read("*a")

local key = GetKey(s)
print("Key found: " .. key)
print("Decoding...")
s = Decode(s, key)
local outfile = io.open(select(1,...) .. ".xor", "wb")
print("Finished!")

这是我获取密钥和解码的代码。目前它获取了错误的密钥,因此错误地解码了文件。

任何人都有任何帮助或能给出一些指示?

由于

0 个答案:

没有答案