Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Scrambler by Just Does Games --
- function clr() term.clear() end
- function cp(x,y) term.setCursorPos(x,y) end
- function encry(ffile, key)
- local file_1 = fs.open(ffile, "r")
- local v_file_lines = 0 -- variable used to store # of lines a file has
- local v_file_letters = 0 -- variable used to store # of letters a file has
- local v_file_current_line_text = "" -- variable used to store line read from the file
- local v_file_current_letter_text -- variable used to store letter read from the file
- local v_file_current_line = 0 -- variable used to show what line the encryptor is on
- local v_file_current_letter = 0 -- variable used to show what letter the encryptor is on
- local v_file_line_table = {} -- table used for converting letters into table later on
- local v_file_line_string = "" -- string used for converting letters into string and copying to second file
- local v_sleep_delay = 6000 -- variable used to tell the system how many times to run the code before using a sleep timer
- local v_sleep_time = .44 -- variable used to show how long to pause the system to avoid 'too long' error
- local v_sleep_variable = 0 -- variable to remember how many times the program has run to use a delay
- local v_key_current = 1 -- variable used to keep track of what key it is on
- local v_tmp = {} -- variable used for anything not important later on in the code and only use temp
- repeat -- finds how many lines and letters there are in the file for later use
- i = file_1.readLine()
- if i ~= nil then
- v_file_lines = v_file_lines + 1
- v_file_letters = v_file_letters + string.len(i)
- end
- until i == nil
- file_1.close()
- local file_1 = fs.open(ffile, "r") -- Reopen file to set selector on 1
- print("File has "..v_file_lines.." lines and "..v_file_letters.." letters") sleep(1)
- v_tmp = {} -- used for converting key into table (string to array)
- for i=1, string.len(key) do
- v_tmp[#v_tmp+1] = string.sub(key,i,i)
- end
- key = v_tmp -- sets key = table
- v_tmp = nil
- if fs.exists(ffile.."_ENCRY") then fs.delete(ffile.."_ENCRY") end
- local file_2 = fs.open(ffile.."_ENCRY", "w") -- opens second file to save encrypted text to
- local running = true
- while running do
- 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
- local v_file_current_line_text = file_1.readLine() -- reads the next line from the file
- if v_file_current_line_text ~= nil then -- checks to see if there is another line, if not closes the program
- for i=1, string.len(v_file_current_line_text) do -- repeats code below for each letter in the string / line
- 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
- v_file_current_letter_text = v_file_current_letter_text + key[v_key_current] -- adds key value of current key
- 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
- 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
- v_file_current_letter_text = v_file_current_letter_text - 255 -- converts it to a number lower than 255
- end
- 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
- end
- v_file_line_string = "" -- resets this variable for new string
- for i=1, #v_file_line_table do
- v_file_line_string = v_file_line_string..v_file_line_table[i] -- creates new string using letters generated
- end
- v_file_line_table = {}
- file_2.writeLine(v_file_line_string) -- writes to second file
- else
- file_1.close()
- file_2.close()
- running = false
- end
- end
- end
- function decry(ffile, key)
- local file_1 = fs.open(ffile, "r")
- local v_file_lines = 0 -- variable used to store # of lines a file has
- local v_file_letters = 0 -- variable used to store # of letters a file has
- local v_file_current_line_text = "" -- variable used to store line read from the file
- local v_file_current_letter_text -- variable used to store letter read from the file
- local v_file_current_line = 0 -- variable used to show what line the encryptor is on
- local v_file_current_letter = 0 -- variable used to show what letter the encryptor is on
- local v_file_line_table = {} -- table used for converting letters into table later on
- local v_file_line_string = "" -- string used for converting letters into string and copying to second file
- local v_sleep_delay = 6000 -- variable used to tell the system how many times to run the code before using a sleep timer
- local v_sleep_time = .44 -- variable used to show how long to pause the system to avoid 'too long' error
- local v_sleep_variable = 0 -- variable to remember how many times the program has run to use a delay
- local v_key_current = 1 -- variable used to keep track of what key it is on
- local v_tmp = {} -- variable used for anything not important later on in the code and only use temp
- repeat -- finds how many lines and letters there are in the file for later use
- i = file_1.readLine()
- if i ~= nil then
- v_file_lines = v_file_lines + 1
- v_file_letters = v_file_letters + string.len(i)
- end
- until i == nil
- file_1.close()
- local file_1 = fs.open(ffile, "r") -- Reopen file to set selector on 1
- print("File has "..v_file_lines.." lines and "..v_file_letters.." letters") sleep(1)
- v_tmp = {} -- used for converting key into table (string to array)
- for i=1, string.len(key) do
- v_tmp[#v_tmp+1] = string.sub(key,i,i)
- end
- key = v_tmp -- sets key = table
- v_tmp = nil
- if fs.exists(ffile.."_DECRY") then fs.delete(ffile.."_DECRY") end
- local file_2 = fs.open(ffile.."_DECRY", "w") -- opens second file to save decrypted text to
- local running = true
- while running do
- 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
- local v_file_current_line_text = file_1.readLine() -- reads the next line from the file
- if v_file_current_line_text ~= nil then -- checks to see if there is another line, if not closes the program
- for i=1, string.len(v_file_current_line_text) do -- repeats code below for each letter in the string / line
- 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
- v_file_current_letter_text = v_file_current_letter_text - key[v_key_current] -- adds key value of current key
- 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
- 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
- v_file_current_letter_text = v_file_current_letter_text + 255 -- converts it to a number lower than 255
- end
- 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
- end
- v_file_line_string = "" -- resets this variable for new string
- for i=1, #v_file_line_table do
- v_file_line_string = v_file_line_string..v_file_line_table[i] -- creates new string using letters generated
- end
- v_file_line_table = {}
- file_2.writeLine(v_file_line_string) -- writes to second file
- else
- file_1.close()
- file_2.close()
- running = false
- end
- end
- end
- function generateKey()
- return math.random(10000000, 99999999)
- end
- m_main = {"Encrypt", "Decrypt", "Exit"}
- m_selected = m_main
- function run(menu)
- local running = true
- local selected = 1
- clr()
- while running do
- cp(1,1)
- for i=1, #menu do
- if i == selected then
- print("> "..menu[i].." <")
- else
- print(" "..menu[i].." ")
- end
- end
- a,i = os.pullEvent("key")
- if i == keys.w or i == keys.up then
- if selected ~= 1 then selected = selected - 1 end
- elseif i == keys.s or i == keys.down then
- if selected ~= #menu then selected = selected + 1 end
- elseif i == keys.e or i == keys.enter then
- running = false
- end
- end
- return selected
- end
- function runtime()
- local running = true
- while running do
- local selected = run(m_selected)
- if m_selected == m_main then
- if selected == #m_selected then
- running = false
- elseif selected == 1 then
- 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
- elseif selected == 2 then
- 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
- end
- end
- end
- clr() cp(1,1) print("Program created by Just Does Games")
- end
- runtime()
Advertisement
Add Comment
Please, Sign In to add comment