Luacocos2d-Lua

Lua菱星算法(命名)实现

2023-01-15  本文已影响0人  binx6

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

--[=[
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)))
上一篇下一篇

猜你喜欢

热点阅读