SHOW:
|
|
- or go back to the newest paste.
1 | local function tostr(bts) | |
2 | local str = "" | |
3 | for k, v in pairs(bts) do | |
4 | if type(v) == "number" then | |
5 | str = str..string.char(v) | |
6 | end | |
7 | end | |
8 | return str | |
9 | end | |
10 | local function tobits(str) | |
11 | local rtrntbl = {} | |
12 | for char in string.gmatch(str, ".") do | |
13 | rtrntbl[#rtrntbl+1] = string.byte(char) | |
14 | end | |
15 | return rtrntbl | |
16 | end | |
17 | local function mkKey(key) | |
18 | local key = tostring(key) | |
19 | local str = "" | |
20 | for char in string.gmatch(key, ".") do | |
21 | str = str..tostring(string.byte(char)) | |
22 | end | |
23 | return str | |
24 | end | |
25 | local function sepkey(key) | |
26 | local key = tostring(key) | |
27 | local ky = {} | |
28 | for char in string.gmatch(key, ".") do | |
29 | ky[#ky+1] = tonumber(char) | |
30 | end | |
31 | return ky | |
32 | end | |
33 | local function enc(keyo, str1) | |
34 | local str2 = tobits(str1) | |
35 | local key = sepkey(mkKey(keyo)) | |
36 | local num = 0 | |
37 | for k, v in pairs(str2) do | |
38 | num = num+1 | |
39 | if num > #key then | |
40 | num = 1 | |
41 | end | |
42 | if v+key[num] > 255 then | |
43 | str2[k] = v+key[num]-255 | |
44 | else | |
45 | str2[k] = v+key[num] | |
46 | end | |
47 | end | |
48 | str2 = tostr(str2) | |
49 | return str2 | |
50 | end | |
51 | function encrypt(key, str) | |
52 | local keyn = sepkey(mkKey(key)) | |
53 | keyn = keyn[#keyn-1] | |
54 | local rtrn | |
55 | local cnt = 0 | |
56 | for i = 1, string.byte(keyn) do | |
57 | cnt = cnt+1 | |
58 | rtrn = enc(key, str) | |
59 | if cnt == 100 then | |
60 | cnt = 0 | |
61 | sleep(0) | |
62 | end | |
63 | end | |
64 | return rtrn | |
65 | end | |
66 | local function dec(keyo, str1) | |
67 | local str2 = tobits(str1) | |
68 | local key = sepkey(mkKey(keyo)) | |
69 | local num = 0 | |
70 | for k, v in pairs(str2) do | |
71 | num = num+1 | |
72 | if num > #key then | |
73 | num = 1 | |
74 | end | |
75 | if v+key[num] < 0 then | |
76 | str2[k] = v-key[num]+255 | |
77 | else | |
78 | str2[k] = v-key[num] | |
79 | end | |
80 | end | |
81 | str2 = tostr(str2) | |
82 | return str2 | |
83 | end | |
84 | function decrypt(key, str) | |
85 | local keyn = sepkey(mkKey(key)) | |
86 | keyn = keyn[#keyn-1] | |
87 | local rtrn | |
88 | local cnt = 0 | |
89 | for i = 1, string.byte(keyn) do | |
90 | cnt = cnt+1 | |
91 | rtrn = dec(key, str) | |
92 | if cnt == 100 then | |
93 | cnt = 0 | |
94 | sleep(0) | |
95 | end | |
96 | end | |
97 | return rtrn | |
98 | end | |
99 | ||
100 | local readdir = function(path) | |
101 | if fs.isDir(path) then | |
102 | return false, "cannot read directory" | |
103 | end | |
104 | local file = fs.open(path,"r") | |
105 | if file then | |
106 | local cont = file.readAll() | |
107 | return cont | |
108 | else | |
109 | return false, "no such file" | |
110 | end | |
111 | end | |
112 | ||
113 | local writetopath = function(path,str) | |
114 | if fs.isDir(path) then | |
115 | return false, "cannot write to existing directory" | |
116 | end | |
117 | local file = fs.open(path,"w") | |
118 | file.write(str) | |
119 | file.close() | |
120 | end | |
121 | ||
122 | function encryptFile(key,path) | |
123 | return writetopath(path,encrypt(key,readdir(path))) | |
124 | end | |
125 | function decryptFile(key,path) | |
126 | return writetopath(path,decrypt(key,readdir(path))) | |
127 | end |