Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not screen then os.loadAPI("apis/screen.lua") end
- magicNumbers = string.char(0x83,0xf9)
- --Opens some sort of file explorer
- --@OptionalArg string path: The path to start from
- --@Returns string result: The selected file
- --@Returns boolean false: If the path didnt exist, or if the user clicked the "quit" button
- function selectFile(path)
- if path and not fs.exists(path) then return false end
- if path and not fs.isDir(path) then return path end
- path = path or "/"
- local files = fs.list(path)
- local items = {}
- for k,v in pairs(files) do
- local color = colors.green
- if not fs.isDir(fs.combine(path,v)) then
- color = colors.white
- end
- local args = screen.section_char .. textutils.serialiseJSON({color=colors.black,bgColor=color}) .. screen.section_char
- table.insert(items,args .. v)
- end
- local selectionScreen = screen.SelectionScreen(1,1,nil,nil,items,"Current dir: "..path)
- local t,data = selectionScreen:selectItem()
- selectionScreen:render(false)
- term.clear()
- if t == "back" then data = ".." end
- if t == "quit" then return false end
- return selectFile(fs.combine(path,data))
- end
- --Allows a user to type in a password without people watching them
- --@OptionalArg number x: The x position to start from
- --@OptionalArg number y: The y position to start from
- --@OptionalArg number maxLength: The maximum length that the password can be
- --@Returns string pass: The password
- function enterPassword(x,y,maxLength)
- x = math.max(tonumber(x) or 1,1)
- y = math.max(tonumber(y) or 1,1)
- maxLength = tonumber(maxLength) or 69420
- maxLength = math.max(maxLength,#"Please enter the password:")
- local current = term.current()
- local maxX,maxY = term.getSize()
- local oldx,oldy = term.getCursorPos()
- local oldColor,oldBG = term.getTextColor(), term.getBackgroundColor()
- local wndw = window.create(current,x,y,math.min(maxX,maxLength),2)
- term.redirect(wndw)
- term.clear()
- local pass = ""
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- print("Please enter the password:")
- term.write("> ")
- term.setTextColor(colors.red)
- local _,key = os.pullEvent("key")
- while key ~= keys.enter do
- if key <= 256 and #pass <= maxLength then
- local _,char = os.pullEvent("char")
- pass = pass .. char
- term.write("*")
- elseif key == keys.delete or key == keys.backspace then
- pass = string.sub(pass,1,-2)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- print("Please enter the password:")
- term.write("> ")
- term.setTextColor(colors.red)
- term.write(string.gsub(pass,".","*"))
- end
- _,key = os.pullEvent("key")
- end
- wndw.setVisible(false)
- term.redirect(current)
- term.setCursorPos(oldx,oldy)
- term.setTextColor(oldColor)
- term.setBackgroundColor(oldBG)
- return pass
- end
- --Turns a string into a number
- --@Arg string str: The string to hash
- --@OptionalArg number size: The range of the hash, from 0 to size-1. Defaults to 256 (making the hash a byte)
- --@Returns number hash: The resultant hash
- local function getHash(str,size)
- size = tonumber(size) or 256
- local hash = 0
- math.randomseed(size)
- for i=1,#str do
- math.randomseed(string.byte(str,i) + math.random(size))
- hash = (hash + math.random(size))%size
- end
- return hash%size
- end
- --Scrambles a string
- --@Arg string str: The string to scramble
- --@Arg string pass: The pass to use to scramble
- --@OptionalArg number epoch: Additional random factor to add to the scramble
- --@Returns string result, table ops: The scrambled string, and the operations
- local function scramble(str,pass,epoch)
- if #str == 0 then return "" end
- local numOps = math.floor(#str*1.5) + getHash(pass,#str + 69)
- local ops = {}
- local res = str
- math.randomseed(getHash(pass,256^2)/numOps + (tonumber(epoch) or 0))
- for i=1,numOps do
- op1,op2 = math.random(#str),math.random(#str)
- local char1 = string.sub(res,op1,op1)
- local char2 = string.sub(res,op2,op2)
- res = string.sub(res,1,op2-1) .. char1 .. string.sub(res,op2+1)
- res = string.sub(res,1,op1-1)..char2 .. string.sub(res,op1+1)
- table.insert(ops,{op1,op2})
- end
- return res,ops
- end
- --Unscrambles a string
- --@Arg string str: The string to unscramble
- --@Arg string pass: the pass to use to unscramble
- --@OptionalArg number epoch:
- local function unscramble(str,pass,epoch)
- local _,ops = scramble(str,pass,epoch)
- local res = str
- for i=#ops,1,-1 do
- local char1 = string.sub(res,ops[i][2],ops[i][2])
- local char2 = string.sub(res,ops[i][1],ops[i][1])
- res = string.sub(res,1,ops[i][1] - 1) .. char1 .. string.sub(res, ops[i][1] + 1, -1)
- res = string.sub(res,1,ops[i][2] - 1) .. char2 .. string.sub(res, ops[i][2] + 1, -1)
- end
- return res
- end
- --Encrypts a string using a passphrase
- --@Arg string str: The string to encrypt
- --@Arg string pass: The passphrase to use to encrypt
- --@OptionalArg number hashSize: The maximum number of the hashsize. Make larger for harder to decrypt data. Defaults to 256
- --@OptionalArg boolean noPadding: If set to true, will not add padding
- --@OptionalArg boolean noScramble: If set to true, will not scramble letters
- --@OptionalArg number epoch: Adds an additional epoch factor to the scrambling and encryption
- --@Returns string gnirts: The encrypted string
- function encryptString(str,pass,hashSize,noPadding,noScramble,epoch)
- epoch = tonumber(epoch) or 0
- local gnirts = {}
- for i=1,#pass do
- gnirts[i] = {}
- math.randomseed(getHash(pass .. string.sub(pass,i,i), hashSize) + epoch)
- for j=1,math.ceil(#str/#pass) do
- local oldChar = string.sub(str,(j-1)*#pass+i,(j-1)*#pass+i)
- if #oldChar > 0 then
- local char = string.char( (string.byte(oldChar)+math.random(256))%256 )
- gnirts[i][j] = char
- end
- end
- end
- local result = ""
- for i=1,#gnirts[1] do
- for j=1,#gnirts do
- result = result .. (gnirts[j][i] or "")
- end
- end
- if not noPadding then
- --Attaching padding
- math.randomseed(getHash(pass, hashSize) + epoch)
- local hlen,flen = math.random(256),math.random(256)
- local header,footer = "",""
- math.randomseed(os.epoch())
- for i=1,hlen do
- header = header .. string.char(math.random(256)-1)
- end
- for i=1,flen do
- footer = footer .. string.char(math.random(256)-1)
- end
- result = header .. result .. footer
- end
- if not noScramble then
- result = scramble(result,pass)
- end
- math.randomseed(os.epoch("local"))
- return result
- end
- --Decrypts the string using a passphrase
- --@Arg string str: The string to decrypt
- --@Arg string pass: The passphrase to use to decrypt
- --@OptionalArg number hashSize: The hashSize of the password
- --@OptionalArg boolean noPadding: Whether to ignore padding or not
- --@OptionalArg boolean noScramble: Whether to not unscramble the data
- --@OptionalArg number epoch: The additional epoch factor to use
- --@Returns string result: The decrypted string
- function decryptString(str,pass,hashSize,noPadding,noScramble,epoch)
- epoch = tonumber(epoch) or 0
- if not noScramble then
- str = unscramble(str,pass)
- end
- if not noPadding then
- math.randomseed(getHash(pass, hashSize) + epoch)
- local hlen,flen = math.random(256),math.random(256)
- str = string.sub(str,hlen+1,-1*(flen+1)) --popping off headers
- end
- local gnirts = {}
- for i=1,#pass do
- gnirts[i] = {}
- math.randomseed(getHash(pass .. string.sub(pass,i,i), hashSize) + epoch)
- for j=1,math.ceil(#str/#pass) do
- local oldChar = string.sub(str,(j-1)*#pass+i,(j-1)*#pass+i)
- if #oldChar > 0 then
- local char = string.char( (string.byte(oldChar)-math.random(256))%256 )
- gnirts[i][j] = char
- end
- end
- end
- local result = ""
- for i=1,#gnirts[1] do
- for j=1,#gnirts do
- result = result .. (gnirts[j][i] or "")
- end
- end
- math.randomseed(os.epoch("local"))
- return result
- end
- --Encrypts a file using a passcode
- --@OptionalArg string path: The file to encrypt. If not given, then it opens up a file explorer
- --@OptionalArg string pass: The password used to encrypt. If not given, it opens up a password entry system
- --@OptionalArg number hashSize: The size of the hash to use for generating random seeds. Default 256
- --@OptionalArg boolean noPadding: Set to true to not add padding
- --@OptionalArg boolean noScramble: Set to true to not scramble the file
- function encryptFile(path,pass,hashSize,noPadding,noScramble)
- hashSize = math.floor(math.max(0,math.min(256*256,tonumber(hashSize) or 256))) --Ensuring valid integer value
- if not path or not fs.exists(path) then
- local f = selectFile()
- if not f then return end
- path = f
- end
- if not pass then
- local password,confirmPass = "", ""
- repeat
- term.clear()
- password = enterPassword(1,1,16)
- term.setCursorPos(1,3)
- print("Confirm password:")
- confirmPass = enterPassword(1,4,16)
- term.clear()
- if password ~= confirmPass then
- term.setCursorPos(1,6)
- print("Passwords do not match!")
- end
- until password == confirmPass and #password >= 1
- pass = password
- term.setCursorPos(1,1)
- term.clear()
- end
- local file = fs.open(path,"r")
- local data = file.readAll()
- file.close()
- local encryptedData = encryptString(data,pass,hashSize,noPadding,noScramble)
- local tempPath = fs.combine("temp/",path)
- local file = fs.open(tempPath,"w+")
- file.write(magicNumbers)
- file.write(string.char(math.floor(hashSize/256)))
- file.write(string.char(hashSize%256))
- local flags = 0
- if noPadding then flags = flags + 1 end
- if noScramble then flags = flags + 2 end
- file.write(string.char(flags))
- file.write(encryptedData)
- file.close()
- fs.delete(path)
- fs.copy(tempPath,path)
- fs.delete("temp")
- end
- --Decrypts a file
- --@OptionalArg string path: the path to decrypt
- --@OptionalArg string pass: The password to decrypt with
- --@OptionalArg boolean replace: Whether the file should be replaced or not
- --@Returns string data, boolean wasEncrypted, string path: The decrypted data, whether it was encrypted or not, and the path
- function decryptFile(path,pass,replace)
- if not path or not fs.exists(path) then
- local f = selectFile()
- if not f then return end
- path = f
- end
- if not pass then
- local password,confirmPass = "", ""
- repeat
- term.clear()
- password = enterPassword(1,1,16)
- term.setCursorPos(1,3)
- print("Confirm password:")
- confirmPass = enterPassword(1,4,16)
- term.clear()
- if password ~= confirmPass then
- term.setCursorPos(1,6)
- print("Passwords do not match!")
- end
- until password == confirmPass and #password >= 1
- pass = password
- term.setCursorPos(1,1)
- term.clear()
- end
- local file = fs.open(path,"r")
- local data = file.readAll()
- file.close()
- if string.sub(data,1,2) ~= magicNumbers then
- print("Not an encrypted file")
- return data, false, path end
- local hashSize = string.byte(string.sub(data,3,3))
- hashSize = hashSize*256+ string.byte(string.sub(data,4,4))
- local flags = string.byte(string.sub(data,5,5))
- local decryptedData = decryptString(string.sub(data,6,-1),pass,hashSize,flags%2 == 1, math.floor(flags/2)%2 == 1)
- if replace then
- local tempPath = fs.combine("temp/",path)
- local file = fs.open(tempPath,"w+")
- file.write(decryptedData)
- file.close()
- fs.delete(path)
- fs.copy(tempPath,path)
- fs.delete("temp")
- end
- return decryptedData, true, path
- end
- --Runs an encrypted file
- --@OptionalArg string path: The file to run
- --@OptionalArg string pass: The password to use to decrypt
- --@Args any args: The args to run with the file
- function runEncrypted(path,pass,...)
- local args = {...}
- local decryptedData, wasEncrypted, path = decryptFile(path,pass,false)
- if not decryptedData then return end
- if wasEncrypted then
- local tempPath = fs.combine("temp/",path)
- local file = fs.open(tempPath,"w+")
- file.write(decryptedData)
- file.close()
- local success = pcall(function() if not os.run({},tempPath,unpack(args)) then error() end end)
- fs.delete("temp")
- if not success then error() end
- else
- os.run({},path,unpack(args))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment