Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- FileHider
- -- By KillaVanilla
- local args = {...}
- if #args == 0 then
- print("FileHider - A file encryption tool")
- print("Usage: "..fs.getName(shell.getRunningProgram()).." [target]")
- return
- end
- if not fs.exists(args[1]) then
- print("Target file "..args[1].." does not exist!")
- return
- end
- -- Download the AES API if we don't already have it:
- if not fs.exists("AES") then
- local webHandle = http.get("http://pastebin.com/raw.php?i=rCYDnCxn")
- if webHandle then
- local apiData = webHandle.readAll()
- webHandle.close()
- local apiHandle = fs.open("AES", "w")
- apiHandle.write(apiData)
- apiHandle.close()
- else
- print("Could not retrieve AES API!")
- print("Ensure that the HTTP API is enabled.")
- return
- end
- end
- os.loadAPI("AES")
- local function selectParameter(parameterName)
- local x,y = term.getCursorPos()
- local key = {}
- local keyStr = ""
- while true do
- term.setCursorPos(1,y)
- term.clearLine()
- term.setCursorPos(1,y+1)
- term.clearLine()
- term.setCursorPos(1,y)
- print("Please type in the "..parameterName..":")
- write(">")
- keyStr = read()
- if #keyStr > 16 then
- print("Please type in a valid "..parameterName..".")
- os.sleep(1.5)
- term.setCursorPos(1, y+2)
- term.clearLine()
- else
- for i=1, 16 do
- if i > #keyStr then
- key[i] = 0
- else
- key[i] = string.byte(keyStr, i, i)
- end
- end
- break
- end
- end
- return key, keyStr
- end
- local doDecrypt = false
- local sX, sY = term.getSize()
- local key, keyStr = selectParameter("encryption key")
- local iv, ivStr = selectParameter("initalization vector")
- local fData = {}
- local blocks = {}
- local blockNum = 1
- local fHandle = fs.open(args[1], "rb")
- while true do
- local byte = fHandle.read()
- if byte then
- if byte > 0xFF then
- local t1 = bit.band(byte, 0x000000FF)
- local t2 = bit.brshift(bit.band(byte, 0x0000FF00), 8)
- local t3 = bit.brshift(bit.band(byte, 0x00FF0000), 16)
- local t4 = bit.brshift(bit.band(byte, 0xFF000000), 24)
- table.insert(fData, t1)
- table.insert(fData, t2)
- if t3 ~= 0 then
- table.insert(fData, t3)
- end
- if t4 ~= 0 then
- table.insert(fData, t4)
- end
- else
- table.insert(fData, byte)
- end
- else
- break
- end
- end
- fHandle.close()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Options")
- print("Using key: "..keyStr)
- print("Using IV: "..ivStr)
- print("Size: "..#fData.." bytes, "..math.ceil(#fData/16).." blocks")
- print(string.rep("=", sX))
- print("1 - Encrypt")
- print("2 - Decrypt")
- print("3 - Reselect Key")
- print("4 - Reselect IV")
- print("5 - Exit")
- print("Use your keyboard to make selections.")
- local event, key = os.pullEvent("key")
- term.clear()
- term.setCursorPos(1,1)
- if key == 2 then
- -- Insert the signature:
- table.insert(fData, 1, string.byte("A"))
- table.insert(fData, 1, string.byte("T"))
- table.insert(fData, 1, string.byte("A"))
- table.insert(fData, 1, string.byte("D"))
- break
- elseif key == 3 then
- doDecrypt = true
- break
- elseif key == 4 then
- key, keyStr = selectParameter("encryption key")
- elseif key == 5 then
- iv, ivStr = selectParameter("initalization vector")
- elseif key == 6 then
- return
- end
- end
- --[[
- for i=1, #fData, 16 do
- blocks[blockNum] = {}
- for j=1, 16 do
- blocks[blockNum][j] = fData[i+(j-1)]
- end
- if #blocks[blockNum] < 16 then
- for j=#blocks[blockNum], 16 do
- table.insert(blocks[blockNum], 0)
- end
- end
- blockNum = blockNum+1
- end
- ]]
- if not doDecrypt then
- print("Please type in the output file path:")
- write(">")
- local fileName = read()
- print("Encrypting file: "..args[1])
- local bytes = AES.encrypt_bytestream(fData, key, iv)
- --[[
- local x,y = term.getCursorPos()
- for i=1, #blocks do
- term.setCursorPos(x,y)
- term.clearLine()
- term.write("Block: "..i.." of "..#blocks..". ("..math.floor( (i/#blocks)*100 ).."%)")
- blocks[i] = AES.encrypt_block(blocks[i], key)
- os.queueEvent("i_can_fly") -- Credit to MCUniverse, who's primeanac program led to this faster way of yielding.
- os.pullEvent()
- end
- term.setCursorPos(1,y+1)
- ]]
- local fHandle = fs.open(fileName, "wb")
- for byte=1, #bytes do
- fHandle.write(bytes[byte])
- end
- fHandle.close()
- print("Done. Encrypted data saved to:")
- print(fileName)
- else
- print("Decrypting file: "..args[1])
- local bytes = AES.decrypt_bytestream(fData, key, iv)
- --[[
- local x,y = term.getCursorPos()
- for i=1, #blocks do
- term.setCursorPos(x,y)
- term.clearLine()
- term.write("Block: "..i.." of "..#blocks..". ("..math.floor( (i/#blocks)*100 ).."%)")
- blocks[i] = AES.decrypt_block(blocks[i], key)
- os.queueEvent("i_can_fly")
- os.pullEvent()
- end
- term.setCursorPos(1,y+1)
- ]]
- if string.char( bytes[1], bytes[2], bytes[3], bytes[4] ) == "DATA" then
- print("Decryption successful.")
- print("Please type in the output file path:")
- write(">")
- local fileName = read()
- local fHandle = fs.open(fileName, "wb")
- for byte=5, #bytes do
- fHandle.write(bytes[byte])
- end
- --[[
- for i=1, #blocks do
- if i==1 then
- for j=5, 16 do
- fHandle.write(blocks[i][j])
- end
- else
- for j=1, 16 do
- fHandle.write(blocks[i][j])
- end
- end
- end
- ]]
- fHandle.close()
- print("Data saved to:")
- print(fileName)
- else
- print("Decryption failed.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement