Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --========================================================--
- -- ROBCO INDUSTRIES (TM) UNIFIED OPERATING SYSTEM
- -- NETWORK MODULE — ENCRYPTED REDNET COMMUNICATION
- --========================================================--
- local network = {}
- local ui = require("robco/ui")
- ------------------------------------------------------------
- -- ENCRYPTION KEY
- ------------------------------------------------------------
- local ENCRYPTION_KEY = "ROBCO-SECURE-KEY"
- ------------------------------------------------------------
- -- XOR BYTE (Minecraft‑compatible)
- ------------------------------------------------------------
- local function xorByte(a, b)
- local r = 0
- for i = 0, 7 do
- local x = a % 2
- local y = b % 2
- if x ~= y then r = r + 2^i end
- a = math.floor(a / 2)
- b = math.floor(b / 2)
- end
- return r
- end
- ------------------------------------------------------------
- -- ENCRYPT / DECRYPT
- ------------------------------------------------------------
- local function encrypt(text, key)
- if not key or key == "" then return text end
- local out = {}
- for i = 1, #text do
- local c = text:byte(i)
- local k = key:byte(((i-1) % #key) + 1)
- table.insert(out, string.char(xorByte(c, k)))
- end
- return table.concat(out)
- end
- local function decrypt(text, key)
- return encrypt(text, key)
- end
- ------------------------------------------------------------
- -- MODEM DETECTION
- ------------------------------------------------------------
- local modem = peripheral.find("modem")
- function network.hasModem()
- return modem ~= nil
- end
- if modem then
- rednet.open(peripheral.getName(modem))
- end
- ------------------------------------------------------------
- -- CONNECT TO ANOTHER TERMINAL
- ------------------------------------------------------------
- function network.connect(id)
- if not modem then
- ui.statusLine("NO MODEM DETECTED")
- return
- end
- if not id then
- ui.statusLine("USAGE: connect <id>")
- return
- end
- ui.statusLine("PINGING " .. id .. "...")
- rednet.send(id, encrypt("ROBCO_CONNECT", ENCRYPTION_KEY), "robco")
- local sender, msg = rednet.receive("robco", 2)
- if sender == id and decrypt(msg, ENCRYPTION_KEY) == "ROBCO_ACK" then
- ui.statusLine("CONNECTION ESTABLISHED")
- else
- ui.statusLine("NO RESPONSE")
- end
- end
- ------------------------------------------------------------
- -- SEND ENCRYPTED MESSAGE
- ------------------------------------------------------------
- function network.sendMsg(id, msg)
- if not modem then
- ui.statusLine("NO MODEM DETECTED")
- return
- end
- if not id or msg == "" then
- ui.statusLine("USAGE: send <id> <msg>")
- return
- end
- rednet.send(id, encrypt(msg, ENCRYPTION_KEY), "robco")
- ui.statusLine("ENCRYPTED MESSAGE SENT")
- end
- ------------------------------------------------------------
- -- LISTEN FOR ENCRYPTED MESSAGE
- ------------------------------------------------------------
- function network.listen()
- if not modem then
- ui.statusLine("NO MODEM DETECTED")
- return
- end
- ui.statusLine("LISTENING...")
- local sender, msg = rednet.receive("robco")
- local dec = decrypt(msg, ENCRYPTION_KEY)
- term.setCursorPos(3, 9)
- print("MESSAGE FROM " .. sender .. ":")
- term.setCursorPos(3, 10)
- print(dec)
- ui.statusLine("MESSAGE RECEIVED")
- end
- ------------------------------------------------------------
- -- BACKGROUND LISTENER (AUTO‑ACK)
- ------------------------------------------------------------
- function network.backgroundListener()
- if not modem then return end
- while true do
- local sender, msg = rednet.receive("robco")
- if decrypt(msg, ENCRYPTION_KEY) == "ROBCO_CONNECT" then
- rednet.send(sender, encrypt("ROBCO_ACK", ENCRYPTION_KEY), "robco")
- end
- end
- end
- return network
Advertisement