Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- XOR encryption function for message security
- local function xorEncryptDecrypt(message, key)
- local encrypted = {}
- for i = 1, #message do
- local char = string.byte(message, i)
- local keyChar = string.byte(key, (i - 1) % #key + 1)
- table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
- end
- return table.concat(encrypted)
- end
- -- Function to handle receiving messages from the server
- local function handleIncomingMessages(protocol, encryptionKey)
- while true do
- local senderId, encryptedMessage, receivedProtocol = rednet.receive(protocol)
- if receivedProtocol == protocol then
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- print(decryptedMessage)
- end
- end
- end
- -- Function to send messages to the server
- local function sendMessageToServer(protocol, username, encryptionKey)
- while true do
- print("[" .. protocol .. "] Type your message (or 'exit' to leave): ")
- local message = read()
- if message == "exit" then
- print("Leaving chatroom...")
- break
- end
- local formattedMessage = username .. ": " .. message
- local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
- rednet.broadcast(encryptedMessage, protocol)
- end
- end
- -- Function to connect to the server and join the chatroom with password
- local function connectToChatroom(protocol, username)
- print("Attempting to join chatroom with protocol: " .. protocol)
- -- Get the password to access the chatroom
- print("Enter the password for this chatroom: ")
- local password = read("*")
- -- Send the password to the server for verification
- rednet.broadcast(password, protocol .. "-password")
- -- Wait for password validation result
- local senderId, validationResponse = rednet.receive(protocol .. "-password-response")
- if validationResponse == "success" then
- print("Password accepted. Joining chatroom...")
- -- Get the encryption key from the server
- rednet.broadcast("request-key", protocol .. "-key-request")
- local senderId, encryptionKey = rednet.receive(protocol .. "-key-response")
- -- Start sending and receiving messages
- parallel.waitForAny(
- function() handleIncomingMessages(protocol, encryptionKey) end,
- function() sendMessageToServer(protocol, username, encryptionKey) end
- )
- else
- print("Incorrect password. Access denied.")
- end
- end
- -- Function to list available modems
- local function listModems()
- local modems = {}
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- table.insert(modems, side)
- end
- end
- return modems
- end
- -- Function to set up the modem for communication
- local function setupModem()
- local modems = listModems()
- if #modems == 0 then
- print("No modems found! Please attach a modem and restart.")
- return nil
- end
- print("Available modems:")
- for i, side in ipairs(modems) do
- print(i .. ": " .. side)
- end
- rednet.open(modems[1]) -- Open the first modem found
- print("Modem on " .. modems[1] .. " initialized.")
- return modems[1]
- end
- -- Main client function
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome to the Protocol Private Chatroom Client")
- -- Setup modem for client communication
- local modemSide = setupModem()
- if modemSide == nil then return end -- Exit if no modem found
- -- Main loop to allow the user to join different chatrooms
- while true do
- print("Enter a protocol to connect to a private chatroom (or 'exit' to quit): ")
- local protocol = read()
- if protocol == "exit" then
- print("Client shutting down...")
- break
- end
- if protocol ~= "" then
- print("Enter your username: ")
- local username = read()
- connectToChatroom(protocol, username)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement