Advertisement
DOGGYWOOF

Untitled

Sep 29th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. -- XOR encryption function for message security
  2. local function xorEncryptDecrypt(message, key)
  3. local encrypted = {}
  4. for i = 1, #message do
  5. local char = string.byte(message, i)
  6. local keyChar = string.byte(key, (i - 1) % #key + 1)
  7. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  8. end
  9. return table.concat(encrypted)
  10. end
  11.  
  12. -- Function to handle receiving messages from the server
  13. local function handleIncomingMessages(protocol, encryptionKey)
  14. while true do
  15. local senderId, encryptedMessage, receivedProtocol = rednet.receive(protocol)
  16. if receivedProtocol == protocol then
  17. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  18. print(decryptedMessage)
  19. end
  20. end
  21. end
  22.  
  23. -- Function to send messages to the server
  24. local function sendMessageToServer(protocol, username, encryptionKey)
  25. while true do
  26. print("[" .. protocol .. "] Type your message (or 'exit' to leave): ")
  27. local message = read()
  28. if message == "exit" then
  29. print("Leaving chatroom...")
  30. break
  31. end
  32.  
  33. local formattedMessage = username .. ": " .. message
  34. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  35. rednet.broadcast(encryptedMessage, protocol)
  36. end
  37. end
  38.  
  39. -- Function to connect to the server and join the chatroom with password
  40. local function connectToChatroom(protocol, username)
  41. print("Attempting to join chatroom with protocol: " .. protocol)
  42.  
  43. -- Get the password to access the chatroom
  44. print("Enter the password for this chatroom: ")
  45. local password = read("*")
  46.  
  47. -- Send the password to the server for verification
  48. rednet.broadcast(password, protocol .. "-password")
  49.  
  50. -- Wait for password validation result
  51. local senderId, validationResponse = rednet.receive(protocol .. "-password-response")
  52.  
  53. if validationResponse == "success" then
  54. print("Password accepted. Joining chatroom...")
  55.  
  56. -- Get the encryption key from the server
  57. rednet.broadcast("request-key", protocol .. "-key-request")
  58. local senderId, encryptionKey = rednet.receive(protocol .. "-key-response")
  59.  
  60. -- Start sending and receiving messages
  61. parallel.waitForAny(
  62. function() handleIncomingMessages(protocol, encryptionKey) end,
  63. function() sendMessageToServer(protocol, username, encryptionKey) end
  64. )
  65. else
  66. print("Incorrect password. Access denied.")
  67. end
  68. end
  69.  
  70. -- Function to list available modems
  71. local function listModems()
  72. local modems = {}
  73. for _, side in ipairs(peripheral.getNames()) do
  74. if peripheral.getType(side) == "modem" then
  75. table.insert(modems, side)
  76. end
  77. end
  78. return modems
  79. end
  80.  
  81. -- Function to set up the modem for communication
  82. local function setupModem()
  83. local modems = listModems()
  84. if #modems == 0 then
  85. print("No modems found! Please attach a modem and restart.")
  86. return nil
  87. end
  88.  
  89. print("Available modems:")
  90. for i, side in ipairs(modems) do
  91. print(i .. ": " .. side)
  92. end
  93.  
  94. rednet.open(modems[1]) -- Open the first modem found
  95. print("Modem on " .. modems[1] .. " initialized.")
  96. return modems[1]
  97. end
  98.  
  99. -- Main client function
  100. term.clear()
  101. term.setCursorPos(1, 1)
  102.  
  103. print("Welcome to the Protocol Private Chatroom Client")
  104.  
  105. -- Setup modem for client communication
  106. local modemSide = setupModem()
  107. if modemSide == nil then return end -- Exit if no modem found
  108.  
  109. -- Main loop to allow the user to join different chatrooms
  110. while true do
  111. print("Enter a protocol to connect to a private chatroom (or 'exit' to quit): ")
  112. local protocol = read()
  113.  
  114. if protocol == "exit" then
  115. print("Client shutting down...")
  116. break
  117. end
  118.  
  119. if protocol ~= "" then
  120. print("Enter your username: ")
  121. local username = read()
  122.  
  123. connectToChatroom(protocol, username)
  124. end
  125. end
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement