关于lua 字符串的相关知识点
举例:
字符串 str
获取字符串长度 string.len(str)
获取某个i位置的节点 str[i] 这里i的值为 0 - 字符串长度-1
下面是我对字符串进行加密解密的算法
加密
local function baseencrypt(str)
local enstr ={}
local len,index = string.len(str),1
local ch = nil
local rep = nil
while index <= len do
repeat
ch = string.byte(str,index)
--print("str[",index,"]=",ch)
if ch >= (1+0*21) and ch < (1+0*21+21) then
rep = ch + 84
break
end
if ch >= (1+1*21) and ch < (1+1*21+21) then
rep = ch + 42
break
end
if ch >= (1+2*21) and ch < (1+2*21+21) then
rep = ch + 63
break
end
if ch >= (1+3*21) and ch < (1+3*21+21) then
rep = ch - 63
break
end
if ch >= (1+4*21) and ch < (1+4*21+21) then
rep = ch - 42
break
end
if ch >= (1+5*21) and ch < (1+5*21+21) then
rep = ch - 84
break
end
until true
table.insert(enstr,string.char(rep))
--print("append ch=",rep," str[",index,"] = ",ch)
index = index + 1
end
return table.concat(enstr)
end
解密算法
local function basedecrypt(str)
local len,index = string.len(str),0
while str[index] ~= 0 do
repeat
if str[index] >= (1+0*21) and str[index] < (1+0*21+21) then
str[index] = str[index] + 63
break
end
if str[index] >= (1+1*21) and str[index] < (1+1*21+21) then
str[index] = str[index] + 84
break
end
if str[index] >= (1+2*21) and str[index] < (1+2*21+21) then
str[index] = str[index] + 42
break
end
if str[index] >= (1+3*21) and str[index] < (1+3*21+21) then
str[index] = str[index] - 42
break
end
if str[index] >= (1+4*21) and str[index] < (1+4*21+21) then
str[index] = str[index] - 84
break
end
if str[index] >= (1+5*21) and str[index] < (1+5*21+21) then
str[index] = str[index] - 63
break
end
until true
index = index+1
end
end
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!