Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RedNet Decoder with Enhanced Info, Auto Decrypt & Auto Save to One File
- local modemSide = "top" -- Change to the side where your modem is connected
- rednet.open(modemSide)
- -- Global settings
- local settings = {
- autoSave = false, -- Auto save decrypted messages
- autoDecrypt = false -- Auto decrypt incoming RedNet messages
- }
- -- Define the default key file for auto decryption
- local defaultKeyFile = "decryption_key.txt" -- The key file for auto-decrypt
- local decryptedMessagesFile = "decrypted_messages.txt" -- The file where all messages will be saved
- -- Function to read the decryption key from a file
- local function readKeyFromFile(fileName)
- if not fs.exists(fileName) then
- error("File does not exist: " .. fileName)
- end
- local file = fs.open(fileName, "r")
- local key = file.readAll()
- file.close()
- return key:gsub("%s+", "") -- Trim whitespace
- end
- -- Function to decrypt a message using XOR
- local function decryptMessage(encryptedMessage, key)
- local decryptedMessage = ""
- local encryptionKey = {} -- Store the encryption key for display
- for i = 1, #encryptedMessage do
- local encChar = string.byte(encryptedMessage:sub(i, i))
- local keyChar = string.byte(key:sub(((i - 1) % #key) + 1))
- decryptedMessage = decryptedMessage .. string.char(bit.bxor(encChar, keyChar))
- table.insert(encryptionKey, keyChar) -- Store the XOR key characters
- end
- return decryptedMessage, encryptionKey
- end
- -- Function to auto save the decrypted message to a single file
- local function autoSaveMessage(decryptedMessage, senderId)
- if settings.autoSave then
- local timestamp = os.date("%Y-%m-%d_%H-%M-%S")
- local file = fs.open(decryptedMessagesFile, "a") -- Open file in append mode
- file.write("\n--------------------------------------------------\n")
- file.write("Timestamp: " .. os.date("%Y-%m-%d %H:%M:%S") .. "\n")
- file.write("Sender ID: " .. senderId .. "\n")
- file.write("Decrypted Message: " .. decryptedMessage .. "\n")
- file.write("--------------------------------------------------\n")
- file.close()
- print("Message saved automatically to", decryptedMessagesFile)
- end
- end
- -- Function to handle RedNet messages
- local function handleRedNetMessages()
- print("\n--------------------------------------------------")
- print("Waiting for a RedNet message... Press 'q' to quit.")
- print("--------------------------------------------------\n")
- while true do
- local senderId, encryptedMessage = rednet.receive()
- -- Show detailed information about the message
- print("\n--------------------------------------------------")
- print("Encrypted message received from ID:", senderId)
- print("Timestamp: " .. os.date("%Y-%m-%d %H:%M:%S"))
- print("Encrypted Message: " .. encryptedMessage)
- print("--------------------------------------------------\n")
- -- Handle decryption
- local decryptedMessage
- local keyFileUsed
- local encryptionKey
- if settings.autoDecrypt then
- -- Use the predefined key file for decryption
- local key
- local success, err = pcall(function()
- key = readKeyFromFile(defaultKeyFile)
- end)
- if not success then
- print("Error reading key:", err)
- return
- end
- decryptedMessage, encryptionKey = decryptMessage(encryptedMessage, key)
- keyFileUsed = defaultKeyFile
- print("Decrypted Message: " .. decryptedMessage)
- else
- -- If autoDecrypt is off, ask for file name
- print("Auto Decrypt is disabled. Please enter the key file name for decryption:")
- local keyFileName = read()
- local key
- if fs.exists(keyFileName) then
- key = readKeyFromFile(keyFileName)
- decryptedMessage, encryptionKey = decryptMessage(encryptedMessage, key)
- keyFileUsed = keyFileName
- print("Decrypted Message: " .. decryptedMessage)
- else
- print("Error: Key file does not exist.")
- return
- end
- end
- -- Show additional information after decryption
- print("\n--------------------------------------------------")
- print("Decryption Complete:")
- print("Original Encrypted Message: " .. encryptedMessage)
- print("Decrypted Message: " .. decryptedMessage)
- print("Sender ID: " .. senderId)
- print("Timestamp: " .. os.date("%Y-%m-%d %H:%M:%S"))
- print("Decryption Key File Used: " .. keyFileUsed)
- print("Encryption Key Used: " .. table.concat(encryptionKey, ", "))
- print("--------------------------------------------------\n")
- -- Auto Save if enabled
- autoSaveMessage(decryptedMessage, senderId)
- end
- end
- -- Main Settings Menu for configuring options
- local function settingsMenu()
- while true do
- print("\n--------------------------------------------------")
- print("Settings Menu:")
- print("1. Toggle Auto Save (Currently: " .. (settings.autoSave and "On" or "Off") .. ")")
- print("2. Toggle Auto Decrypt (Currently: " .. (settings.autoDecrypt and "On" or "Off") .. ")")
- print("3. Set Decryption Key File (Current: " .. defaultKeyFile .. ")")
- print("4. Back to Main Menu")
- print("--------------------------------------------------")
- print("Select an option (1-4):")
- local choice = tonumber(read())
- if choice == 1 then
- settings.autoSave = not settings.autoSave
- print("Auto Save is now " .. (settings.autoSave and "enabled" or "disabled"))
- elseif choice == 2 then
- settings.autoDecrypt = not settings.autoDecrypt
- print("Auto Decrypt is now " .. (settings.autoDecrypt and "enabled" or "disabled"))
- elseif choice == 3 then
- print("Enter the new decryption key file path:")
- defaultKeyFile = read()
- print("Decryption key file is now set to: " .. defaultKeyFile)
- elseif choice == 4 then
- break
- else
- print("Invalid choice, please try again.")
- end
- end
- end
- -- Main Menu for navigating the program
- local function mainMenu()
- while true do
- print("\n--------------------------------------------------")
- print("RedNet Decoder Main Menu")
- print("1. Start RedNet - Receive and Decode Messages")
- print("2. Settings - Configure Auto Save and Auto Decrypt")
- print("3. Exit")
- print("--------------------------------------------------")
- print("Please select an option (1-3):")
- local choice = tonumber(read())
- if choice == 1 then
- handleRedNetMessages()
- elseif choice == 2 then
- settingsMenu()
- elseif choice == 3 then
- print("Exiting program...")
- break
- else
- print("Invalid choice, please try again.")
- end
- end
- end
- -- Run the main menu
- mainMenu()
- rednet.close()
- print("RedNet Decoder has stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement