Advertisement
Piorjade

Encrypted Chat for ComputerCraft

Aug 10th, 2017
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. --[[
  2.         Encrypted Chat for ComputerCraft
  3.  
  4.     This chat system is connection based and uses the Diffie-Hellman-Merkle key exchange and the AES Encryption API
  5.     by SquidDev to encrypt messages.
  6.  
  7.     It is very simple and probably dirty made but it works quite well :)
  8.  
  9.     ~Piorjade
  10.  
  11. ]]--
  12.  
  13. shell.run("wget https://git.io/aeslua aeslua")
  14. os.loadAPI("aeslua")
  15. fs.delete("aeslua")
  16. rednet.open("top")
  17. Y = 20752
  18. P = 2340971
  19. print(math.pow(10, 13))
  20. X = math.random(math.pow(10, 13))
  21.  
  22. print("your secret number is: "..X)
  23.  
  24. print("Connect or host? (c/h)")
  25. local answer = ""
  26. repeat
  27.     write("> ")
  28.     answer = read()
  29. until answer == "c" or answer == "h"
  30.  
  31. if answer == "c" then
  32.     print("")
  33.     write("Enter ID: ")
  34.     id = tonumber(read())
  35.     print("Waiting for "..id.." to respond....")
  36.     rednet.send(id, {"connect",math.pow(Y, X%P)}, "encrypted_chat")
  37.     local sender, msg, protocol
  38.     repeat
  39.         sender, msg, protocol = rednet.receive()
  40.         print(tostring(sender).." "..tostring(msg).." "..tostring(protocol))
  41.     until sender == id and protocol == "encrypted_chat" and msg[1] == "connect"
  42.     local sender_number = msg[2]
  43.     local sender_library = msg[3]
  44.     local encryption_number = math.pow(sender_number, X%P)
  45.    
  46.     print("You are now sending messages to "..id)
  47.     local window1 = window.create(term.current(), 1, 1, 51, 18)
  48.     window1.setBackgroundColor(colors.black)
  49.     window1.setTextColor(colors.yellow)
  50.     window1.clear()
  51.     local window2 = window.create(term.current(), 1, 19, 51, 1)
  52.     window2.setBackgroundColor(colors.gray)
  53.     window2.clear()
  54.    
  55.     local function receive()
  56.         while true do
  57.             local _, msg, prot = rednet.receive()
  58.             if id == _ and prot == "encrypted_chat" then
  59.                 local str = aeslua.decrypt(tostring(encryption_number), msg, aeslua.AES128, aeslua.CBCMODE, sender_library)
  60.                 term.redirect(window1)
  61.                 term.setCursorBlink(false)
  62.                 print(id.."> "..str)
  63.                 window2.restoreCursor()
  64.                 term.redirect(window2)
  65.                 term.setCursorBlink(true)
  66.             end
  67.         end
  68.     end
  69.    
  70.     local function send()
  71.         while true do
  72.             write("> ")
  73.             local omsg = read()
  74.             if omsg == "/exit" then break end
  75.             msg = aeslua.encrypt(tostring(encryption_number), omsg, aeslua.AES128, aeslua.CBCMODE, sender_library)
  76.             rednet.send(id, msg, "encrypted_chat")
  77.             term.clear()
  78.             term.setCursorPos(1,1)
  79.             term.redirect(window1)
  80.             print("You> "..omsg)
  81.             term.redirect(window2)
  82.         end
  83.     end
  84.    
  85.     local evt = {}
  86.     local c1 = coroutine.create(receive)
  87.     local c2 = coroutine.create(send)
  88.     while true do
  89.         coroutine.resume(c1, unpack(evt))
  90.         coroutine.resume(c2, unpack(evt))
  91.         evt = {os.pullEventRaw()}
  92.         if coroutine.status(c2) == "dead" then
  93.             rednet.send(id, aeslua.encrypt(tostring(encryption_number), "**exited**", aeslua.AES128, aeslua.CBCMODE, sender_library), "encrypted_chat")
  94.             break
  95.         end
  96.     end
  97.     print("Thanks for using the program!")
  98. else
  99.     local library = {}
  100.     for i = 1, 16 do library[i] = math.random(1, 255) end
  101.     print("")
  102.     write("Enter ID: ")
  103.     id = tonumber(read())
  104.     print("Waiting for "..id.." to connect..")
  105.     local sender, msg, protocol
  106.     repeat
  107.         sender, msg, protocol = rednet.receive()
  108.     until sender == id and protocol == "encrypted_chat" and msg[1] == "connect"
  109.     local sender_number = msg[2]
  110.     local encryption_number = math.pow(sender_number, X%P)
  111.     rednet.send(sender, {"connect", math.pow(Y, X%P), library}, "encrypted_chat")
  112.     print("You are now chatting with "..id)
  113.    
  114.    
  115.     local window1 = window.create(term.current(), 1, 1, 51, 18)
  116.     window1.setBackgroundColor(colors.black)
  117.     window1.setTextColor(colors.yellow)
  118.     window1.clear()
  119.     local window2 = window.create(term.current(), 1, 19, 51, 1)
  120.     window2.setBackgroundColor(colors.gray)
  121.     window2.clear()
  122.    
  123.     local function receive()
  124.         while true do
  125.             local _, msg, prot = rednet.receive()
  126.             if id == _ and prot == "encrypted_chat" then
  127.                 local str = aeslua.decrypt(tostring(encryption_number), msg, aeslua.AES128, aeslua.CBCMODE, library)
  128.                 term.redirect(window1)
  129.                 term.setCursorBlink(false)
  130.                 print(id.."> "..str)
  131.                 window2.restoreCursor()
  132.                 term.redirect(window2)
  133.                 term.setCursorBlink(true)
  134.             end
  135.         end
  136.     end
  137.    
  138.     local function send()
  139.         while true do
  140.             write("> ")
  141.             local omsg = read()
  142.             if omsg == "/exit" then break end
  143.             msg = aeslua.encrypt(tostring(encryption_number), omsg, aeslua.AES128, aeslua.CBCMODE, library)
  144.             rednet.send(id, msg, "encrypted_chat")
  145.             term.clear()
  146.             term.setCursorPos(1,1)
  147.             term.redirect(window1)
  148.             print("You> "..omsg)
  149.             term.redirect(window2)
  150.         end
  151.     end
  152.    
  153.     local evt = {}
  154.     local c1 = coroutine.create(receive)
  155.     local c2 = coroutine.create(send)
  156.     while true do
  157.         coroutine.resume(c1, unpack(evt))
  158.         coroutine.resume(c2, unpack(evt))
  159.         evt = {os.pullEventRaw()}
  160.         if coroutine.status(c2) == "dead" then
  161.             rednet.send(id, aeslua.encrypt(tostring(encryption_number), "**exited**", aeslua.AES128, aeslua.CBCMODE, library), "encrypted_chat")
  162.             break
  163.         end
  164.     end
  165.     print("Thanks for using the program!")
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement