开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 106|回复: 1
收起左侧

[脚本] 易语言怎么运行lua加密解密,附lua代码

[复制链接]
结帖率:45% (14/31)
发表于 2025-4-1 22:03:00 | 显示全部楼层 |阅读模式   广东省梅州市
50精币
-- 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] = i
    end

    local j = 0
    for i = 0, 255 do
        j = (j + S[i] + string.byte(key, (i % key_len) + 1)) % 256
        S[i], S[j] = S[j], S[i] -- 交换 S[i] 和 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[i]) % 256
        S[i], S[j] = S[j], S[i] -- 交换 S[i] 和 S[j]
        local keystream_byte = S[(S[i] + 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[i] -- 使用 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[i] -- 使用 XOR 解密
        table.insert(result, string.char(decrypted_byte))
    end

    return table.concat(result)
end

-- 示例用法
--[===[math.randomseed(os.time()) -- 初始化随机种子]===]
local text = "我的世界露出"
local key = "supersecretkey"

-- 加密
local encrypted = rc4_encrypt(text, key)
print("Encrypted (Base64): " .. encrypted)

-- 解密
local decrypted = rc4_decrypt(encrypted, key)
print("Decrypted: " .. decrypted)


这个是lua的加密解密 怎么用lua支持库或者模块调用出来,论坛找了很多资料都调用不出来


回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳
结帖率:45% (14/31)

签到天数: 3 天

 楼主| 发表于 2025-4-1 22:22:55 | 显示全部楼层   广东省梅州市
QQ浏览器截图20250401222114.png
这是我用御风的模块测试了没有效果
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表