miduo_client/Assets/ManagedResources/~Lua/Base/HttpCrypt.lua

55 lines
1.4 KiB
Lua

-- 对字符串进行加密解密
require "Common.BitMath"
function strSplit(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
function StrToBytes(s)
local t={}
for i=1,string.len(s) do
table.insert(t,string.byte(string.sub(s,i,i)))
end
return t
end
function HTTP_ENCRYPT(src, key)
local data = StrToBytes(src)
local keys = StrToBytes(key)
local sb = ""
for i = 1, #data do
local n = BitMath.andOp(255, data[i]) + BitMath.andOp(255, keys[(i - 1) % #keys + 1])
sb = sb.."@"..n
end
return sb
end
function HTTP_DECRYPT(src, key)
local list = strSplit(src, "@")
if not list or #list < 1 then
return src
end
table.remove(list, 1)
local keys = StrToBytes(key)
local data = ""
for i = 1, #list do
local n = list[i] - BitMath.andOp(255, keys[(i - 1) % #keys + 1])
data = data .. string.char(n)
end
return data
end
-- local t = HTTP_ENCRYPT("fajkfkahsdfjkhkashd", "123")
-- print(t)
-- local data = HTTP_DECRYPT(t, "123")
-- print(data)