-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt
80 lines (77 loc) · 2.41 KB
/
encrypt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
local function enc(str, key)
if not key then return false end
local f = ""
local kb = {}
for i = 1, #key do
kb[i] = string.byte(key:sub(i,i)) - 31
end
local c = 1
for i = 1, #str do
local t1 = string.byte(str:sub(i,i))
if t1 == 10 then
f = f .. string.char(10)
else
local t2 = t1 + kb[c]
if t2 > 126 then
local t3 = t2 - 126
t2 = 31 + t3
end
f = f .. string.char(t2)
end
c = c + 1
if c > #key then
c = 1
end
end
return f
end
local function dec(str, key)
if not key then return false end
local f = ""
local kb = {}
for i = 1, #key do
kb[i] = string.byte(key:sub(i,i)) - 31
end
local c = 1
for i = 1, #str do
local t2 = string.byte(str:sub(i,i)) - 31
if t2 == -21 then
f = f .. string.char(10)
else
local t3 = t2 - kb[c] + 31
if t3 < 32 then
t3 = t3 + 126 - 31
end
if t3 < 32 then return false, t3 end
if t3 > 126 then return false, t3 end
f = f .. string.char(t3)
end
c = c + 1
if c > #key then
c = 1
end
end
return f
end
function encrypt(str, key)
if not key then return false end
if #key < 3 then return false end
local f = str
key = key .. key .. "#me1"
for i = 0, #key - 1 do
f = enc(f, key:sub(i,i + 1))
end
f = enc(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
return f
end
function decrypt(str, key)
if not key then return false end
if #key < 3 then return false end
local f = str
key = key .. key .. "#me1"
for i = 0, #key - 1 do
f = dec(f, key:sub(i,i + 1))
end
f = dec(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
return f
end