Guest User

cryptogame

a guest
Nov 23rd, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.50 KB | None | 0 0
  1. --Message Stuff
  2. local alphabet = {}
  3. local alphabetCount = {}
  4. local cipher = {}
  5. local stopChars = {["."] = true, [","] = true, ["!"] = true, ["?"] = true, ["("] = true,
  6.   [")"] = true, ["'"] = true, ["\""] = true, [";"] = true, [" "] = true, [""] = true}
  7. local message = ""
  8. local emessage = ""
  9.  
  10. --Interface Stuff
  11. local w,h = term.getSize()
  12. local listscroll,msgscroll = 0,0
  13. local lines = {}
  14. local playerCipher = {}
  15. local selChar = ""
  16. local decrypting = true
  17.  
  18. function split(str, pattern)
  19.   local t = { }
  20.   local fpat = "(.-)" .. pattern
  21.   local last_end = 1
  22.   local s, e, cap = str:find(fpat, 1)
  23.   while s do
  24.     if s ~= 1 or cap ~= "" then
  25.       table.insert(t,cap)
  26.     end
  27.     last_end = e+1
  28.     s, e, cap = str:find(fpat, last_end)
  29.   end
  30.   if last_end <= #str then
  31.     cap = str:sub(last_end)
  32.     table.insert(t, cap)
  33.   end
  34.   return t
  35. end
  36.  
  37. local function openMessage(path)
  38.   local file = nil
  39.   if string.sub(path, 1, 1) == "/" then
  40.     file = fs.open(path, "r")
  41.   else
  42.     file = fs.open(shell.resolve(".").."/"..path,
  43.       "r")
  44.   end
  45.   if not file then error(path.." not found") end
  46.  
  47.   local submsg = file:readLine()
  48.   while submsg do
  49.     message = message..submsg
  50.     submsg = file:readLine()
  51.   end
  52.   file:close()
  53.    
  54.   for i=1,#message do
  55.     local char = string.upper(string.sub(message, i, i))
  56.     local unique = true
  57.     if not char or char == "" then unique = false end
  58.     if stopChars[char] then
  59.         unique = false
  60.     end
  61.     for a=1,#alphabet do
  62.       if alphabet[a] == char then
  63.         unique = false
  64.         break
  65.       end
  66.     end
  67.     if unique then
  68.         table.insert(alphabet, char)
  69.         alphabetCount[char] = 0
  70.     end
  71.   end
  72.   table.sort(alphabet)
  73. end
  74.  
  75. function createCipher()
  76.   local locA = { }
  77.   for i=1,#alphabet do
  78.     locA[i] = alphabet[i]
  79.   end
  80.   while #locA > 0 do
  81.     local index = math.random(1,#locA)
  82.     --this is to ensure every letter in the cipher is different to the original
  83.     if locA[index] ~= alphabet[#cipher+1] then
  84.         table.insert(cipher, locA[index])
  85.         table.remove(locA, index)
  86.     end
  87.   end
  88. end
  89.  
  90. function encryptMessage()
  91.   emessage = ""
  92.   for i=1,#message do
  93.     local msgChar = string.upper(string.sub(message,i,i))
  94.     local index = -1
  95.     for a=1,#alphabet do
  96.       if alphabet[a] == msgChar then
  97.         index = a
  98.         break
  99.       end
  100.     end
  101.     if index == -1 then
  102.         emessage = emessage..msgChar
  103.     else
  104.         emessage = emessage..cipher[index]
  105.         alphabetCount[cipher[index]] = alphabetCount[cipher[index]] + 1
  106.     end
  107.   end
  108. end
  109.  
  110. function printDebug()
  111.     print(message.."\n")
  112.     local fullA = ""
  113.     for i=1,#alphabet-1 do
  114.         fullA = fullA..alphabet[i]..","
  115.     end
  116.     print("["..fullA..alphabet[#alphabet].."]")
  117.     fullA = ""
  118.     for i=1,#cipher-1 do
  119.         fullA = fullA..cipher[i]..","
  120.     end
  121.     print("["..fullA..cipher[#cipher].."]")
  122.     print("\n"..emessage.."\n")
  123.    
  124.     local cipherUnique = true
  125.     for i=1,#cipher do
  126.         if cipher[i] == alphabet[i] then
  127.             print("Duplicate at "..i..": "..cipher[i].."/"..alphabet[i])
  128.             cipherUnique = false
  129.         end
  130.     end
  131.     if cipherUnique then print("Cipher unique")
  132.     else print("Cipher not unique") end
  133. end
  134.  
  135. function prepareDecryptInterface()
  136.     local words = split(emessage, " ")
  137.     local lineIndex = 1
  138.     lines[lineIndex] = {}
  139.     while #words > 0 do
  140.         if #lines[lineIndex] + #words[1] >  w - 11 then
  141.             lineIndex = lineIndex + 1
  142.             lines[lineIndex] = {}
  143.         end
  144.         for i=1,#words[1] do
  145.             table.insert(lines[lineIndex], string.sub(words[1], i, i))
  146.         end
  147.         table.insert(lines[lineIndex], " ")
  148.         table.remove(words, 1)
  149.     end
  150. end
  151.  
  152. function drawDecryptInterface()
  153.     term.setBackgroundColour(colours.black)
  154.     term.clear()
  155.     --Header
  156.     term.setCursorPos(1,1)
  157.     term.setBackgroundColour(colours.lime)
  158.     term.setTextColour(colours.black)
  159.     local msg = "Message Decrypter"
  160.     term.write(string.rep(" ", w/2 - #msg/2)..msg..string.rep(" ", w/2 - #msg/2))
  161.     for i=2,h do
  162.         term.setCursorPos(8, i)
  163.         if i < 5 and listscroll > 0 then
  164.             term.write("^")
  165.         elseif i > h-3 and listscroll < #alphabet - h - 1 then
  166.             term.write("V")
  167.         else
  168.             term.write(" ")
  169.         end
  170.     end
  171.     --Sidebar
  172.     term.setBackgroundColour(colours.black)
  173.     for i=1,h-1 do
  174.         local index = i + listscroll
  175.         term.setCursorPos(1, i+1)
  176.         local char = " "
  177.         if playerCipher[alphabet[index]] then char = playerCipher[alphabet[index]] end
  178.         if selChar == alphabet[index] then
  179.             term.setBackgroundColour(colours.grey)
  180.         else
  181.             term.setBackgroundColour(colours.black)
  182.         end
  183.         term.setTextColour(colours.orange)
  184.         term.write(char)
  185.         term.setBackgroundColour(colours.black)
  186.         term.setTextColour(colours.lime)
  187.         term.write(" "..alphabet[index].."-"..alphabetCount[alphabet[index]])
  188.     end
  189.     --Lines
  190.     for i=1,#lines do
  191.         for c=1,#lines[i] do
  192.             term.setCursorPos(10 + c - 1, i * 3)
  193.             term.setBackgroundColour(colours.black)
  194.             term.setTextColour(colours.lime)
  195.             term.write(lines[i][c])
  196.             term.setCursorPos(10 + c - 1, i * 3 + 1)
  197.             if selChar == lines[i][c] then
  198.                 term.setBackgroundColour(colours.grey)
  199.             end
  200.             term.setTextColour(colours.orange)
  201.             if playerCipher[lines[i][c]] then
  202.                 term.write(playerCipher[lines[i][c]])
  203.             elseif stopChars[lines[i][c]] then
  204.                 term.write(lines[i][c])
  205.             else term.write(" ") end
  206.         end
  207.     end
  208. end
  209.  
  210. function handleInput()
  211.     local id,key,x,y = os.pullEvent()
  212.    
  213.     if id == "mouse_scroll" then
  214.         if x <=8 then
  215.             scroll = scroll + key * 3
  216.             if scroll < 0 then scroll = 0
  217.             elseif scroll > #alphabet-h then scroll = #alphabet-h
  218.             end
  219.         else
  220.            
  221.         end
  222.     elseif id == "mouse_click" then
  223.         if y % 3 == 1 and y > 3 and y <= #lines*3+1 and x >= 10 and
  224.                 x < #lines[math.floor(y/3)]+10 then
  225.             selChar = lines[math.floor(y/3)][x-9]
  226.             if stopChars[selChar] then
  227.                 selChar = ""
  228.             end
  229.         elseif x < 8 and y > 1 then
  230.             selChar = alphabet[y-1+scroll]
  231.         elseif x == 8 and y < 4 and y > 1 then
  232.             scroll = scroll - 3
  233.             if scroll < 0 then scroll = 0 end
  234.         elseif x == 8 and y > h-3 then
  235.             scroll = scroll + 3
  236.             if scroll > #alphabet-h then scroll = #alphabet-h end
  237.         else
  238.             selChar = ""
  239.         end
  240.     elseif id == "char" then
  241.         if selChar ~= "" then
  242.             for k,v in pairs(playerCipher) do
  243.                 if v == string.upper(key) then
  244.                     playerCipher[k] = " "
  245.                 end
  246.             end
  247.             playerCipher[selChar] = string.upper(key)
  248.         end
  249.     elseif id == "key" and key == keys.enter then
  250.         decrypting = false
  251.     end
  252. end
  253.  
  254. local tArgs = {...}
  255. if #tArgs < 1 then
  256.   error("Usage : encrypt <path>")
  257. end
  258.  
  259. openMessage(tArgs[1])
  260. createCipher()
  261. encryptMessage()
  262. prepareDecryptInterface()
  263. while decrypting do
  264.     drawDecryptInterface()
  265.     handleInput()
  266. end
Advertisement
Add Comment
Please, Sign In to add comment