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 fileName = "encryption_key.txt"
- local file = fs.open(fileName, "w")
- file.write(key)
- file.close()
- return fileName
- end
- -- Function to load the key from a file
- function loadKeyFromFile(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()
- 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(key)
- 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, keyPress = os.pullEvent("key")
- if keyPress == keys.f1 then
- print("Enter password:")
- local enteredPassword = read("*")
- local file = fs.open("password.txt", "r")
- local savedEncryptedPassword = file.readAll()
- file.close()
- local encryptedPassword = encrypt(enteredPassword, key)
- if encryptedPassword == savedEncryptedPassword then
- processDirectory("/disk", decrypt, key)
- shell.run("/disk/boot/BIOS")
- else
- print("Error: Incorrect password.")
- end
- return
- elseif keyPress == keys.f8 then
- os.shutdown()
- return
- elseif keyPress == 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
- 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 setup the encryption system
- function setupEncryptionSystem()
- local key = generateKey()
- print("Enter a password to secure your system:")
- local password = read("*")
- local encryptedPassword = encrypt(password, key)
- -- Save the encrypted password to password.txt
- local file = fs.open("password.txt", "w")
- file.write(encryptedPassword)
- file.close()
- saveKeyToFile(key)
- print("Setup complete. Encryption key and password saved.")
- 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 == "setup" then
- setupEncryptionSystem()
- else
- local key = loadKeyFromFile("encryption_key.txt")
- if not key then
- print("No encryption key found. Please run with 'setup' to create a new encryption system.")
- return
- end
- print("Doggy OS File System Protection")
- showProtectionScreen(key)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement