-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
269 lines (224 loc) · 9.04 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from pathlib import Path
import logging
import concurrent.futures
import sys
import getopt
BLOCK_SIZE = 16
BLOCK_MULTIPLIER = 100
global ALPHABET
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.1234567890"
maxWorker = 10
helpText = '''Usage: python main.py [args] fileExtension fileExtension ...
Arguments: -r : deletes en- or decrypted files after use
-m : set mode: 1- encrypt
2- decrypt
3- deletes encrypted files
4- deletes files with extensions if empty everything except .enc and .py
5- encrypt one file
-p : sets password no space allowed
-w : sets max number of threads
'''
version = "1.1d"
def generateKey(length, key):
retKey = str()
for i in range(length):
retKey += key[i % len(key)]
return retKey
def vencrypt(msg, key):
key = generateKey(len(msg), key)
ciphertext = "E"
for index, char in enumerate(msg):
ciphertext += ALPHABET[(ALPHABET.find(key[index]) + ALPHABET.find(char)) % len(ALPHABET)]
return ciphertext
def vdecrypt(ciphertext, key):
key = generateKey(len(ciphertext), key)
msg = str()
ciphertext = ciphertext[1:]
for index, char in enumerate(ciphertext):
msg += ALPHABET[(ALPHABET.find(char) - ALPHABET.find(key[index])) % len(ALPHABET)]
return msg
def encryptFile(filePath, password):
try:
logging.info("Started encoding: " + filePath.resolve().as_posix())
hashObj = SHA256.new(password.encode('utf-8'))
hkey = hashObj.digest()
encryptPath = Path(filePath.parent.resolve().as_posix() + "/" + vencrypt(filePath.name, password) + ".enc")
if encryptPath.exists():
encryptPath.unlink()
with open(filePath, "rb") as input_file, encryptPath.open("ab") as output_file:
content = b''
content = input_file.read(BLOCK_SIZE*BLOCK_MULTIPLIER)
while content != b'':
output_file.write(encrypt(hkey, content))
content = input_file.read(BLOCK_SIZE*BLOCK_MULTIPLIER)
logging.info("Encoded " + filePath.resolve().as_posix())
logging.info("To " +encryptPath.resolve().as_posix())
except Exception as e:
print(e)
def decryptFile(filePath, password):
logging.info("Started decoding: " + filePath.resolve().as_posix())
try:
hashObj = SHA256.new(password.encode('utf-8'))
hkey = hashObj.digest()
decryptFilePath = Path(filePath.parent.resolve().as_posix() + "/" + vdecrypt(filePath.name, password)[:-4])
if decryptFilePath.exists():
decryptFilePath.unlink()
with filePath.open("rb") as input_file, decryptFilePath.open("ab") as output_file:
values = input_file.read(BLOCK_SIZE*BLOCK_MULTIPLIER)
while values != b'':
output_file.write(decrypt(hkey, values))
values = input_file.read(BLOCK_SIZE*BLOCK_MULTIPLIER)
logging.info("Decoded: " + filePath.resolve().as_posix()[:-4])
logging.info("TO: " + decryptFilePath.resolve().as_posix() )
except Exception as e:
print(e)
def pad(msg, BLOCK_SIZE, PAD):
return msg + PAD * ((BLOCK_SIZE - len(msg) % BLOCK_SIZE) % BLOCK_SIZE)
def encrypt(key, msg):
PAD = b'\0'
cipher = AES.new(key, AES.MODE_ECB)
result = cipher.encrypt(pad(msg, BLOCK_SIZE, PAD))
return result
def decrypt(key, msg):
PAD = b'\0'
decipher = AES.new(key, AES.MODE_ECB)
pt = decipher.decrypt(msg)
for i in range(len(pt)-1, -1, -1):
if pt[i] == PAD:
pt = pt[:i]
else:
break
return pt
def getMaxLen(arr):
maxLen = 0
for elem in arr:
if len(elem) > maxLen:
maxLen = len(elem)
return maxLen
def getTargetFiles(fileExtension):
fileExtensions = []
if len(fileExtension) == 0:
fileExtensions.append("*")
else:
for Extension in fileExtension:
fileExtensionFormatted = "*."
for char in Extension:
fileExtensionFormatted += "[" + char + "]"
fileExtensions.append(fileExtensionFormatted)
return fileExtensions
def generateEncryptThreads(fileExtensions, password, removeFiles, path):
fileExtensionFormatted = getTargetFiles(fileExtensions)
filePaths = []
for fileExtension in fileExtensionFormatted:
filePaths = filePaths + list(Path(path).rglob(fileExtension))
with concurrent.futures.ThreadPoolExecutor(max_workers=maxWorker) as executor:
for filePath in filePaths:
executor.submit(encryptFile, *(filePath, password))
if removeFiles:
for filePath in filePaths:
filePath.unlink()
def generateDecryptThreads(password, removeFiles, path):
filePaths = list(Path(path).rglob("*.[eE][nN][cC]"))
with concurrent.futures.ThreadPoolExecutor(max_workers=maxWorker) as executor:
for filePath in filePaths:
executor.submit(decryptFile, *(filePath, password))
if removeFiles:
for filePath in filePaths:
filePath.unlink()
def removeEncryptedFiles(path):
filePaths = list(Path(path).rglob("*.[eE][nN][cC]"))
for filePath in filePaths:
filePath.unlink()
def removeExFiles(fileExtensions, path):
fileExtensionFormatted = getTargetFiles(fileExtensions)
filePaths = []
for fileExtension in fileExtensionFormatted:
filePaths = filePaths + list(Path(path).rglob(fileExtension))
for filePath in filePaths:
filePath.unlink()
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
if len(sys.argv[1:]) < 1:
print("(1) - encrypt\n(2) - decrypt\n(3) - remove .enc files\n(4) - remove other files")
mode = int(input("---> "))
password = str()
passwordConfirm = str()
if mode == 1 or mode == 2:
password = input("password: ")
passwordConfirm = input("confirm password: ")
if password != passwordConfirm:
logging.error("Passwords not matching")
exit()
if mode == 1:
fileExtensions = input("Enter file extensions (jpg png ...): ").split()
removeFiles = input("Remove unencrypted files afterwards(Y): ")
if removeFiles[0].upper() == 'Y':
removeFiles = True
else:
removeFiles = False
path = input("Select folder to encrypt (\".\" for current dir): ")
generateEncryptThreads(fileExtensions, password, removeFiles, path)
elif mode == 2:
removeFiles = input("Remove encrypted files afterwards(Y): ")
if removeFiles[0].upper() == 'Y':
removeFiles = True
else:
removeFiles = False
path = input("Select folder to decrypt (\".\" for current dir): ")
generateDecryptThreads(password, removeFiles, path)
elif mode == 3:
path = input("Select folder for removal (\".\" for current dir): ")
removeEncryptedFiles(path)
elif mode == 4:
fileExtensions = input("Enter file extensions (jpg png ...): ").split()
path = input("Select folder for removal (\".\" for current dir): ")
removeExFiles(fileExtensions, path)
else:
removeFiles = False
password = ""
mode = 0
opts, args = getopt.getopt(sys.argv[1:], "rm:p:w:vd:h")
for opt, arg in opts:
if opt == '-r':
removeFiles = True
elif opt == '-m':
mode = int(arg)
elif opt == '-w':
maxWorker = int(arg)
elif opt == '-p':
password = arg
elif opt == '-d':
path = arg
elif opt == '-h':
print(helpText)
exit()
if mode == 0 or (password == "" and mode in (1,2,5)):
print("Missing arguments!\nType -h as argument to get help Page.")
exit()
if mode == 1:
generateEncryptThreads(args, password, removeFiles, path)
elif mode == 2:
generateDecryptThreads(password, removeFiles, path)
elif mode == 3:
removeEncryptedFiles()
elif mode == 4:
# print(args)
if args == []:
filePaths = list(Path(path).rglob("*.*"))
removePaths = list()
for index, filePath in enumerate(filePaths):
if not ".enc" in filePath.name and not ".py" in filePath.name:
removePaths.append(filePath)
try:
for removeFilePath in removePaths:
removeFilePath.unlink()
except Exception as e:
print(e)
else:
removeExFiles(args)
elif mode == 5:
encryptFile(Path(args), password)