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 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
- -- Function to save the key to a file
- function saveKeyToFile(key)
- local randomNumber = math.random(1, 1000)
- local fileName = "key" .. randomNumber .. ".txt"
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Function to load the key from a file and delete it after loading
- function loadKeyFromFileAndDelete(fileName)
- 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()
- fs.delete(path) -- Delete the key file after loading
- return key
- else
- print("Error: Unable to open key file.")
- return nil
- end
- else
- print("Error: Key file does not exist.")
- return nil
- end
- end
- -- Function to show protection screen options
- function showProtectionScreen(keyFileName)
- 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
- local key = loadKeyFromFileAndDelete(keyFileName)
- if key then
- processDirectory("/disk", decrypt, key)
- shell.run("/disk/boot/BIOS")
- else
- print("Error: Unable to load encryption key.")
- end
- return
- elseif key == keys.f8 then
- os.shutdown()
- return
- elseif key == keys.f9 then
- recoverSystem()
- return
- end
- end
- 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
- -- 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 == "encrypt" then
- local key = generateKey()
- encryptDirectory("/disk", key)
- local keyFileName = saveKeyToFile(key)
- print("All files in /disk/ encrypted.")
- print("Key saved to file: " .. keyFileName)
- print()
- print("Encryption complete.")
- else
- print("Doggy OS File System Protection")
- print()
- -- Check if a key file exists in the root directory
- local rootFiles = fs.list("/")
- local keyFile = nil
- for _, file in ipairs(rootFiles) do
- if string.match(file, "^key%d+%.txt$") then
- keyFile = file
- break
- end
- end
- if keyFile then
- showProtectionScreen(keyFile)
- else
- print("No encryption key found. Automatically encrypting files...")
- local key = generateKey()
- encryptDirectory("/disk", key)
- local keyFileName = saveKeyToFile(key)
- print("All files in /disk/ encrypted.")
- print("Key saved to file: " .. keyFileName)
- print()
- print("Encryption complete.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement