Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to generate a key for encryption and decryption
- function generateKey()
- local chars = {}
- for i = 32, 126 do -- ASCII range for printable characters
- table.insert(chars, string.char(i))
- end
- local key = {}
- while #chars > 0 do
- local index = math.random(#chars)
- table.insert(key, table.remove(chars, index))
- end
- return table.concat(key)
- end
- -- Function to create a map from a key
- function createMap(key)
- local map = {}
- for i = 32, 126 do
- map[string.char(i)] = key:sub(i - 31, i - 31)
- end
- return map
- end
- -- Function to invert a map
- function invertMap(map)
- local invMap = {}
- for k, v in pairs(map) do
- invMap[v] = k
- end
- return invMap
- end
- -- Function to encrypt text using a key
- function encrypt(text, key)
- local map = createMap(key)
- local encrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(encrypted, map[char] or char)
- end
- return table.concat(encrypted)
- end
- -- Function to decrypt text using a key
- function decrypt(text, key)
- local blockedPath = "/disk/blocked"
- if fs.exists(blockedPath) then
- -- Display error message and prompt to reboot
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS File System Protection")
- print()
- print("Something happened and the file system cannot be decrypted.")
- print("To protect the data, this device needs to be reset and Doggy OS has to be reinstalled.")
- print()
- print("Press Enter to reboot...")
- read() -- Wait for user to press Enter
- os.reboot()
- else
- local map = invertMap(createMap(key))
- local decrypted = {}
- for i = 1, #text do
- local char = text:sub(i, i)
- table.insert(decrypted, map[char] or char)
- end
- return table.concat(decrypted)
- end
- end
- -- Function to save the key to a file
- function saveKeyToFile(key)
- local fileName = "key.txt" -- Fixed key file name
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Function to load the key from a file without deleting it
- function loadKeyFromFile()
- local fileName = "key.txt" -- Fixed key file name
- local path = fs.combine("/", fileName)
- if fs.exists(path) then
- local file = fs.open(path, "r")
- if file then
- local key = file.readAll()
- file.close()
- return key
- else
- return nil, "Error: Unable to open key file."
- end
- else
- return nil, "Error: Key file does not exist."
- end
- end
- -- Function to set or reset the password in the password file
- -- and update the key file
- function setPassword(newPassword)
- local key = generateKey() -- Generate a new encryption key
- local encryptedPassword = encrypt(newPassword, key)
- local fileName = "pass.txt" -- Fixed password file name
- local file = fs.open(fileName, "w")
- file.write(encryptedPassword)
- file.close()
- -- Save the key to key.txt
- saveKeyToFile(key)
- return true
- end
- -- Function to verify password during system startup
- function verifyPassword(enteredPassword)
- local fileName = "pass.txt" -- Fixed password file name
- local path = fs.combine("/", fileName)
- if fs.exists(path) then
- local file = fs.open(path, "r")
- if file then
- local encryptedPassword = file.readAll()
- file.close()
- local key, loadError = loadKeyFromFile()
- if key then
- local decryptedPassword = decrypt(encryptedPassword, key)
- if enteredPassword == decryptedPassword then
- return true
- else
- -- Create blocked file on incorrect password and initiate reboot
- local blockedFile = fs.open("/disk/blocked", "w")
- blockedFile.write("This file indicates decryption is blocked.")
- blockedFile.close()
- print("Incorrect password.")
- print("Press Enter to reboot...")
- read() -- Wait for user to press Enter
- os.reboot()
- return false
- end
- else
- print(loadError)
- return false
- end
- else
- print("Error: Unable to open password file.")
- return false
- end
- else
- print("Error: Password file does not exist.")
- return false
- end
- end
- -- Function to create a blocked file
- function createBlockedFile()
- local fileName = "/disk/blocked"
- local file = fs.open(fileName, "w")
- file.write("This file indicates decryption is blocked.")
- file.close()
- end
- -- Function to delete /disk and /recovery directories, and key/password files
- function formatSystem()
- -- Delete /disk and /recovery directories
- fs.delete("/disk")
- fs.delete("/recovery")
- -- Remove key.txt and pass.txt
- fs.delete("/key.txt")
- fs.delete("/pass.txt")
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS File System Protection")
- print()
- print("This device has been reset and needs to be repaired.")
- print("All user and system data has been cleared.")
- print("Insert this device into a Doggy OS Machine to reinstall all firmware and software.")
- end
- -- Function to recover the system (delete disk and replace with recovery)
- function recoverSystem()
- local files = fs.list("/disk")
- for _, file in ipairs(files) do
- local filePath = fs.combine("/disk", file)
- if not fs.isDir(filePath) then
- fs.delete(filePath)
- end
- end
- -- Optionally copy recovery files from a backup location or setup
- print("System recovered. Please restart your system.")
- end
- -- Function to encrypt files in a directory
- function encryptDirectory(path, key)
- local files = fs.list(path)
- for _, file in ipairs(files) do
- local filePath = fs.combine(path, file)
- if fs.isDir(filePath) then
- encryptDirectory(filePath, key)
- else
- local file = fs.open(filePath, "r")
- local content = file.readAll()
- file.close()
- local encryptedContent = encrypt(content, key)
- file = fs.open(filePath, "w")
- file.write(encryptedContent)
- file.close()
- end
- end
- end
- -- Function to process files in a directory with a given process function
- function processDirectory(path, processFunction, key)
- local files = fs.list(path)
- for _, file in ipairs(files) do
- local filePath = fs.combine(path, file)
- if fs.isDir(filePath) then
- processDirectory(filePath, processFunction, key)
- else
- local file = fs.open(filePath, "r")
- local content = file.readAll()
- file.close()
- local processedContent = processFunction(content, key)
- file = fs.open(filePath, "w")
- file.write(processedContent)
- file.close()
- end
- end
- end
- -- Function to encrypt the entire system ("/disk" directory)
- function encryptSystem()
- local key = loadKeyFromFile()
- if key then
- encryptDirectory("/disk", key)
- print("System encrypted.")
- else
- print("Error: Unable to load encryption key.")
- end
- end
- -- Function to show protection screen options
- function showProtectionScreen()
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS File System Protection")
- print()
- print("F1 - Unlock")
- print("F8 - Shutdown")
- print("F9 - Recover System")
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.f1 then
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter decryption password:")
- local password = read("*")
- if verifyPassword(password) then
- local key = loadKeyFromFile()
- if key then
- processDirectory("/disk", decrypt, key)
- shell.run("/disk/boot/BIOS")
- else
- print("Error: Unable to load encryption key.")
- end
- else
- print("Incorrect password.")
- end
- return
- elseif key == keys.f8 then
- os.shutdown()
- return
- elseif key == keys.f9 then
- recoverSystem()
- return
- end
- end
- end
- -- Main Program
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS File System Encryption")
- print()
- print("Initializing...")
- local tArgs = {...}
- local mode = tArgs[1]
- if mode == "decrypt" then
- -- Check if decryption is blocked
- local blockedPath = "/disk/blocked"
- if fs.exists(blockedPath) then
- -- Display error message
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS File System Protection")
- print()
- print("Something happened and the file system cannot be decrypted.")
- print("To protect the data, this device needs to be reset and Doggy OS has to be reinstalled.")
- print()
- print("Press Enter to reboot...")
- read() -- Wait for user to press Enter
- os.reboot()
- else
- showProtectionScreen()
- end
- elseif mode == "reset" then
- -- Reset password functionality
- local newPassword = tArgs[2]
- if newPassword then
- if setPassword(newPassword) then
- print("Password reset successful.")
- else
- print("Error resetting password.")
- end
- else
- print("Usage: reset newpassword")
- end
- elseif mode == "encrypt" then
- encryptSystem()
- elseif mode == "format" then
- formatSystem()
- else
- print("Invalid mode. Supported modes: decrypt, reset, encrypt, format")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement