开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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


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

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

查看: 149|回复: 2
收起左侧

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

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


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


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

QQ浏览器截图20250401222114.png
我用御风的写出来没有效果 不知道是模块的原因还是啥,求大神指导一下或者用支持库也行

最佳答案

查看完整内容

不要使用别人的模块,局限性太多。 1.下载LUA官方库lua5.1.dll或者更新的版本, 2.把这个DLL放在你编译后的程序的同级目录, 3.在编程时声明本DLL的函数 [e=0].版本 2 .DLL命令 luaL_newstate, 整数型, "lua5.1.dll", "luaL_newstate" .DLL命令 luaL_loadstring, 整数型, "lua5.1.dll", "luaL_loadstring" 参数 lua_state: 整数型 参数 script: 文本型 .DLL命令 lua_pcall, 整数型, "lua5.1.dll", "lua_pcall" 参数 l ...

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

签到天数: 6 天

发表于 2025-4-1 22:50:45 | 显示全部楼层   河南省郑州市
不要使用别人的模块,局限性太多。
1.下载LUA官方库lua5.1.dll或者更新的版本,
2.把这个DLL放在你编译后的程序的同级目录,
3.在编程时声明本DLL的函数
  
.版本 2
DLL命令名返回值类型公开备 注
luaL_newstate整数型 
DLL库文件名:
lua5.1.dll
在DLL库中对应命令名:
luaL_newstate
参数名类 型传址数组备 注
DLL命令名返回值类型公开备 注
luaL_loadstring整数型 
DLL库文件名:
lua5.1.dll
在DLL库中对应命令名:
luaL_loadstring
参数名类 型传址数组备 注
参数 lua_state: 整数型
参数 script: 文本型
DLL命令名返回值类型公开备 注
lua_pcall整数型 
DLL库文件名:
lua5.1.dll
在DLL库中对应命令名:
lua_pcall
参数名类 型传址数组备 注
参数 lua_state: 整数型
参数 nargs: 整数型
参数 nresults: 整数型
参数 errfunc: 整数型


[color=rgba(0, 0, 0, 0.9)]调用LUA脚本示例
  
子程序名返回值类型公开备 注
执行LUA脚本逻辑型  
参数名类 型参考可空数组备 注
脚本内容文本型
变量名类 型静态数组备 注
L整数型  
L = luaL_newstate ()
如果真 (L = 0)
返回 ()
.如果真结束
如果真 (luaL_loadstring (L, 脚本内容) ≠ 0)
' 处理加载错误
返回 ()
.如果真结束
如果真 (lua_pcall (L, 0, 0, 0) ≠ 0)
' 处理执行错误
返回 ()
.如果真结束
' 释放LUA状态
' (需根据DLL提供的函数释放资源)
返回 ()
回复

使用道具 举报

结帖率:45% (14/31)

签到天数: 7 天

 楼主| 发表于 2025-4-2 19:29:45 | 显示全部楼层   广东省梅州市
神行 发表于 2025-4-2 13:37
不要使用别人的模块,局限性太多。
1.下载LUA官方库lua5.1.dll或者更新的版本,
2.把这个DLL放在你编译后的 ...

大佬 有没有指定运行某个函数的啊,你这个好像不能传参吧?
回复

使用道具 举报

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

本版积分规则 致发广告者

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

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

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