forked from trojanhoes/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.lua
More file actions
52 lines (40 loc) · 915 Bytes
/
Copy pathcipher.lua
File metadata and controls
52 lines (40 loc) · 915 Bytes
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
local bit = assert(bit or require('bit32') or require('bit') or require('LuaBit'), 'There are no any BIT library')
local seed = 1337 --CHANGE ME
local char, byte, xor = string.char, string.byte, bit.bxor
local function random(k, i)
return xor(seed * i + k * k, 2 ^ k - 1) % i
end
local function shuffle(k)
local t = {}
for i = 0, 255 do
t[i] = i
end
for i = 255, 1, -1 do
local j = random(k, i)
t[i], t[j] = t[j], t[i]
end
return t
end
local function revert(t)
local out = {}
for k, v in pairs(t) do
out[v] = k
end
return out
end
local cipher = {}
function cipher.encode(str, k)
local out, t = {}, shuffle(k)
for i, b in pairs({byte(str, 1, #str)}) do
out[i] = t[b]
end
return char(unpack(out))
end
function cipher.decode(str, k)
local out, t = {}, revert(shuffle(k))
for i, b in pairs({byte(str, 1, #str)}) do
out[i] = t[b]
end
return char(unpack(out))
end
return cipher