-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (55 loc) · 3.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import keyModule, encryptModule, os, json, glob, sys, getopt
#creating json config file
config = {
"REMOVE_ENCRYPTED": "True",
"REMOVE_DECRYPTED": "True"
}
#loading json config file
with open('config.json', 'w') as configFile:
json.dump(config, configFile)
configFile.close()
#checking if key file is present, else creating a key file
if os.path.isfile('key.key') == True:
KEY = keyModule.openkey()
print('Key file found.')
elif os.path.isfile('/key.key') == False:
print('Key file not found. Creating new key.')
keyModule.createkey()
KEY = keyModule.openkey()
def encryptDir(KEY, filesToEncrypt):
for file in filesToEncrypt:
encryptModule.encryptFile(KEY, arg + file)
def decryptDir(KEY, filesToDecrypt):
for file in filesToDecrypt:
encryptModule.decryptFile(KEY, file)
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], 'o:v', ['encdir=', 'decdir=', 'encfile=', 'decfile=', 'key==', 'dirkey==', 'help=='])
except getopt.GetoptError as error:
print(error)
sys.exit(2)
for opt, arg in opts:
if opt == '--encdir':
files = [ f for f in os.listdir(arg) if os.path.isfile(os.path.join(arg,f)) ]
encryptDir(KEY, files)
elif opt == '--decdir':
decryptDir(KEY, glob.glob(arg+'*.encrypted'))
elif opt =='--encfile':
encryptModule.encryptFile(KEY, arg)
elif opt == '--decfile':
encryptModule.decryptFile(KEY, arg)
elif opt == '--key':
KEY = bytes(arg)
elif opt == '--dirkey':
try:
file = open(arg, 'rb')
KEY = file.read()
except ValueError:
print('Key file not found.')
# elif opt or arg == '--help':
# print("List of available parameters:")
# print("--help returns list of available parameters")
# print("--encfile <path to file> encodes a file and returns the key used to encode")
# print("--decfile <path to file + \".encrypted\"> decodes a file if either the key file is present in current dir, or if the key is specified via --key or if the key path is specified via --keydir")
# print("--encdir <path to directory> encodes all files inside the specified directory and returns the key")
# print("--decdir <path to directory> decodes all *.encrypted files inside specified directory if either the key file is present in current dir, or if the key is specified via --key or if the key path is specified via --keydir")