lua rc4演算法實現

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   for   

  由於項目需要,用python django寫restful介面遇到瓶頸,python django+uwsgi處理請求是會阻塞的,

如果阻塞請求不及時處理,會卡住越來越多的其它的請求,導致越來越多的502。所以將請求處理頻繁的,會阻

塞長時間的介面用lua實現,lua放在nginx裡跑,還是很快的。

  呵呵,費話少說了!

  項目因用 到rc4密碼編譯演算法,但網上實現lua rc4演算法的很少,有的要依賴lua第三方庫,很不方便。根據wiki實

現自己的演算法:

  

-- RC4-- http://en.wikipedia.org/wiki/RC4function 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 Sendfunction 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 Kendfunction RC4(key, text)    local text_len = string.len(text)    local S = KSA(key)            local K = PRGA(S, text_len)     return output(K, text)endfunction 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 0endfunction bit_op.cond_xor(r_a, r_b)    return (r_a + r_b == 1) and 1 or 0endfunction bit_op.cond_or(r_a, r_b)    return (r_a + r_b > 0) and 1 or 0endfunction 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 resendfunction bxor(a, b)    return bit_op.base(‘cond_xor‘, a, b)endfunction band(a, b)    return bit_op.base(‘cond_and‘, a, b)endfunction 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.