Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function tostr(bts)
- local str = ""
- for k, v in pairs(bts) do
- if type(v) == "number" then
- str = str..string.char(v)
- end
- end
- return str
- end
- local function tobits(str)
- local rtrntbl = {}
- for char in string.gmatch(str, ".") do
- rtrntbl[#rtrntbl+1] = string.byte(char)
- end
- return rtrntbl
- end
- local function mkKey(key)
- local key = tostring(key)
- local str = ""
- for char in string.gmatch(key, ".") do
- str = str..tostring(string.byte(char))
- end
- return str
- end
- local function sepkey(key)
- local key = tostring(key)
- local ky = {}
- for char in string.gmatch(key, ".") do
- ky[#ky+1] = tonumber(char)
- end
- return ky
- end
- local function enc(keyo, str1)
- local str2 = tobits(str1)
- local key = sepkey(mkKey(keyo))
- local num = 0
- for k, v in pairs(str2) do
- num = num+1
- if num > #key then
- num = 1
- end
- if v+key[num] > 255 then
- str2[k] = v+key[num]-255
- else
- str2[k] = v+key[num]
- end
- end
- str2 = tostr(str2)
- return str2
- end
- function encrypt(key, str)
- local keyn = sepkey(mkKey(key))
- keyn = keyn[#keyn-1]
- local rtrn
- local cnt = 0
- for i = 1, string.byte(keyn) do
- cnt = cnt+1
- rtrn = enc(key, str)
- if cnt == 100 then
- cnt = 0
- sleep(0)
- end
- end
- return rtrn
- end
- local function dec(keyo, str1)
- local str2 = tobits(str1)
- local key = sepkey(mkKey(keyo))
- local num = 0
- for k, v in pairs(str2) do
- num = num+1
- if num > #key then
- num = 1
- end
- if v+key[num] < 0 then
- str2[k] = v-key[num]+255
- else
- str2[k] = v-key[num]
- end
- end
- str2 = tostr(str2)
- return str2
- end
- function decrypt(key, str)
- local keyn = sepkey(mkKey(key))
- keyn = keyn[#keyn-1]
- local rtrn
- local cnt = 0
- for i = 1, string.byte(keyn) do
- cnt = cnt+1
- rtrn = dec(key, str)
- if cnt == 100 then
- cnt = 0
- sleep(0)
- end
- end
- return rtrn
- end
- local readdir = function(path)
- if fs.isDir(path) then
- return false, "cannot read directory"
- end
- local file = fs.open(path,"r")
- if file then
- local cont = file.readAll()
- return cont
- else
- return false, "no such file"
- end
- end
- local writetopath = function(path,str)
- if fs.isDir(path) then
- return false, "cannot write to existing directory"
- end
- local file = fs.open(path,"w")
- file.write(str)
- file.close()
- end
- function encryptFile(key,path)
- return writetopath(path,encrypt(key,readdir(path)))
- end
- function decryptFile(key,path)
- return writetopath(path,decrypt(key,readdir(path)))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement