ObscuredValue.lua 3.86 KB
local cryptoKey = 0x020207070202
local delta = 8

local mt = {}

function mt.__add(a, b)
    local ta = type(a)
    local tb = type(b)

    if ta == "table" and tb == "table" then
        return Obscured(a.Decrypt() + b.Decrypt())
    end

    if ta == "number" then
        return Obscured(a + b.Decrypt())
    end

    if tb == "number" then
        return Obscured(a.Decrypt() + b)
    end
    
    error("add wrong")
end

function mt.__sub(a, b)
    local ta = type(a)
    local tb = type(b)

    if ta == "table" and tb == "table" then
        return Obscured(a.Decrypt() - b.Decrypt())
    end

    if ta == "number" then
        return Obscured(a - b.Decrypt())
    end

    if tb == "number" then
        return Obscured(a.Decrypt() - b)
    end

    error("sub wrong")
end

function mt.__mul(a, b)
    local ta = type(a)
    local tb = type(b)

    if ta == "table" and tb == "table" then
        return Obscured(a.Decrypt() * b.Decrypt())
    end

    if ta == "number" then
        return Obscured(a * b.Decrypt())
    end

    if tb == "number" then
        return Obscured(a.Decrypt() * b)
    end

    error("sub wrong")
end

function mt.__div(a, b)
    local ta = type(a)
    local tb = type(b)

    if ta == "table" and tb == "table" then
        return Obscured(a.Decrypt() / b.Decrypt())
    end

    if ta == "number" then
        return Obscured(a / b.Decrypt())
    end

    if tb == "number" then
        return Obscured(a.Decrypt() / b)
    end

    error("sub wrong")
end

function mt.__mod(a, b)
    local ta = type(a)
    local tb = type(b)

    if ta == "table" and tb == "table" then
        return Obscured(a.Decrypt() % b.Decrypt())
    end

    if ta == "number" then
        return Obscured(a % b.Decrypt())
    end

    if tb == "number" then
        return Obscured(a.Decrypt() % b)
    end

    error("sub wrong")
end

function mt.__unm(a)
    return Obscured(-a.Decrypt())
end

function mt.__len(a)
    return #a.Decrypt()
end

function mt.__lt(a, b)
    return a.Decrypt() < b.Decrypt()
end

function mt.__le(a, b)
    return a.Decrypt() <= b.Decrypt()
end

function mt.__eq(a, b)
    return a.Decrypt() == b.Decrypt()
end

function mt.__call(t)
    return t.Decrypt()
end

function mt.__newindex(t,_,v)
    t.SetVaule(v)
end

function mt.__index(t)
    return t.Decrypt()
end

local function Obscured(value)
    local hiddenValue = value
    local currentKey = cryptoKey
    local dataName = type(value)

    local o = {}
    
    function o.Encrypt(value, key)
        key = key or currentKey

        if type(value) == "number" then
            value = tostring(value)
        end

        hiddenValue = {}

        for i = 1, #value do
            hiddenValue[i] = string.byte(value, i)
            hiddenValue[i] = bit.bxor(hiddenValue[i], key)
            if hiddenValue[i] % 2 == 1 then
                hiddenValue[i] = 2*hiddenValue[i] + delta + 7
            else
                hiddenValue[i] = 2*hiddenValue[i] - delta
            end
        end
    end

    function o.Decrypt(key)
        key = key or currentKey
        local d = ""

        for i,v in ipairs(hiddenValue) do
            if v % 2 == 1 then
                v = v - delta - 7
            else
                v = v + delta
            end
            v = v / 2
            v = bit.bxor(v, key)
            d = d .. string.char(v)
        end

        if dataName == "number" then
            d = tonumber(d)
        end

        return d
    end
    
    function o.SetNewCryptoKey(newKey)
        cryptoKey = newKey
        o.ApplyNewCryptoKey()
    end

    function o.ApplyNewCryptoKey()
        if currentKey ~= cryptoKey then
            o.Encrypt(Decrypt(), cryptoKey)
            currentKey = cryptoKey
        end
    end
    
    function o.SetVaule(value) 
        hiddenValue = value
        dataName = type(value)
        o.Encrypt(value)
    end

    o.Encrypt(value)
    setmetatable(o,mt)
    return o
end

return Obscured