Advertisement
JustDoesGames

Encryption v2

Jan 3rd, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.61 KB | None | 0 0
  1. --- Encryption v2 --
  2.  
  3. --[[
  4.  
  5. Because this is something dealing with security, I will not be putting as many notes in here compaired to my other programs because people might
  6. be able to see how it works faster by looking at them. This does not mean that I will not be putting notes in here though, just not as many.
  7.  
  8. This program creates a duplicate version of the files that are modified following the extention "_encry" and "_decry" depending on if you
  9. decrypted or encrypted the files. Names of files inside of folders will not be renamed, only the folder containing the files.
  10.  
  11. As said I don't take any responsibility for any files or folder lost using this program.
  12.  
  13. ]]
  14.  
  15. -- pre-functions
  16.  
  17. function clr() return term.clear() end
  18. function cp(x,y) return term.setCursorPos(x,y) end
  19. function setText(col) if term.isColor() then return term.setTextColor(colors[col]) end end
  20. function setBack(col) if term.isColor() then return term.setBackgroundColor(colors[col]) end end
  21. function notnil(vari) if vari == nil then return false else return true end end
  22.  
  23. -- pre-functions
  24.  
  25.  
  26. -- pre-values --
  27.  
  28. w,h = term.getSize() -- gets size of computer (entire computer, not the window api)
  29. local running = true
  30. term.setCursorBlink(true)
  31.  
  32. -- pre-values --
  33.  
  34.  
  35. -- main functions --
  36.  
  37. local function _save(fil,info)
  38.     local file_info = {}
  39.     local file = fs.open(fil, "w")
  40.     local cnt = 0
  41.     for i=1, #info do
  42.         cnt = cnt + 1
  43.         file.writeLine(info[i])
  44.         if cnt == 200 then
  45.             sleep(.000001)
  46.             cnt = 0
  47.         end
  48.     end file.close() return true
  49. end
  50.  
  51. local function _load(fil)
  52.     local file_info = {}
  53.     local file = fs.open(fil, "r")
  54.     local tmp = ""
  55.     local cnt = 0
  56.     repeat
  57.         cnt = cnt + 1
  58.         tmp = file.readLine()
  59.         if notnil(tmp) then
  60.             table.insert(file_info, tmp)
  61.         end
  62.         if cnt == 200 then
  63.             sleep(.000001)
  64.             cnt = 0
  65.         end
  66.     until not notnil(tmp) file.close() return file_info
  67. end
  68.  
  69. local old = term.current()
  70. local wind = window.create(term.current(), 1,1,w,h-2)
  71. local first_display = true
  72.  
  73. local function console(text,warn)
  74.     local oldTerm = term.redirect(wind)
  75.     if first_display then first_display = false print("Console v.1.0-ED-2020") print("Created by Just Does Games") end
  76.    
  77.     if notnil(warn) then if warn then setText("orange") else setText("red") end else setText("gray") end
  78.     print("") if not notnil(console_display) then write("[Console]") end setText("white")
  79.     local toggle = 0
  80.     for i=1, string.len(text) do
  81.         write(string.sub(text,i,i))
  82.         if string.sub(text,i,i) == ":" then
  83.             setText("yellow")
  84.         end
  85.         if toggle == 10 then
  86.             toggle = 0
  87.             if not notnil(instant) then sleep(.000000001) end
  88.         else
  89.             toggle = toggle+1
  90.         end
  91.     end
  92.     local newTerm = term.redirect(old)
  93.     cp(1,h-1) term.clearLine() cp(1,h-1) setText("white") write("> ") term.setCursorBlink(true) if not notnil(instant) then sleep(.035) end
  94. end
  95.  
  96. local function listAll(_path, _files)
  97.     local path = _path or ""
  98.     local files = _files or {}
  99.     local loop = 0
  100.     --if #path > 1 then table.insert(files, path) end
  101.     for _, file in ipairs(fs.list(path)) do
  102.         local path = fs.combine(path, file)
  103.         if fs.isDir(path) then
  104.             listAll(path, files)
  105.         else
  106.             table.insert(files, path)
  107.         end
  108.         loop = loop + 1
  109.         if loop == 1000 then
  110.             sleep(.1)
  111.             loop = 0
  112.         end
  113.     end
  114.     for i=1, #files do
  115.         if fs.isDir(files[i]) then table.remove(files,i) end
  116.     end
  117.     return files
  118. end
  119.  
  120. function encrypt(location, password)
  121.     if type(password) ~= "string" then return error("password was not a string. got "..type(password)) end
  122.     if type(location) ~= "string" then return error("location was not a string. got "..type(location)) end
  123.     if not fs.exists(location) then return error("location does not exists. got "..location) end
  124.    
  125.     console("Running with password: "..password)
  126.     console("Running with path: "..location)
  127.     console("Creating coded password from password...")
  128.     local pass = {}
  129.     for i=1, string.len(password) do
  130.         table.insert(pass, string.byte(string.sub(password,i,i)))
  131.     end
  132.     console("Password Decoded!")
  133.    
  134.     console("Loading file(s)...")
  135.    
  136.     file_info = {}
  137.     local full_list = {}
  138.     if fs.isDir(location) then
  139.         full_list = listAll(location)
  140.         for i=1, #full_list do
  141.             table.insert(file_info, _load(full_list[i]))
  142.         end
  143.     else
  144.         table.insert(full_list, location)
  145.         table.insert(file_info, _load(location))
  146.     end
  147.    
  148.     console("Total amount of files found: "..#full_list)
  149.     console("Converting to code...")
  150.    
  151.     local byte_file_info = {}
  152.     for i=1, #file_info do
  153.         local loop = 0
  154.         byte_file_info[i] = {}
  155.         for ii=1, #file_info[i] do
  156.             if string.len(file_info[i][ii]) == 0 then file_info[i][ii] = " " end
  157.             --console("DEBUG: "..#file_info[i][ii])
  158.             byte_file_info[i][ii] = {}
  159.             for iii=1, string.len(file_info[i][ii]) do
  160.                 table.insert(byte_file_info[i][ii], string.byte(string.sub(file_info[i][ii],iii,iii)))
  161.             end
  162.             if loop == 1000 then
  163.                 loop = 0
  164.                 sleep(.1)
  165.             else
  166.                 loop = loop + 1
  167.             end
  168.         end
  169.         console("Loaded "..i.." out of "..#file_info..": "..math.floor((i/#file_info)*100).."%")
  170.     end
  171.     --error(#byte_file_info[1]) -- should return # of lines of this program
  172.     console(#byte_file_info.." File(s) converted Successfully!", true)
  173.     console("Encrypting "..#byte_file_info.." file(s)...")
  174.    
  175.     for i=1, #byte_file_info do -- repeats only twice
  176.         local loop = 1
  177.         local d_loop = 0
  178.         for ii=1, #byte_file_info[i] do -- repeats 300+ times
  179.             for iii=1, #byte_file_info[i][ii] do
  180.                 byte_file_info[i][ii][iii] = byte_file_info[i][ii][iii]+pass[loop]
  181.                 if byte_file_info[i][ii][iii] > 255 then
  182.                     byte_file_info[i][ii][iii] = byte_file_info[i][ii][iii]-255
  183.                 end
  184.                 loop = loop + 1
  185.                 if loop > #pass then
  186.                     loop = 1
  187.                 end
  188.             end
  189.             if d_loop == 1000 then
  190.                 d_loop = 0
  191.                 console(fs.getName(full_list[i])..": "..ii.."/"..#byte_file_info[i].."  "..math.floor((ii/#byte_file_info[i])*10).."%")
  192.             else
  193.                 d_loop = d_loop + 1
  194.             end
  195.             --console("Debug: "..i.."/"..ii)
  196.         end
  197.         console("Encrypted "..i.." out of "..#byte_file_info.." : "..math.floor((i/#byte_file_info)*100).."%") sleep(.1)
  198.     end
  199.    
  200.     console("Encryption Done!",true)
  201.     console("Saving file(s)...",true)
  202.     --error(#byte_file_info[1])
  203.     for i=1, #byte_file_info do
  204.         local file = fs.open(string.gsub(location,"_decry","").."_encry/"..string.gsub(full_list[i],location,""), "w")
  205.         if notnil(file) then
  206.             for ii=1, #byte_file_info[i] do
  207.                 for iii=1, #byte_file_info[i][ii] do
  208.                     file.write(string.char(byte_file_info[i][ii][iii]))
  209.                 end
  210.                 file.writeLine("")
  211.             end
  212.             file.close()
  213.             console("Saving: "..i.."/"..#byte_file_info.." "..math.floor((i/#byte_file_info)*100).."%")
  214.         else
  215.             console("Error saving to file, this can happen for many reasons.",false)
  216.         end
  217.     end
  218.     console("Save Successfull! Done.",true)
  219.     console("Thank you. Made by Just Does Games!") sleep(1)
  220.     return true
  221. end
  222.  
  223. function decrypt(location, password)
  224.     if type(password) ~= "string" then return error("password was not a string. got "..type(password)) end
  225.     if type(location) ~= "string" then return error("location was not a string. got "..type(location)) end
  226.     if not fs.exists(location) then return error("location does not exists. got "..location) end
  227.    
  228.     console("Running with password: "..password)
  229.     console("Running with path: "..location)
  230.     console("Creating coded password from password...")
  231.     local pass = {}
  232.     for i=1, string.len(password) do
  233.         table.insert(pass, string.byte(string.sub(password,i,i)))
  234.     end
  235.     console("Password Decoded!")
  236.    
  237.     console("Loading file(s)...")
  238.    
  239.     file_info = {}
  240.     local full_list = {}
  241.     if fs.isDir(location) then
  242.         full_list = listAll(location)
  243.         for i=1, #full_list do
  244.             table.insert(file_info, _load(full_list[i]))
  245.         end
  246.     else
  247.         table.insert(full_list, location)
  248.         table.insert(file_info, _load(location))
  249.     end
  250.    
  251.     console("Total amount of files found: "..#full_list)
  252.     console("Converting to code...")
  253.    
  254.     local byte_file_info = {}
  255.     for i=1, #file_info do
  256.         local loop = 0
  257.         byte_file_info[i] = {}
  258.         for ii=1, #file_info[i] do
  259.             if string.len(file_info[i][ii]) == 0 then file_info[i][ii] = " " end
  260.             --console("DEBUG: "..#file_info[i][ii])
  261.             byte_file_info[i][ii] = {}
  262.             for iii=1, string.len(file_info[i][ii]) do
  263.                 table.insert(byte_file_info[i][ii], string.byte(string.sub(file_info[i][ii],iii,iii)))
  264.             end
  265.             if loop == 1000 then
  266.                 loop = 0
  267.                 sleep(.1)
  268.             else
  269.                 loop = loop + 1
  270.             end
  271.         end
  272.         console("Loaded "..i.." out of "..#file_info..": "..math.floor((i/#file_info)*100).."%")
  273.     end
  274.     --error(#byte_file_info[1]) -- should return # of lines of this program
  275.     console(#byte_file_info.." File(s) converted Successfully!", true)
  276.     console("Attempting to decrypt "..#byte_file_info.." file(s)...")
  277.    
  278.     for i=1, #byte_file_info do -- repeats only twice
  279.         local loop = 1
  280.         local d_loop = 0
  281.         for ii=1, #byte_file_info[i] do -- repeats 300+ times
  282.             for iii=1, #byte_file_info[i][ii] do
  283.                 byte_file_info[i][ii][iii] = byte_file_info[i][ii][iii]-pass[loop]
  284.                 if byte_file_info[i][ii][iii] < 1 then
  285.                     byte_file_info[i][ii][iii] = byte_file_info[i][ii][iii]+255
  286.                 end
  287.                 loop = loop + 1
  288.                 if loop > #pass then
  289.                     loop = 1
  290.                 end
  291.             end
  292.             if d_loop == 1000 then
  293.                 d_loop = 0
  294.                 console(fs.getName(full_list[i])..": "..ii.."/"..#byte_file_info[i].."  "..math.floor((ii/#byte_file_info[i])*10).."%")
  295.             else
  296.                 d_loop = d_loop + 1
  297.             end
  298.             --console("Debug: "..i.."/"..ii)
  299.         end
  300.         console("Decrypted "..i.." out of "..#byte_file_info.." : "..math.floor((i/#byte_file_info)*100).."%") sleep(.1)
  301.     end
  302.    
  303.     console("Decryption Done!",true)
  304.     console("Saving file(s)...",true)
  305.     --error(#byte_file_info[1])
  306.     for i=1, #byte_file_info do
  307.         local file = fs.open(string.gsub(location,"_encry","").."_decry/"..string.gsub(full_list[i],location,""), "w")
  308.         if notnil(file) then
  309.             for ii=1, #byte_file_info[i] do
  310.                 for iii=1, #byte_file_info[i][ii] do
  311.                     file.write(string.char(byte_file_info[i][ii][iii]))
  312.                 end
  313.                 file.writeLine("")
  314.             end
  315.             file.close()
  316.             console("Saving: "..i.."/"..#byte_file_info.." "..math.floor((i/#byte_file_info)*100).."%")
  317.         else
  318.             console("Error saving to file, this can happen for many reasons.",false)
  319.         end
  320.     end
  321.     console("Save Finished! Done.",true)
  322.     console("Thank you. Made by Just Does Games!",true) sleep(1)
  323.     return true
  324. end
  325.  
  326. -- main functions --
  327.  
  328.  
  329.  
  330. -- Separate functions from Encryption --
  331.  
  332. local function engine()
  333.     cp(1,1) setText("white") setBack("black") clr()
  334.     --encrypt("WHOLE_COMPUTER_COPY","Password")
  335.     --decrypt("startup_encry.lua","Password")
  336.     console("Welcome! type 'help' to display commands.")
  337.     local commands = {
  338.        
  339.         help = function()
  340.             console("List of commands")
  341.             console(" - 'encrypt' -> encrypt a file")
  342.             console(" - 'decrypt' -> decrypt a file")
  343.             console(" - 'exit' -> exit the program")
  344.            
  345.         end,
  346.        
  347.         encrypt = function()
  348.             console("Encryption",true)
  349.             console("Enter directory or file name...")
  350.             file = read()
  351.             if fs.exists(file) then
  352.                 if fs.isDir(file) then
  353.                     console("Directory found!",true)
  354.                 else
  355.                     console("File found!",true)
  356.                 end
  357.                 console("Enter encryption password...",true)
  358.                 pass = read()
  359.                 if string.len(pass) < 1 then
  360.                     console("Encryption password has to be something!",false)
  361.                 else
  362.                     console("Confirm password: "..pass)
  363.                     console("Type 'yes' to confirm...",true)
  364.                     conf = read()
  365.                     if conf == "yes" then
  366.                         console("Encryption has started!") sleep(1)
  367.                         encrypt(file, pass)
  368.                     else
  369.                         console("Failed to confirm. Exiting...",false)
  370.                     end
  371.                 end
  372.             else
  373.                 console("File or directory does not exists",false)
  374.             end
  375.         end,
  376.        
  377.         decrypt = function()
  378.             console("Decryption",true)
  379.             console("Enter directory or file name...")
  380.             file = read()
  381.             if fs.exists(file) then
  382.                 if fs.isDir(file) then
  383.                     console("Directory found!",true)
  384.                 else
  385.                     console("File found!",true)
  386.                 end
  387.                 console("Enter decryption password...",true)
  388.                 pass = read()
  389.                 if string.len(pass) < 1 then
  390.                     console("Decryption password has to be something!",false)
  391.                 else
  392.                     console("Confirm password: "..pass)
  393.                     console("Type 'yes' to confirm...",true)
  394.                     conf = read()
  395.                     if conf == "yes" then
  396.                         console("Decryption has started!") sleep(1)
  397.                         decrypt(file, pass)
  398.                     else
  399.                         console("Failed to confirm. Exiting...",false)
  400.                     end
  401.                 end
  402.             else
  403.                 console("File or directory does not exists",false)
  404.             end
  405.         end,
  406.        
  407.         exit = function()
  408.             console("Exiting...") running = false
  409.         end
  410.        
  411.     }
  412.    
  413.     while running do
  414.         local res = string.lower(read())
  415.         if notnil(commands[res]) then
  416.             commands[res]()
  417.         else
  418.             console("Unknown command? do 'help' for help.")
  419.         end
  420.     end
  421. end
  422.  
  423. engine()
  424. print("")
  425. -- Separate functions from Encryption --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement