美文网首页Luacocos2d-Lua
Lua菱星算法(命名)实现

Lua菱星算法(命名)实现

作者: binx6 | 来源:发表于2023-01-15 09:21 被阅读0次

    究竟新的一年是莲步轻移还是平步青云,展望着去吧

    --[=[
    Author:bbs.binx6.cc
    Tip:这个算法我命名其为菱星算法,表面看起来有Base的影子实际不然,这些字符只是表象可以化去
    Emergency Notice:Copyright By Author ©,转载请务必保留作者信息
    --]=]
    
    -- 菱星编码
    function encrypt(str, key)
        local key = key or "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_=❖"
        local result = ""
        local len = #str
        local j = 1
        for i=1,len do
            local char = string.sub(str,i,i)
            local keychar = string.sub(key,j,j)
            local a = string.byte(char)
            local b = string.byte(keychar)
            local c = a + b
            if c > 255 then
                c = c - 255
            end
            result = result .. string.char(c)
            j = j + 1
            if j > #key then
                j = 1
            end
        end
        return result
    end
    
    -- 菱星解码
    function decrypt(str, key)
        local key = key or "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_=❖"
        local result = ""
        local len = #str
        local j = 1
        for i=1,len do
            local char = string.sub(str,i,i)
            local keychar = string.sub(key,j,j)
            local a = string.byte(char)
            local b = string.byte(keychar)
            local c = a - b
            if c < 0 then
                c = c + 255
            end
            result = result .. string.char(c)
            j = j + 1
            if j > #key then
                j = 1
            end
        end
        return result
    end
    
    -- local str = ""
    local str = [[
    -- 内容放这里,已经过几轮严谨的测试,若仍发生奇怪或特殊的问题敬请私信,有空就会答复
    ]]
    
    print(encrypt(str))
    print(decrypt(encrypt(str)))
    

    相关文章

      网友评论

        本文标题:Lua菱星算法(命名)实现

        本文链接:https://www.haomeiwen.com/subject/xmincdtx.html