JustDoesGames

Encryption

Jul 3rd, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.18 KB | None | 0 0
  1.  
  2. -- Scrambler by Just Does Games --
  3.  
  4.  
  5.  
  6. function clr() term.clear() end
  7. function cp(x,y) term.setCursorPos(x,y) end
  8.  
  9.  
  10. function encry(ffile, key)
  11.     local file_1 = fs.open(ffile, "r")
  12.     local v_file_lines = 0 -- variable used to store # of lines a file has
  13.     local v_file_letters = 0 -- variable used to store # of letters a file has
  14.     local v_file_current_line_text = "" -- variable used to store line read from the file
  15.     local v_file_current_letter_text -- variable used to store letter read from the file
  16.     local v_file_current_line = 0 -- variable used to show what line the encryptor is on
  17.     local v_file_current_letter = 0 -- variable used to show what letter the encryptor is on
  18.     local v_file_line_table = {} -- table used for converting letters into table later on
  19.     local v_file_line_string = "" -- string used for converting letters into string and copying to second file
  20.     local v_sleep_delay = 6000 -- variable used to tell the system how many times to run the code before using a sleep timer
  21.     local v_sleep_time = .44 -- variable used to show how long to pause the system to avoid 'too long' error
  22.     local v_sleep_variable = 0 -- variable to remember how many times the program has run to use a delay
  23.     local v_key_current = 1 -- variable used to keep track of what key it is on
  24.     local v_tmp = {} -- variable used for anything not important later on in the code and only use temp
  25.    
  26.     repeat -- finds how many lines and letters there are in the file for later use
  27.         i = file_1.readLine()
  28.         if i ~= nil then
  29.             v_file_lines = v_file_lines + 1
  30.             v_file_letters = v_file_letters + string.len(i)
  31.         end
  32.     until i == nil
  33.     file_1.close()
  34.     local file_1 = fs.open(ffile, "r") -- Reopen file to set selector on 1
  35.    
  36.     print("File has "..v_file_lines.." lines and "..v_file_letters.." letters") sleep(1)
  37.    
  38.     v_tmp = {} -- used for converting key into table (string to array)
  39.     for i=1, string.len(key) do
  40.         v_tmp[#v_tmp+1] = string.sub(key,i,i)
  41.     end
  42.     key = v_tmp -- sets key = table
  43.     v_tmp = nil
  44.    
  45.     if fs.exists(ffile.."_ENCRY") then fs.delete(ffile.."_ENCRY") end
  46.     local file_2 = fs.open(ffile.."_ENCRY", "w") -- opens second file to save encrypted text to
  47.    
  48.     local running = true
  49.     while running do
  50.         if v_sleep_variable == v_sleep_delay then sleep(v_sleep_time) v_sleep_variable = 0 else v_sleep_variable = v_sleep_variable + 1 end
  51.        
  52.         local v_file_current_line_text = file_1.readLine() -- reads the next line from the file
  53.         if v_file_current_line_text ~= nil then -- checks to see if there is another line, if not closes the program
  54.             for i=1, string.len(v_file_current_line_text) do -- repeats code below for each letter in the string / line
  55.                 v_file_current_letter_text = string.byte(string.sub(v_file_current_line_text,i,i)) -- separates letter from line and converts it into a usable number
  56.                 v_file_current_letter_text = v_file_current_letter_text + key[v_key_current] -- adds key value of current key
  57.                 if v_key_current == #key then v_key_current = 1 else v_key_current = v_key_current + 1 end -- moves the selected key value by 1 each time
  58.                 if v_file_current_letter_text > 255 then -- 255 is the max number of strings in computercraft so we need this number to be lower than this
  59.                     v_file_current_letter_text = v_file_current_letter_text - 255 -- converts it to a number lower than 255
  60.                 end
  61.                 v_file_line_table[#v_file_line_table+1] = string.char(v_file_current_letter_text) -- places letter into table and converts it into a letter for later use
  62.             end
  63.             v_file_line_string = "" -- resets this variable for new string
  64.             for i=1, #v_file_line_table do
  65.                 v_file_line_string = v_file_line_string..v_file_line_table[i] -- creates new string using letters generated
  66.             end
  67.             v_file_line_table = {}
  68.             file_2.writeLine(v_file_line_string) -- writes to second file
  69.         else
  70.             file_1.close()
  71.             file_2.close()
  72.             running = false
  73.         end
  74.     end
  75. end
  76.  
  77. function decry(ffile, key)
  78.     local file_1 = fs.open(ffile, "r")
  79.     local v_file_lines = 0 -- variable used to store # of lines a file has
  80.     local v_file_letters = 0 -- variable used to store # of letters a file has
  81.     local v_file_current_line_text = "" -- variable used to store line read from the file
  82.     local v_file_current_letter_text -- variable used to store letter read from the file
  83.     local v_file_current_line = 0 -- variable used to show what line the encryptor is on
  84.     local v_file_current_letter = 0 -- variable used to show what letter the encryptor is on
  85.     local v_file_line_table = {} -- table used for converting letters into table later on
  86.     local v_file_line_string = "" -- string used for converting letters into string and copying to second file
  87.     local v_sleep_delay = 6000 -- variable used to tell the system how many times to run the code before using a sleep timer
  88.     local v_sleep_time = .44 -- variable used to show how long to pause the system to avoid 'too long' error
  89.     local v_sleep_variable = 0 -- variable to remember how many times the program has run to use a delay
  90.     local v_key_current = 1 -- variable used to keep track of what key it is on
  91.     local v_tmp = {} -- variable used for anything not important later on in the code and only use temp
  92.    
  93.     repeat -- finds how many lines and letters there are in the file for later use
  94.         i = file_1.readLine()
  95.         if i ~= nil then
  96.             v_file_lines = v_file_lines + 1
  97.             v_file_letters = v_file_letters + string.len(i)
  98.         end
  99.     until i == nil
  100.     file_1.close()
  101.     local file_1 = fs.open(ffile, "r") -- Reopen file to set selector on 1
  102.    
  103.     print("File has "..v_file_lines.." lines and "..v_file_letters.." letters") sleep(1)
  104.    
  105.     v_tmp = {} -- used for converting key into table (string to array)
  106.     for i=1, string.len(key) do
  107.         v_tmp[#v_tmp+1] = string.sub(key,i,i)
  108.     end
  109.     key = v_tmp -- sets key = table
  110.     v_tmp = nil
  111.    
  112.     if fs.exists(ffile.."_DECRY") then fs.delete(ffile.."_DECRY") end
  113.     local file_2 = fs.open(ffile.."_DECRY", "w") -- opens second file to save decrypted text to
  114.    
  115.     local running = true
  116.     while running do
  117.         if v_sleep_variable == v_sleep_delay then sleep(v_sleep_time) v_sleep_variable = 0 else v_sleep_variable = v_sleep_variable + 1 end
  118.        
  119.         local v_file_current_line_text = file_1.readLine() -- reads the next line from the file
  120.         if v_file_current_line_text ~= nil then -- checks to see if there is another line, if not closes the program
  121.             for i=1, string.len(v_file_current_line_text) do -- repeats code below for each letter in the string / line
  122.                 v_file_current_letter_text = string.byte(string.sub(v_file_current_line_text,i,i)) -- separates letter from line and converts it into a usable number
  123.                 v_file_current_letter_text = v_file_current_letter_text - key[v_key_current] -- adds key value of current key
  124.                 if v_key_current == #key then v_key_current = 1 else v_key_current = v_key_current + 1 end -- moves the selected key value by 1 each time
  125.                 if v_file_current_letter_text < 1 then -- 255 is the max number of strings in computercraft so we need this number to be lower than this
  126.                     v_file_current_letter_text = v_file_current_letter_text + 255 -- converts it to a number lower than 255
  127.                 end
  128.                 v_file_line_table[#v_file_line_table+1] = string.char(v_file_current_letter_text) -- places letter into table and converts it into a letter for later use
  129.             end
  130.             v_file_line_string = "" -- resets this variable for new string
  131.             for i=1, #v_file_line_table do
  132.                 v_file_line_string = v_file_line_string..v_file_line_table[i] -- creates new string using letters generated
  133.             end
  134.             v_file_line_table = {}
  135.             file_2.writeLine(v_file_line_string) -- writes to second file
  136.         else
  137.             file_1.close()
  138.             file_2.close()
  139.             running = false
  140.         end
  141.     end
  142. end
  143.  
  144. function generateKey()
  145.     return math.random(10000000, 99999999)
  146. end
  147.  
  148. m_main = {"Encrypt", "Decrypt", "Exit"}
  149.  
  150. m_selected = m_main
  151.  
  152. function run(menu)
  153.     local running = true
  154.     local selected = 1
  155.     clr()
  156.     while running do
  157.         cp(1,1)
  158.         for i=1, #menu do
  159.             if i == selected then
  160.                 print("> "..menu[i].." <")
  161.             else
  162.                 print("  "..menu[i].."  ")
  163.             end
  164.         end
  165.         a,i = os.pullEvent("key")
  166.        
  167.         if i == keys.w or i == keys.up then
  168.             if selected ~= 1 then selected = selected - 1 end
  169.         elseif i == keys.s or i == keys.down then
  170.             if selected ~= #menu then selected = selected + 1 end
  171.         elseif i == keys.e or i == keys.enter then
  172.             running = false
  173.         end
  174.     end
  175.     return selected
  176. end
  177.  
  178. function runtime()
  179.     local running = true
  180.     while running do
  181.     local selected = run(m_selected)
  182.     if m_selected == m_main then
  183.         if selected == #m_selected then
  184.             running = false
  185.         elseif selected == 1 then
  186.             clr() cp(1,1) write("File name: ") name = read() if fs.exists(name) then write("Key: ") key = tonumber(read()) if type(key) == "number" then encry(name,key) print("Encrypted File with key.") sleep(1) print("Press any key to continue") os.pullEvent("key") end end
  187.         elseif selected == 2 then
  188.             clr() cp(1,1) write("File name: ") name = read() if fs.exists(name) then write("Key: ") key = tonumber(read()) if type(key) == "number" then decry(name,key) print("Decrypted File with key.") print("Decryption may not have worked due to an incorrect key. Keep this in mind.") sleep(1) print("Press any key to continue") os.pullEvent("key") end end
  189.         end
  190.     end
  191.     end
  192.     clr() cp(1,1) print("Program created by Just Does Games")
  193. end
  194.  
  195. runtime()
Advertisement
Add Comment
Please, Sign In to add comment