Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Message Stuff
- local alphabet = {}
- local alphabetCount = {}
- local cipher = {}
- local stopChars = {["."] = true, [","] = true, ["!"] = true, ["?"] = true, ["("] = true,
- [")"] = true, ["'"] = true, ["\""] = true, [";"] = true, [" "] = true, [""] = true}
- local message = ""
- local emessage = ""
- --Interface Stuff
- local w,h = term.getSize()
- local listscroll,msgscroll = 0,0
- local lines = {}
- local playerCipher = {}
- local selChar = ""
- local decrypting = true
- function split(str, pattern)
- local t = { }
- local fpat = "(.-)" .. pattern
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function openMessage(path)
- local file = nil
- if string.sub(path, 1, 1) == "/" then
- file = fs.open(path, "r")
- else
- file = fs.open(shell.resolve(".").."/"..path,
- "r")
- end
- if not file then error(path.." not found") end
- local submsg = file:readLine()
- while submsg do
- message = message..submsg
- submsg = file:readLine()
- end
- file:close()
- for i=1,#message do
- local char = string.upper(string.sub(message, i, i))
- local unique = true
- if not char or char == "" then unique = false end
- if stopChars[char] then
- unique = false
- end
- for a=1,#alphabet do
- if alphabet[a] == char then
- unique = false
- break
- end
- end
- if unique then
- table.insert(alphabet, char)
- alphabetCount[char] = 0
- end
- end
- table.sort(alphabet)
- end
- function createCipher()
- local locA = { }
- for i=1,#alphabet do
- locA[i] = alphabet[i]
- end
- while #locA > 0 do
- local index = math.random(1,#locA)
- --this is to ensure every letter in the cipher is different to the original
- if locA[index] ~= alphabet[#cipher+1] then
- table.insert(cipher, locA[index])
- table.remove(locA, index)
- end
- end
- end
- function encryptMessage()
- emessage = ""
- for i=1,#message do
- local msgChar = string.upper(string.sub(message,i,i))
- local index = -1
- for a=1,#alphabet do
- if alphabet[a] == msgChar then
- index = a
- break
- end
- end
- if index == -1 then
- emessage = emessage..msgChar
- else
- emessage = emessage..cipher[index]
- alphabetCount[cipher[index]] = alphabetCount[cipher[index]] + 1
- end
- end
- end
- function printDebug()
- print(message.."\n")
- local fullA = ""
- for i=1,#alphabet-1 do
- fullA = fullA..alphabet[i]..","
- end
- print("["..fullA..alphabet[#alphabet].."]")
- fullA = ""
- for i=1,#cipher-1 do
- fullA = fullA..cipher[i]..","
- end
- print("["..fullA..cipher[#cipher].."]")
- print("\n"..emessage.."\n")
- local cipherUnique = true
- for i=1,#cipher do
- if cipher[i] == alphabet[i] then
- print("Duplicate at "..i..": "..cipher[i].."/"..alphabet[i])
- cipherUnique = false
- end
- end
- if cipherUnique then print("Cipher unique")
- else print("Cipher not unique") end
- end
- function prepareDecryptInterface()
- local words = split(emessage, " ")
- local lineIndex = 1
- lines[lineIndex] = {}
- while #words > 0 do
- if #lines[lineIndex] + #words[1] > w - 11 then
- lineIndex = lineIndex + 1
- lines[lineIndex] = {}
- end
- for i=1,#words[1] do
- table.insert(lines[lineIndex], string.sub(words[1], i, i))
- end
- table.insert(lines[lineIndex], " ")
- table.remove(words, 1)
- end
- end
- function drawDecryptInterface()
- term.setBackgroundColour(colours.black)
- term.clear()
- --Header
- term.setCursorPos(1,1)
- term.setBackgroundColour(colours.lime)
- term.setTextColour(colours.black)
- local msg = "Message Decrypter"
- term.write(string.rep(" ", w/2 - #msg/2)..msg..string.rep(" ", w/2 - #msg/2))
- for i=2,h do
- term.setCursorPos(8, i)
- if i < 5 and listscroll > 0 then
- term.write("^")
- elseif i > h-3 and listscroll < #alphabet - h - 1 then
- term.write("V")
- else
- term.write(" ")
- end
- end
- --Sidebar
- term.setBackgroundColour(colours.black)
- for i=1,h-1 do
- local index = i + listscroll
- term.setCursorPos(1, i+1)
- local char = " "
- if playerCipher[alphabet[index]] then char = playerCipher[alphabet[index]] end
- if selChar == alphabet[index] then
- term.setBackgroundColour(colours.grey)
- else
- term.setBackgroundColour(colours.black)
- end
- term.setTextColour(colours.orange)
- term.write(char)
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.lime)
- term.write(" "..alphabet[index].."-"..alphabetCount[alphabet[index]])
- end
- --Lines
- for i=1,#lines do
- for c=1,#lines[i] do
- term.setCursorPos(10 + c - 1, i * 3)
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.lime)
- term.write(lines[i][c])
- term.setCursorPos(10 + c - 1, i * 3 + 1)
- if selChar == lines[i][c] then
- term.setBackgroundColour(colours.grey)
- end
- term.setTextColour(colours.orange)
- if playerCipher[lines[i][c]] then
- term.write(playerCipher[lines[i][c]])
- elseif stopChars[lines[i][c]] then
- term.write(lines[i][c])
- else term.write(" ") end
- end
- end
- end
- function handleInput()
- local id,key,x,y = os.pullEvent()
- if id == "mouse_scroll" then
- if x <=8 then
- scroll = scroll + key * 3
- if scroll < 0 then scroll = 0
- elseif scroll > #alphabet-h then scroll = #alphabet-h
- end
- else
- end
- elseif id == "mouse_click" then
- if y % 3 == 1 and y > 3 and y <= #lines*3+1 and x >= 10 and
- x < #lines[math.floor(y/3)]+10 then
- selChar = lines[math.floor(y/3)][x-9]
- if stopChars[selChar] then
- selChar = ""
- end
- elseif x < 8 and y > 1 then
- selChar = alphabet[y-1+scroll]
- elseif x == 8 and y < 4 and y > 1 then
- scroll = scroll - 3
- if scroll < 0 then scroll = 0 end
- elseif x == 8 and y > h-3 then
- scroll = scroll + 3
- if scroll > #alphabet-h then scroll = #alphabet-h end
- else
- selChar = ""
- end
- elseif id == "char" then
- if selChar ~= "" then
- for k,v in pairs(playerCipher) do
- if v == string.upper(key) then
- playerCipher[k] = " "
- end
- end
- playerCipher[selChar] = string.upper(key)
- end
- elseif id == "key" and key == keys.enter then
- decrypting = false
- end
- end
- local tArgs = {...}
- if #tArgs < 1 then
- error("Usage : encrypt <path>")
- end
- openMessage(tArgs[1])
- createCipher()
- encryptMessage()
- prepareDecryptInterface()
- while decrypting do
- drawDecryptInterface()
- handleInput()
- end
Advertisement
Add Comment
Please, Sign In to add comment