Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Client-Side Code
- local chatProtocol = "" -- Custom protocol (ID entered by user)
- local encryptionKey
- -- Function to check if a file exists
- local function fileExists(path)
- local file = fs.open(path, "r")
- if file then
- file.close()
- return true
- end
- return false
- end
- -- Function to download or create the XOR.key file
- local function downloadOrCreateKey()
- local keyFilePath = "XOR.key"
- if fileExists(keyFilePath) then
- print("Encryption key found.")
- else
- print("Downloading encryption key...")
- local key = "x3A7rB8zD2L5qW4nH1"
- local file = fs.open(keyFilePath, "w")
- file.write(key)
- file.close()
- print("Encryption key saved as XOR.key.")
- end
- local file = fs.open(keyFilePath, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- XOR encryption function
- 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 sending messages
- local function sendMessage(username)
- while true do
- print("Type your message (or 'exit' to quit): ")
- local userInput = read()
- if userInput == "exit" then
- print("Exiting chat...")
- break
- end
- local formattedMessage = username .. ": " .. userInput
- local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
- rednet.broadcast(encryptedMessage, chatProtocol)
- print("You: " .. userInput)
- end
- end
- -- Function to handle receiving messages
- local function handleIncomingMessages()
- while true do
- local senderId, encryptedMessage, protocol = rednet.receive()
- if protocol == chatProtocol then
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
- if username and chatMessage then
- print("[" .. username .. "]: " .. chatMessage)
- else
- print("[Unknown User]: " .. decryptedMessage)
- end
- end
- end
- end
- -- Main program
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome to the Doggy OS Network Chatroom!")
- print("Enter the chatroom ID to connect:")
- chatProtocol = read()
- encryptionKey = downloadOrCreateKey()
- rednet.open("top") -- Open modem on front side to connect to the server
- print("Connecting to chatroom ID: " .. chatProtocol)
- print("Type 'exit' to leave the chat.")
- parallel.waitForAny(
- function() handleIncomingMessages() end,
- function() sendMessage("User_" .. math.random(1000, 9999)) end
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement