Advertisement
LDDestroier

CC Hard Drive Encrypter/Decrypter

Mar 16th, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. --[[
  2.  pastebin run 0DUyWTFc
  3. --]]
  4. --API made by valithor.
  5. local encrypt = function(msg,key)
  6.     local num = ""
  7.     for i = 1, #key do
  8.         local let = key:sub(i,i):byte()
  9.         num = let <= 9  and num.."99"..let or let<=99 and num.."9"..let or num..let
  10.         num = #msg..num
  11.     end
  12.     math.randomseed(tonumber(num))
  13.     local encrypt = ""
  14.     for i = 1, #msg do
  15.         local rotation = math.random(0,94)
  16.         local byte = msg:sub(i,i):byte()
  17.         local rotate = rotation+byte <= 127 and rotation +byte or ((rotation+byte)%127)+32
  18.         encrypt = encrypt..string.char(rotate)
  19.     end
  20.     return encrypt
  21. end
  22.  
  23. local fixstr = function(str)
  24.     return str:gsub(string.char(9)," ")
  25. end
  26.  
  27. local yield = function()
  28.     os.queueEvent("yield")
  29.     os.pullEvent("yield")
  30. end
  31.  
  32. local decrypt = function(msg,key)
  33.     local num = ""
  34.     for i = 1, #key do
  35.         local let = key:sub(i,i):byte()
  36.         num = let <= 9 and num.."99"..let or let<=99 and num.."9"..let or num..let
  37.         num = #msg..num
  38.     end
  39.     math.randomseed(tonumber(num))
  40.     local decrypt = ""
  41.     for i = 1, #msg do
  42.         local rotation = math.random(0,94)
  43.         local byte = msg:sub(i,i):byte()
  44.         local rotate = byte-rotation >= 32 and byte-rotation or byte-rotation
  45.         if rotate < 32 then
  46.             rotate = rotate+95
  47.         end
  48.         decrypt = decrypt..string.char(rotate)
  49.     end
  50.     return decrypt
  51. end
  52.  
  53. local tEnc = function(msg,key)
  54.     return encrypt(tostring(msg),key)
  55. end
  56. local tDec = function(msg,key)
  57.     return decrypt(tostring(msg),key)
  58. end
  59.  
  60. listAll = function(_path, _files, noredundant, excludeFiles)
  61.     local thisProgram = ""
  62.     if shell then
  63.         thisProgram = shell.getRunningProgram()
  64.     end
  65.     local path = _path or ""
  66.     local files = _files or {}
  67.     if #path > 1 then table.insert(files, path) end
  68.     for _, file in ipairs(fs.list(path)) do
  69.         local path = fs.combine(path, file)
  70.         if (file ~= thisProgram) and (not fs.isReadOnly(file)) then
  71.             local guud = true
  72.             for a = 1, #excludeFiles do
  73.                 if excludeFiles[a] == file then
  74.                     guud = false
  75.                     break
  76.                 end
  77.             end
  78.             if guud then
  79.                 if fs.isDir(path) then
  80.                     listAll(path, files, noredundant, excludeFiles)
  81.                 else
  82.                     table.insert(files, path)
  83.                 end
  84.             end
  85.         end
  86.     end
  87.     if noredundant then
  88.         for a = 1, #files do
  89.             if fs.isDir(tostring(files[a])) then
  90.                 if #fs.list(tostring(files[a])) ~= 0 then
  91.                     table.remove(files,a)
  92.                 end
  93.             end
  94.         end
  95.     end
  96.     return files
  97. end
  98.  
  99. local function choice(input,visual)
  100.     if visual then
  101.         write("[")
  102.         for a = 1, input:len() do
  103.             write(input:sub(a,a):upper())
  104.             if a ~= input:len() then write(",") end
  105.         end
  106.         write("]?")
  107.     end
  108.     local event, button
  109.     repeat
  110.         event, button = os.pullEvent("key")
  111.         if type(button) == "number" then button = keys.getName(button) end
  112.         if button == nil then button = " " end
  113.     until string.find(input, button)
  114.     if visual then
  115.         print(button:upper())
  116.     end
  117.     return button, true
  118. end
  119.  
  120.  
  121. local getEncAll = function(encOrDec,kee,_paath,exclude)
  122.     local paath = _paath or ""
  123.     local contents
  124.     local filetree = {}
  125.     local filelist = listAll(paath,nil,true,exclude)
  126.     for a = 1, #filelist do
  127.         local isDir
  128.         contents = {}
  129.         if not fs.isDir(filelist[a]) then
  130.             local file = fs.open(filelist[a],"r")
  131.             local line = ""
  132.             local s = 0
  133.             while line do
  134.                 line = file.readLine()
  135.                 if line then
  136.                     if encOrDec == "e" then
  137.                         table.insert(contents,tEnc(fixstr(line),kee))
  138.                     else
  139.                         table.insert(contents,tDec(fixstr(line),kee))
  140.                     end
  141.                     if s >= 64 then
  142.                         yield()
  143.                         s = 0
  144.                     else
  145.                         s = s + 1
  146.                     end
  147.                 end
  148.             end
  149.             file.close()
  150.             isDir = false
  151.         else
  152.             contents = {""}
  153.             isDir = true
  154.         end
  155.         filetree[filelist[a]] = {fyle = contents, dir = isDir}
  156.     end
  157.     return filetree
  158. end
  159.  
  160. doCrypting = function(mode,key,path,exclude)
  161.     if mode ~= "e" and mode ~= "d" then
  162.         error("mode should be 'e' or 'd'")
  163.     end
  164.     key = key or "password"
  165.     path = path or ""
  166.     local amnt = 0
  167.     local encedFiles = getEncAll(mode,key,path,exclude)
  168.     for k,v in pairs(encedFiles) do
  169.         amnt = amnt + 1
  170.         if not v.dir then
  171.             if not fs.getDir(k) then fs.makeDir(fs.getDir(k)) end
  172.             local file = fs.open(k,"w")
  173.             file.write(table.concat(v.fyle,"\n"))
  174.             file.close()
  175.         else
  176.             fs.makeDir(k)
  177.         end
  178.     end
  179.     return amnt, mode
  180. end
  181. if shell then --if not being used as an API
  182.     sleep(0)
  183.     print("Encrypt or decrypt?")
  184.     local ency = choice("ed",true)
  185.     print("\nWhat path?")
  186.     write("/")
  187.     sleep(0)
  188.     local doPath = read()
  189.     print("\nEnter key:")
  190.     write(":")
  191.     sleep(0)
  192.     local enckey = read("*")
  193.     if ency == "e" then
  194.         print("Encrypting...")
  195.     else
  196.         print("Decrypting...")
  197.     end
  198.     doCrypting(ency,enckey,doPath,{shell.getRunningProgram()})
  199.     print("Done!")
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement