Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- FileHider
- -- By KillaVanilla
- -- CLI by randomdude999
- local args = {...}
- if #args == 0 then
- print("FileHiderCLI - A command-line file encryption tool")
- print("Usage: "..fs.getName(shell.getRunningProgram()).." <file> <encrypt | decrypt> <key> <init. vector> <output>")
- return
- end
- if not fs.exists(args[1]) then
- error("File "..args[1].." does not exist",0)
- end
- -- Download the AES API if we don't already have it:
- if not fs.exists("/.apis/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("/.apis/AES", "w")
- apiHandle.write(apiData)
- apiHandle.close()
- else
- error("Could not retrieve AES API.\nMake sure Internet connection is available.")
- end
- end
- os.loadAPI("AES")
- local function getParam(param)
- local key = {}
- local keyStr = args[param]
- while true do
- if #keyStr > 16 then
- error("Key too long. (Must be smaller than or equal to 16)",0)
- 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 key, keyStr = getParam(3)
- local iv, ivStr = getParam(4)
- local outputFile = args[5]
- 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()
- if args[2] == "encrypt" 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"))
- local fileName = args[5]
- local bytes = AES.encrypt_bytestream(fData, key, iv)
- local fHandle = fs.open(fileName, "wb")
- for byte=1, #bytes do
- fHandle.write(bytes[byte])
- end
- fHandle.close()
- elseif args[2] == "decrypt" then
- local bytes = AES.decrypt_bytestream(fData, key, iv)
- if string.char( bytes[1], bytes[2], bytes[3], bytes[4] ) == "DATA" then
- local fileName = args[5]
- local fHandle = fs.open(fileName, "wb")
- for byte=5, #bytes do
- fHandle.write(bytes[byte])
- end
- fHandle.close()
- else
- error("Decryption failed. Is the key / init. vector incorrect, or was the file encrypted with this tool?\n(Technical info: First 4 bytes are not equal to \"DATA\")",0)
- end
- else
- error("2nd parameter should be either encrypt or decrypt.")
- end
Advertisement
Add Comment
Please, Sign In to add comment