lua代码
-- Base64 编码
local function base64_encode(data)
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return ((data:gsub('.', function(x)
local r, b = '', x:byte()
for i = 8, 1, -1 do r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0') end
return r
end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c = 0
for i = 1, 6 do c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0 )end
return b:sub(c + 1, c + 1)
end) .. ({ '', '==', '=' })[#data % 3 + 1])
end
-- Base64 解码
local function base64_decode(data)
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
data = string.gsub(data, '[^' .. b .. '=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r, f = '', (b:find(x) - 1)
for i = 6, 1, -1 do r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and '1' or '0') end
return r
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c = 0
for i = 1, 8 do c = c + (x:sub(i, i) == '1' and 2 ^ (8 - i) or 0) end
return string.char(c)
end))
end
-- 生成随机字符串
local function generate_random_string(length)
local chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
local random_string = ""
for i = 1, length do
local rand = math.random(1, #chars)
random_string = random_string .. string.sub(chars, rand, rand)
end
return random_string
end
-- RC4 密钥调度算法(KSA)
local function KSA(key)
local key_len = #key
local S = {}
for i = 0, 255 do
S = i
end
local j = 0
for i = 0, 255 do
j = (j + S + string.byte(key, (i % key_len) + 1)) % 256
S, S[j] = S[j], S -- 交换 S 和 S[j]
end
return S
end
-- RC4 伪随机生成算法(PRGA)
local function PRGA(S, text_len)
local i, j = 0, 0
local keystream = {}
for k = 1, text_len do
i = (i + 1) % 256
j = (j + S) % 256
S, S[j] = S[j], S -- 交换 S 和 S[j]
local keystream_byte = S[(S + S[j]) % 256]
table.insert(keystream, keystream_byte)
end
return keystream
end
-- RC4 加密函数
local function rc4_encrypt(text, key)
-- 生成随机 IV(16 字节)
local iv = generate_random_string(16)
-- 将 IV 和密钥结合
local combined_key = iv .. key
-- 加密
local S = KSA(combined_key)
local text_len = #text
local keystream = PRGA(S, text_len)
local result = {}
for i = 1, text_len do
local text_byte = string.byte(text, i)
local encrypted_byte = text_byte ~ keystream -- 使用 XOR 加密
table.insert(result, string.char(encrypted_byte))
end
-- 返回 Base64 编码的 IV 和加密结果
return base64_encode(iv .. table.concat(result))
end
-- RC4 解密函数
local function rc4_decrypt(encrypted_text, key)
-- Base64 解码
local decoded_text = base64_decode(encrypted_text)
-- 提取 IV(前 16 字节)
local iv = string.sub(decoded_text, 1, 16)
-- 提取密文
local ciphertext = string.sub(decoded_text, 17)
-- 将 IV 和密钥结合
local combined_key = iv .. key
-- 解密
local S = KSA(combined_key)
local text_len = #ciphertext
local keystream = PRGA(S, text_len)
local result = {}
for i = 1, text_len do
local encrypted_byte = string.byte(ciphertext, i)
local decrypted_byte = encrypted_byte ~ keystream -- 使用 XOR 解密
table.insert(result, string.char(decrypted_byte))
end
return table.concat(result)
end
-- 示例用法
--[===[math.randomseed(os.time()) -- 初始化随机种子]===]
local text = "我的世界露出"
local key = "supersecretkey"