-
Notifications
You must be signed in to change notification settings - Fork 1
/
Crypt.py
192 lines (149 loc) · 5.91 KB
/
Crypt.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
import ray
import time
import datetime
import os.path
from cryptography.fernet import Fernet
ray.init()
#find difference between list1 and list2...used here for finding newly connected or disconnected drive
def difference(list1, list2):
list_difference = [item for item in list1 if item not in list2]
return list_difference
#def driveConnected():
# print("New drive connected")
#def driveDisconnected():
# print("Drive disconnected")
#load key from current directory
def load_key():
return open("key.key", "rb").read()
#calling encryption
def call_encryption(path):
number_of_files = 1
progress = 1
result_id = []
skipped = []
key = load_key()
print("encrypting files...")
for dirpath, dirnames, files in os.walk(path):
for file in files:
number_of_files += 1
result_id.append(encryption.remote(dirpath + "\\" + file, key)) #encryption
while(result_id):
done_id, result_id = ray.wait(result_id) #recieve object ID for completed project
done_id= ray.get(done_id)
progress += done_id[0][0]/number_of_files*100 #track progress
print(int(progress), '% completed', end = '\r')
if done_id[0][1] != '':
skipped.append(done_id[0][1])
print("\nencrypted", number_of_files - len(skipped), "files")
if(len(skipped) > 0):
print("skipped", len(skipped), "files. File access denied")
print("Encryption completed in ", end = '')
return (datetime.datetime.now(), number_of_files, "encrypted", len(skipped), "skipped")
#calling decryption
def call_decryption(path):
number_of_files = 1
skipped = []
result_id = []
progress = 1
key = load_key()
print("decrypting files...")
for dirpath, dirnames, files in os.walk(path):
for file in files:
number_of_files += 1
result_id.append(decryption.remote(dirpath + "\\" + file, key)) #decryption
while(result_id):
done_id, result_id = ray.wait(result_id) #recieve object ID for completed project
done_id = ray.get(done_id)
progress += done_id[0][0]/number_of_files*100 #track progress
print(int(progress), '% completed', end = '\r')
if done_id[0][1] != '':
skipped.append(done_id[0][1])
print("\ndecrypted", number_of_files - len(skipped), "files")
if(len(skipped) > 0):
print("skipped", len(skipped), "files. Files were either invalid of modified previously")
print("Decryption completed in ", end='')
return (datetime.datetime.now(), number_of_files, "decrypted", len(skipped), "skipped")
#encryption
@ray.remote
def encryption(file_location, key):
f = Fernet(key)
with open(file_location, "rb") as data:
file_data = data.read() # read all file data
try:
encrypted_data = f.encrypt(file_data) # encrypt data
except:
return (1, file_location)
else:
# write the encrypted file
with open(file_location, "wb") as data:
data.write(encrypted_data)
return (1, '')
#decryption
@ray.remote
def decryption(file_location, key):
f = Fernet(key)
with open(file_location, "rb") as data:
encrypted_data = data.read() # read the encrypted data
try:
decrypted_data = f.decrypt(encrypted_data) # decrypt data
except:
return (1, file_location)
else:
# write the original file
with open(file_location, "wb") as data:
data.write(decrypted_data)
return (1, '')
#driver code
if __name__ == '__main__':
#list of possible drives
driveList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)] #connected drives
crypt = False
controller = False #to avoid encrpting an already encrypted drive
print("connect a USB drive to start the process")
while True:
#finding newly connected or disconnected drives
uncheckedDrives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)]
x = difference(uncheckedDrives, drives)
#if drive is connected
if x:
print(x[0], "drive connected")
# driveConnected()
start = datetime.datetime.now()
for i in x:
path = i + "\\" #path of the drive
#encrypt files
if crypt == False and controller == False:
log = call_encryption(path) #encryption process
finish = datetime.datetime.now()
print(finish - start)
crypt = True
#decrypt files
elif crypt == True and controller == True:
log = call_decryption(path) #decryption process
finish = datetime.datetime.now()
print(finish - start)
crypt = False
x = difference(drives, uncheckedDrives)
#if drive is disconnected
if x:
if controller:
controller = False
else:
controller = True
print(str(x[0]) + " drive removed")
# driveDisconnected()
drives = ['%s:' % d for d in driveList if os.path.exists('%s:' % d)]
time.sleep(1)
#roadmap:
#solve multiple encryption problem
#add manual control
#add support for argparser to run application on cli
#add password protection
#support encryption of large files
#create a dedicated website
#containerize the app using docker
#create and distribute executables and docker images
#add support for log files
#add RSA encryption
#(maybe) add GUI