• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

luarc4算法实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  由于项目需要,用python django写restful接口遇到瓶颈,python django+uwsgi处理请求是会阻塞的,

如果阻塞请求不及时处理,会卡住越来越多的其它的请求,导致越来越多的502。所以将请求处理频繁的,会阻

塞长时间的接口用lua实现,lua放在nginx里跑,还是很快的。

  呵呵,费话少说了!

  项目因用 到rc4加密算法,但网上实现lua rc4算法的很少,有的要依赖lua第三方库,很不方便。根据wiki

现自己的算法:

  

-- RC4
-- http://en.wikipedia.org/wiki/RC4

function KSA(key)
    local key_len = string.len(key)
    local S = {}
    local key_byte = {}

    for i = 0, 255 do
        S[i] = i
    end

    for i = 1, key_len do
        key_byte[i-1] = string.byte(key, i, i)
    end

    local j = 0
    for i = 0, 255 do
        j = (j + S[i] + key_byte[i % key_len]) % 256
        S[i], S[j] = S[j], S[i]
    end
    return S
end

function PRGA(S, text_len)
    local i = 0
    local j = 0
    local K = {}

    for n = 1, text_len do

        i = (i + 1) % 256
        j = (j + S[i]) % 256

        S[i], S[j] = S[j], S[i]
        K[n] = S[(S[i] + S[j]) % 256]
    end
    return K
end

function RC4(key, text)
    local text_len = string.len(text)

    local S = KSA(key)        
    local K = PRGA(S, text_len) 
    return output(K, text)
end

function output(S, text)
    local len = string.len(text)
    local c = nil
    local res = {}
    for i = 1, len do
        c = string.byte(text, i, i)
        res[i] = string.char(bxor(S[i], c))
    end
    return table.concat(res)
end


-------------------------------
-------------bit wise-----------
-------------------------------

local bit_op = {}
function bit_op.cond_and(r_a, r_b)
    return (r_a + r_b == 2) and 1 or 0
end

function bit_op.cond_xor(r_a, r_b)
    return (r_a + r_b == 1) and 1 or 0
end

function bit_op.cond_or(r_a, r_b)
    return (r_a + r_b > 0) and 1 or 0
end

function bit_op.base(op_cond, a, b)
    -- bit operation
    if a < b then
        a, b = b, a
    end
    local res = 0
    local shift = 1
    while a ~= 0 do
        r_a = a % 2
        r_b = b % 2
   
        res = shift * bit_op[op_cond](r_a, r_b) + res 
        shift = shift * 2

        a = math.modf(a / 2)
        b = math.modf(b / 2)
    end
    return res
end

function bxor(a, b)
    return bit_op.base('cond_xor', a, b)
end

function band(a, b)
    return bit_op.base('cond_and', a, b)
end

function bor(a, b)
    return bit_op.base('cond_or', a, b)
end

--key = "Key"
--text = "Plaintext"
--K = RC4(key, text)
--print (K)
--text = RC4(key, K)
--print (text)
--
--key = "Wiki"
--text = "pedia"
--K = RC4(key, text)
--print (K)
--
--key = "Secret"
--text = "Attack at dawn"
--K = RC4(key, text)
--print (K)

 

  可以根据python的Crypto.Cipher库中ARC4算法比较,相关代码在github


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua中如何实现类似gdb的断点调试--02通用变量打印发布时间:2022-07-22
下一篇:
lua文件读写处理(操作敏感词库)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap