-- Define the Advanced Obfuscator Function
function obfuscate(script)
    -- Helper Functions
    local function generateRandomName()
        return "var" .. math.random(100000, 999999)
    end

    local function xorEncryptDecrypt(str, key)
        local encrypted = {}
        for i = 1, #str do
            local c = str:sub(i, i)
            local enc = string.char(string.byte(c) ~ key)
            table.insert(encrypted, enc)
        end
        return table.concat(encrypted)
    end

    local function encodeToBytes(str)
        return str:gsub(".", function(c)
            return string.format("\\%03d", string.byte(c))
        end)
    end

    local function obfuscateShortScript(script)
        local key = math.random(1, 255)
        local encoded = encodeToBytes(script)
        local encrypted = xorEncryptDecrypt(encoded, key)
        return "loadstring(decryptString(" .. string.format("%q", encrypted) .. ", " .. key .. "))()"
    end

    local function obfuscateCode(code)
        -- Variable Renaming
        local renamed = {}
        local counter = 0
        code = code:gsub("%f[%a_](%w+)%f[^%w_]", function(var)
            if not renamed[var] then
                counter = counter + 1
                renamed[var] = generateRandomName()
            end
            return renamed[var]
        end)

        -- String Encryption
        code = code:gsub('"(.-)"', function(str)
            local key = math.random(1, 255)
            local encryptedStr = xorEncryptDecrypt(str, key)
            return '"..decryptString("' .. encryptedStr .. '", ' .. key .. ').."'
        end)

        -- Function Wrapping with Obfuscated Loader
        code = [[
            local function decryptString(str, key)
                local decrypted = {}
                for i = 1, #str do
                    local c = str:sub(i, i)
                    local dec = string.char(string.byte(c) ~ key)
                    table.insert(decrypted, dec)
                end
                return table.concat(decrypted)
            end

            local function executeCode()
                ]] .. code .. [[
            end

            local function obfuscatedLoader()
                local loaderCode = ]] .. string.format("%q", encodeToBytes("executeCode()")) .. [[
                loadstring(decryptString(loaderCode, ]] .. math.random(1, 255) .. [[))()
            end

            obfuscatedLoader()
        ]]

        -- Control Flow Obfuscation
        code = [[
            local function controlFlow()
                if false then
                    while true do
                        if true then
                            ]] .. code .. [[
                        end
                    end
                else
                    -- Unreachable code
                    local dummy = function() end
                    dummy()
                end
            end
            controlFlow()
        ]]

        -- Dead Code Insertion
        code = [[
            local function dummyFunction()
                local dummy = 42
                for i = 1, 100 do
                    dummy = dummy + i
                end
            end
            dummyFunction()
        ]] .. code

        -- Arithmetic Obfuscation
        code = code:gsub("([%w_]+)%s*=%s*([%w_]+)%s*([+-%*/])%s*([%w_]+)", function(var1, var2, op, var3)
            local randomOp = math.random(1, 4)
            local randomValue = math.random(1, 10)
            if randomOp == 1 then
                return var1 .. "=" .. var2 .. op .. var3 .. "+" .. randomValue
            elseif randomOp == 2 then
                return var1 .. "=" .. var2 .. op .. var3 .. "-" .. randomValue
            elseif randomOp == 3 then
                return var1 .. "=" .. var2 .. op .. var3 .. "*" .. randomValue
            else
                return var1 .. "=" .. var2 .. op .. var3 .. "/" .. randomValue
            end
        end)

        -- Whitespace Removal & Code Flattening
        code = code:gsub("%s+", "") -- Remove whitespace
        code = code:gsub("\n", "") -- Remove newlines
        code = code:gsub("endend", "end") -- Flatten end statements

        return code
    end

    -- Final Encoding with Obfuscated Loader
    local function encodeWithObfuscatedLoader(code)
        local encoded = "return function() " .. code .. " end"
        local encodedBytes = encodeToBytes(encoded)
        local key = math.random(1, 255)
        local encryptedBytes = xorEncryptDecrypt(encodedBytes, key)
        return "loadstring(decryptString(" .. string.format("%q", encryptedBytes) .. ", " .. key .. "))()"
    end

    -- Obfuscate the Provided Script
    local obfuscatedCode = obfuscateCode(script)
    
    -- Encode into an obfuscated loader format
    return encodeWithObfuscatedLoader(obfuscatedCode)
end

-- Your Lua script to be obfuscated
local myScript = [[
    Script here!
]]

-- Obfuscate the Script
local obfuscatedScript = obfuscate(myScript)

-- Output the Obfuscated Script
print(obfuscatedScript)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: