Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define a key for XOR encryption/decryption
- local key = 123 -- Change this to a more secure key in practice
- -- Function to encrypt/decrypt a string using XOR
- local function xorEncryptDecrypt(data, key)
- local result = ""
- for i = 1, #data do
- result = result .. string.char(bit.bxor(data:byte(i), key))
- end
- return result
- end
- -- Encrypt function
- local function encryptDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- -- Recursively encrypt directories
- encryptDirectory(path)
- else
- local fileHandle = fs.open(path, "r")
- local content = fileHandle.readAll()
- fileHandle.close()
- local encryptedContent = xorEncryptDecrypt(content, key)
- fileHandle = fs.open(path, "w")
- fileHandle.write(encryptedContent)
- fileHandle.close()
- print("Encrypted: " .. path)
- end
- end
- end
- -- Decrypt function
- local function decryptDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- -- Recursively decrypt directories
- decryptDirectory(path)
- else
- local fileHandle = fs.open(path, "r")
- local content = fileHandle.readAll()
- fileHandle.close()
- local decryptedContent = xorEncryptDecrypt(content, key)
- fileHandle = fs.open(path, "w")
- fileHandle.write(decryptedContent)
- fileHandle.close()
- print("Decrypted: " .. path)
- end
- end
- end
- -- Function to prompt for input and return a trimmed string
- local function prompt(promptText)
- write(promptText)
- return read():gsub("^%s*(.-)%s*$", "%1") -- Trim spaces
- end
- -- Function to check if user credentials are correct
- local function checkCredentials(username, password)
- local userFolder = "/disk/users/" .. username
- if not fs.exists(userFolder) then
- print("User folder does not exist: " .. userFolder)
- return false
- end
- local passwordFile = userFolder .. "/password.txt"
- if not fs.exists(passwordFile) then
- print("Password file does not exist: " .. passwordFile)
- return false
- end
- local storedPassword = fs.open(passwordFile, "r").readAll()
- if storedPassword ~= password then
- print("Incorrect password.")
- return false
- end
- return true
- end
- -- Function to encrypt a disk
- local function encryptDisk()
- local username = prompt("Enter username: ")
- local password = prompt("Enter password: ")
- if not checkCredentials(username, password) then
- print("Access denied.")
- return
- end
- -- Check for disk insertion
- local disk = "/disk2/"
- if not fs.exists(disk) then
- print("No disk detected.")
- return
- end
- -- Check for files containing VA11-ILLA or setup
- local function containsRestrictedFiles(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- if containsRestrictedFiles(path) then
- return true
- end
- elseif file:find("VA11-ILLA") or file:find("setup") then
- return true
- end
- end
- return false
- end
- if containsRestrictedFiles(disk) then
- print("Access denied. Disk contains restricted files.")
- return
- end
- -- Ask if user wants to keep a backup
- local backup = prompt("Do you want to keep a backup of the data? (yes/no): ")
- if backup:lower() == "yes" then
- local backupDir = disk .. "-backup/"
- if fs.exists(backupDir) then
- fs.delete(backupDir) -- Delete if it exists
- end
- fs.copy(disk, backupDir)
- print("Backup created.")
- end
- -- Encrypt the disk
- encryptDirectory(disk)
- print("Disk encrypted.")
- -- Store the encryption key
- local keyFile = fs.open("/disk/users/" .. username .. "/encryption_key.txt", "w")
- keyFile.write(tostring(key))
- keyFile.close()
- print("Encryption key saved.")
- end
- -- Function to decrypt a disk
- local function decryptDisk()
- local username = prompt("Enter username: ")
- local password = prompt("Enter password: ")
- if not checkCredentials(username, password) then
- print("Access denied.")
- return
- end
- -- Check for disk insertion
- local disk = "/disk2/"
- if not fs.exists(disk) then
- print("No disk detected.")
- return
- end
- -- Load the encryption key
- local keyFile = "/disk/users/" .. username .. "/encryption_key.txt"
- if not fs.exists(keyFile) then
- print("No encryption key found.")
- return
- end
- key = tonumber(fs.open(keyFile, "r").readAll())
- -- Decrypt the disk
- decryptDirectory(disk)
- print("Disk decrypted.")
- -- Show directories and files
- local function showFiles(directory, indent)
- indent = indent or ""
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local path = fs.combine(directory, file)
- if fs.isDir(path) then
- print(indent .. "[" .. file .. "]")
- showFiles(path, indent .. " ")
- else
- print(indent .. file)
- end
- end
- end
- showFiles(disk)
- end
- -- Main program loop
- while true do
- print("1. Encrypt Disk")
- print("2. Decrypt Disk")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- encryptDisk()
- elseif choice == 2 then
- decryptDisk()
- elseif choice == 3 then
- break
- else
- print("Invalid choice.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement