lua string函数练习-加密解密

淡若初秋学习日记 淡若初秋学习日记 217 人阅读 | 1 人回复 | 2025-04-03

[i=s] 本帖最后由 arklr73021 于 2025-4-3 13:27 编辑 [/i]

利用string.type函数和string.char函数偏移来尝试字符串加密解密,在加密解密过程中利用table存储加解密获得的ascii码

-- --**********************************************************************
local tb = {} -- 设置一个表用于存放加密前的ascii码
local encryptiontb = {} -- 设置一个表用于存放加密后的ascii码
local function encryption(str,offset)
    for i = 1 , #str do
        tb[i] = string.byte(str,i) -- 用表tb来存储转换获取的ascii码
        encryptiontb[i] = string.byte(str,i) + offset --对转换获得的ASCII码进行偏移+9 的加密
    end
    print("加密前:"..table.concat(tb,","))
    print("加密后:"..table.concat(encryptiontb,",").."--加密偏移值:"..offset)
    local nn = ""
    local nn = table.concat(encryptiontb,",")
    return nn -- 将加密后的字符串返回
end
QA = encryption("world",9)
print("获取到的加密后的字符串:"..QA)


local function decrypt(encryptString,decryptionOffset)
    local li = {}
    for k , v in pairs(encryptString) do
        li[k] = string.char(encryptString[k]-decryptionOffset) -- 迭代解密并将解密获得的值存入到表(li)中
    end
    return li
end

local oo = decrypt(encryptiontb,9) -- 函数传入上面加密偏移+9获取的表,加密偏移数值
print("解密获取字符串:"..table.concat(oo))

--优化修改解密函数

local tb = {} -- 设置一个表用于存放加密前的ascii码
local encryptiontb = {} -- 设置一个表用于存放加密后的ascii码
local function encryption(str,offset)
    for i = 1 , #str do
        tb[i] = string.byte(str,i) -- 用表tb来存储转换获取的ascii码
        encryptiontb[i] = string.byte(str,i) + offset --对转换获得的ASCII码进行偏移+9 的加密
    end
    print("加密前:"..table.concat(tb,","))
    print("加密后:"..table.concat(encryptiontb,",").."--加密偏移值:"..offset)
    local nn = ""
    local nn = table.concat(encryptiontb,",")
    return nn -- 将加密后的字符串返回
end
QA = encryption("world",9)
print("获取到的加密后的字符串:"..QA)


local function decrypt(encryptString,decryptionOffset)
    local li = {}
    for k , v in pairs(encryptString) do
        table.insert(li,string.char(encryptString[k]-decryptionOffset))  -- 迭代解密并将解密获得的值存入到表(li)中
    end
    return li
end

local oo = decrypt(encryptiontb,9) -- 函数传入上面加密偏移+9获取的表,加密偏移数值
print("解密获取字符串:"..table.concat(oo))

分享到:

回答|共 1 个

飓风_Ai

发表于 2025-4-3 01:31:22 | 显示全部楼层


arklr73021 发表于 2025-4-3 01:31
lua string函数练习-加密解密利用string.type函数和string.char函数偏移来尝试字符串加密解密,在加密解密过 ...
哈哈,看来你在练习字符串加密解密呢!这个代码写得不错,不过有几个小问题需要改进哦~让我来帮你优化一下:

1. 你用了tb和encryptiontb作为表,但每次循环都直接赋值而不是插入元素
2. decrypt函数处理方式不太对

优化后的版本:
  1. local function encryption(str, offset)
  2.     local tb = {}
  3.     local encryptiontb = {}
  4.     for i = 1, #str do
  5.         table.insert(tb, string.byte(str, i))
  6.         table.insert(encryptiontb, string.byte(str, i) + offset)
  7.     end
  8.     print("加密前:"..table.concat(tb,","))
  9.     print("加密后:"..table.concat(encryptiontb,",").."--加密偏移值:"..offset)
  10.     return encryptiontb
  11. end
  12. local function decrypt(encryptTable, decryptionOffset)
  13.     local li = {}
  14.     for k, v in ipairs(encryptTable) do
  15.         li[k] = string.char(v - decryptionOffset)
  16.     end
  17.     return table.concat(li)
  18. end
  19. local encrypted = encryption("world", 9)
  20. print("解密获取字符串:"..decrypt(encrypted, 9))
复制代码

加油啊小伙子!你已经在掌握lua字符串操作的路上了!记住:每个优秀的程序员都是从写加密解密开始的,就像每个黑客都是从"Hello World"开始的~ 😄

PS:下次可以试试更复杂的加密算法,比如凯撒密码的变种!我看好你哦!   

-- Ai喂养中
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则