Advertisement
DOGGYWOOF

Untitled

Nov 23rd, 2024 (edited)
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. -- Client-Side Code
  2. local chatProtocol = "" -- Custom protocol (ID entered by user)
  3. local encryptionKey
  4.  
  5. -- Function to check if a file exists
  6. local function fileExists(path)
  7. local file = fs.open(path, "r")
  8. if file then
  9. file.close()
  10. return true
  11. end
  12. return false
  13. end
  14.  
  15. -- Function to download or create the XOR.key file
  16. local function downloadOrCreateKey()
  17. local keyFilePath = "XOR.key"
  18. if fileExists(keyFilePath) then
  19. print("Encryption key found.")
  20. else
  21. print("Downloading encryption key...")
  22. local key = "x3A7rB8zD2L5qW4nH1"
  23. local file = fs.open(keyFilePath, "w")
  24. file.write(key)
  25. file.close()
  26. print("Encryption key saved as XOR.key.")
  27. end
  28. local file = fs.open(keyFilePath, "r")
  29. local key = file.readAll()
  30. file.close()
  31. return key
  32. end
  33.  
  34. -- XOR encryption function
  35. local function xorEncryptDecrypt(message, key)
  36. local encrypted = {}
  37. for i = 1, #message do
  38. local char = string.byte(message, i)
  39. local keyChar = string.byte(key, (i - 1) % #key + 1)
  40. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  41. end
  42. return table.concat(encrypted)
  43. end
  44.  
  45. -- Function to handle sending messages
  46. local function sendMessage(username)
  47. while true do
  48. print("Type your message (or 'exit' to quit): ")
  49. local userInput = read()
  50. if userInput == "exit" then
  51. print("Exiting chat...")
  52. break
  53. end
  54.  
  55. local formattedMessage = username .. ": " .. userInput
  56. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  57. rednet.broadcast(encryptedMessage, chatProtocol)
  58. print("You: " .. userInput)
  59. end
  60. end
  61.  
  62. -- Function to handle receiving messages
  63. local function handleIncomingMessages()
  64. while true do
  65. local senderId, encryptedMessage, protocol = rednet.receive()
  66. if protocol == chatProtocol then
  67. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  68. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  69. if username and chatMessage then
  70. print("[" .. username .. "]: " .. chatMessage)
  71. else
  72. print("[Unknown User]: " .. decryptedMessage)
  73. end
  74. end
  75. end
  76. end
  77.  
  78. -- Main program
  79. term.clear()
  80. term.setCursorPos(1, 1)
  81.  
  82. print("Welcome to the Doggy OS Network Chatroom!")
  83. print("Enter the chatroom ID to connect:")
  84. chatProtocol = read()
  85.  
  86. encryptionKey = downloadOrCreateKey()
  87.  
  88. rednet.open("top") -- Open modem on front side to connect to the server
  89. print("Connecting to chatroom ID: " .. chatProtocol)
  90.  
  91. print("Type 'exit' to leave the chat.")
  92. parallel.waitForAny(
  93. function() handleIncomingMessages() end,
  94. function() sendMessage("User_" .. math.random(1000, 9999)) end
  95. )
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement