Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Encrypted Chat for ComputerCraft
- This chat system is connection based and uses the Diffie-Hellman-Merkle key exchange and the AES Encryption API
- by SquidDev to encrypt messages.
- It is very simple and probably dirty made but it works quite well :)
- ~Piorjade
- ]]--
- shell.run("wget https://git.io/aeslua aeslua")
- os.loadAPI("aeslua")
- fs.delete("aeslua")
- rednet.open("top")
- Y = 20752
- P = 2340971
- print(math.pow(10, 13))
- X = math.random(math.pow(10, 13))
- print("your secret number is: "..X)
- print("Connect or host? (c/h)")
- local answer = ""
- repeat
- write("> ")
- answer = read()
- until answer == "c" or answer == "h"
- if answer == "c" then
- print("")
- write("Enter ID: ")
- id = tonumber(read())
- print("Waiting for "..id.." to respond....")
- rednet.send(id, {"connect",math.pow(Y, X%P)}, "encrypted_chat")
- local sender, msg, protocol
- repeat
- sender, msg, protocol = rednet.receive()
- print(tostring(sender).." "..tostring(msg).." "..tostring(protocol))
- until sender == id and protocol == "encrypted_chat" and msg[1] == "connect"
- local sender_number = msg[2]
- local sender_library = msg[3]
- local encryption_number = math.pow(sender_number, X%P)
- print("You are now sending messages to "..id)
- local window1 = window.create(term.current(), 1, 1, 51, 18)
- window1.setBackgroundColor(colors.black)
- window1.setTextColor(colors.yellow)
- window1.clear()
- local window2 = window.create(term.current(), 1, 19, 51, 1)
- window2.setBackgroundColor(colors.gray)
- window2.clear()
- local function receive()
- while true do
- local _, msg, prot = rednet.receive()
- if id == _ and prot == "encrypted_chat" then
- local str = aeslua.decrypt(tostring(encryption_number), msg, aeslua.AES128, aeslua.CBCMODE, sender_library)
- term.redirect(window1)
- term.setCursorBlink(false)
- print(id.."> "..str)
- window2.restoreCursor()
- term.redirect(window2)
- term.setCursorBlink(true)
- end
- end
- end
- local function send()
- while true do
- write("> ")
- local omsg = read()
- if omsg == "/exit" then break end
- msg = aeslua.encrypt(tostring(encryption_number), omsg, aeslua.AES128, aeslua.CBCMODE, sender_library)
- rednet.send(id, msg, "encrypted_chat")
- term.clear()
- term.setCursorPos(1,1)
- term.redirect(window1)
- print("You> "..omsg)
- term.redirect(window2)
- end
- end
- local evt = {}
- local c1 = coroutine.create(receive)
- local c2 = coroutine.create(send)
- while true do
- coroutine.resume(c1, unpack(evt))
- coroutine.resume(c2, unpack(evt))
- evt = {os.pullEventRaw()}
- if coroutine.status(c2) == "dead" then
- rednet.send(id, aeslua.encrypt(tostring(encryption_number), "**exited**", aeslua.AES128, aeslua.CBCMODE, sender_library), "encrypted_chat")
- break
- end
- end
- print("Thanks for using the program!")
- else
- local library = {}
- for i = 1, 16 do library[i] = math.random(1, 255) end
- print("")
- write("Enter ID: ")
- id = tonumber(read())
- print("Waiting for "..id.." to connect..")
- local sender, msg, protocol
- repeat
- sender, msg, protocol = rednet.receive()
- until sender == id and protocol == "encrypted_chat" and msg[1] == "connect"
- local sender_number = msg[2]
- local encryption_number = math.pow(sender_number, X%P)
- rednet.send(sender, {"connect", math.pow(Y, X%P), library}, "encrypted_chat")
- print("You are now chatting with "..id)
- local window1 = window.create(term.current(), 1, 1, 51, 18)
- window1.setBackgroundColor(colors.black)
- window1.setTextColor(colors.yellow)
- window1.clear()
- local window2 = window.create(term.current(), 1, 19, 51, 1)
- window2.setBackgroundColor(colors.gray)
- window2.clear()
- local function receive()
- while true do
- local _, msg, prot = rednet.receive()
- if id == _ and prot == "encrypted_chat" then
- local str = aeslua.decrypt(tostring(encryption_number), msg, aeslua.AES128, aeslua.CBCMODE, library)
- term.redirect(window1)
- term.setCursorBlink(false)
- print(id.."> "..str)
- window2.restoreCursor()
- term.redirect(window2)
- term.setCursorBlink(true)
- end
- end
- end
- local function send()
- while true do
- write("> ")
- local omsg = read()
- if omsg == "/exit" then break end
- msg = aeslua.encrypt(tostring(encryption_number), omsg, aeslua.AES128, aeslua.CBCMODE, library)
- rednet.send(id, msg, "encrypted_chat")
- term.clear()
- term.setCursorPos(1,1)
- term.redirect(window1)
- print("You> "..omsg)
- term.redirect(window2)
- end
- end
- local evt = {}
- local c1 = coroutine.create(receive)
- local c2 = coroutine.create(send)
- while true do
- coroutine.resume(c1, unpack(evt))
- coroutine.resume(c2, unpack(evt))
- evt = {os.pullEventRaw()}
- if coroutine.status(c2) == "dead" then
- rednet.send(id, aeslua.encrypt(tostring(encryption_number), "**exited**", aeslua.AES128, aeslua.CBCMODE, library), "encrypted_chat")
- break
- end
- end
- print("Thanks for using the program!")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement