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, dir)
- local randomNumber = math.random(1, 1000)
- local fileName = dir .. "/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
- function loadKeyFromFile(fileName)
- local file = fs.open(fileName, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- Function to find the key file in a directory
- function findKeyFile(dir)
- local files = fs.list(dir)
- for _, file in ipairs(files) do
- if file:match("^key%d+%.txt$") then
- return dir .. "/" .. file
- end
- end
- return nil
- end
- -- Function to load the encrypted text from the password file
- function loadEncryptedText(userDir)
- local fileName = userDir .. "/password.txt"
- if not fs.exists(fileName) then
- print("Password file does not exist.")
- return nil
- end
- local file = fs.open(fileName, "r")
- local encryptedText = file.readAll()
- file.close()
- return encryptedText
- end
- -- API Functions
- function create(username, text)
- local userDir = "/disk/users/" .. username
- if not fs.exists(userDir) then
- fs.makeDir(userDir)
- end
- local key = generateKey()
- local encryptedText = encrypt(text, key)
- local keyFileName = saveKeyToFile(key, userDir)
- local passwordFile = fs.open(userDir .. "/password.txt", "w")
- passwordFile.write(encryptedText)
- passwordFile.close()
- return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName }
- end
- function delete(username)
- local userDir = "/disk/users/" .. username
- if fs.exists(userDir) then
- fs.delete(userDir)
- return { status = "success", message = "User directory deleted." }
- else
- return { status = "error", message = "User directory not found." }
- end
- end
- function authenticate(adminUsername, adminPassword)
- -- Replace with actual admin credential verification
- if adminUsername == "admin" and adminPassword == "password" then
- return { status = "success", message = "Authenticated successfully." }
- else
- return { status = "error", message = "Authentication failed." }
- end
- end
- -- Example Usage
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage: api <command> <args>")
- return
- end
- local command = tArgs[1]
- if command == "create" then
- if #tArgs < 3 then
- print("Usage: api create <username> <text>")
- return
- end
- local username = tArgs[2]
- local text = table.concat(tArgs, " ", 3)
- local result = create(username, text)
- print(textutils.serialize(result))
- elseif command == "delete" then
- if #tArgs < 2 then
- print("Usage: api delete <username>")
- return
- end
- local username = tArgs[2]
- local result = delete(username)
- print(textutils.serialize(result))
- elseif command == "authenticate" then
- if #tArgs < 3 then
- print("Usage: api authenticate <adminUsername> <adminPassword>")
- return
- end
- local adminUsername = tArgs[2]
- local adminPassword = tArgs[3]
- local result = authenticate(adminUsername, adminPassword)
- print(textutils.serialize(result))
- else
- print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement