View difference between Paste ID: wQ1aUnpU and 53QyFfHt
SHOW: | | - or go back to the newest paste.
1
-- FileHider
2
-- By KillaVanilla
3
-- CLI by randomdude999
4
5
local args = {...}
6
7-
	print("FileHider - A file encryption tool")
7+
8-
	print("Usage: "..fs.getName(shell.getRunningProgram()).." [target]")
8+
	print("FileHiderCLI - A command-line file encryption tool")
9
	print("Usage: "..fs.getName(shell.getRunningProgram()).." <file> <encrypt | decrypt> <key> <init. vector> <output>")
10
	return
11
end
12
13-
	print("Target file "..args[1].." does not exist!")
13+
14
	error("File "..args[1].." does not exist",0)
15
end
16
17
-- Download the AES API if we don't already have it:
18
19-
if not fs.exists("AES") then
19+
if not fs.exists("/.apis/AES") then
20
	local webHandle = http.get("http://pastebin.com/raw.php?i=rCYDnCxn")
21
	if webHandle then
22
		local apiData = webHandle.readAll()
23
		webHandle.close()
24-
		local apiHandle = fs.open("AES", "w")
24+
		local apiHandle = fs.open("/.apis/AES", "w")
25
		apiHandle.write(apiData)
26
		apiHandle.close()
27
	else
28-
		print("Could not retrieve AES API!")
28+
		error("Could not retrieve AES API.\nMake sure Internet connection is available.")
29-
		print("Ensure that the HTTP API is enabled.")
29+
30-
		return
30+
31
32
os.loadAPI("AES")
33
34
local function getParam(param)
35
	local key = {}
36-
local function selectParameter(parameterName)
36+
	local keyStr = args[param]
37-
	local x,y = term.getCursorPos()
37+
38
		if #keyStr > 16 then
39-
	local keyStr = ""
39+
			error("Key too long. (Must be smaller than or equal to 16)",0)
40
		else
41-
		term.setCursorPos(1,y)
41+
42-
		term.clearLine()
42+
43-
		term.setCursorPos(1,y+1)
43+
44-
		term.clearLine()
44+
45-
		term.setCursorPos(1,y)
45+
46-
		print("Please type in the "..parameterName..":")
46+
47-
		write(">")
47+
48-
		keyStr = read()
48+
49
		end
50-
			print("Please type in a valid "..parameterName..".")
50+
51-
			os.sleep(1.5)
51+
52-
			term.setCursorPos(1, y+2)
52+
53-
			term.clearLine()
53+
54
local key, keyStr = getParam(3)
55
local iv, ivStr = getParam(4)
56
local outputFile = args[5]
57
local fData = {}
58
local blocks = {}
59
local blockNum = 1
60
61
local fHandle = fs.open(args[1], "rb")
62
while true do
63
	local byte = fHandle.read()
64
	if byte then
65
		if byte > 0xFF then
66
			local t1 = bit.band(byte, 0x000000FF)
67
			local t2 = bit.brshift(bit.band(byte, 0x0000FF00), 8)
68-
local doDecrypt = false
68+
69-
local sX, sY = term.getSize()
69+
70-
local key, keyStr = selectParameter("encryption key")
70+
71-
local iv, ivStr = selectParameter("initalization vector")
71+
72
			if t3 ~= 0 then
73
				table.insert(fData, t3)
74
			end
75
			if t4 ~= 0 then
76
				table.insert(fData, t4)
77
			end
78
		else
79
			table.insert(fData, byte)
80
		end
81
	else
82
		break
83
	end
84
end
85
fHandle.close()
86
87
if args[2] == "encrypt" then
88
	-- Insert the signature:
89
	table.insert(fData, 1, string.byte("A"))
90
	table.insert(fData, 1, string.byte("T"))
91
	table.insert(fData, 1, string.byte("A"))
92
	table.insert(fData, 1, string.byte("D"))
93
	local fileName = args[5]
94
	local bytes = AES.encrypt_bytestream(fData, key, iv)
95
	local fHandle = fs.open(fileName, "wb")
96
	for byte=1, #bytes do
97
		fHandle.write(bytes[byte])
98
	end
99
	fHandle.close()
100
elseif args[2] == "decrypt" then
101
	local bytes = AES.decrypt_bytestream(fData, key, iv)
102
	if string.char( bytes[1], bytes[2], bytes[3], bytes[4] ) == "DATA" then
103-
	term.clear()
103+
		local fileName = args[5]
104-
	term.setCursorPos(1,1)
104+
105-
	print("Options")
105+
106-
	print("Using key: "..keyStr)
106+
107-
	print("Using IV: "..ivStr)
107+
108-
	print("Size: "..#fData.." bytes, "..math.ceil(#fData/16).." blocks")
108+
109-
	print(string.rep("=", sX))
109+
110-
	print("1 - Encrypt")
110+
		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)
111-
	print("2 - Decrypt")
111+
112-
	print("3 - Reselect Key")
112+
113-
	print("4 - Reselect IV")
113+
	error("2nd parameter should be either encrypt or decrypt.")
114-
	print("5 - Exit")
114+