-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_encrypter.py
225 lines (193 loc) · 7.24 KB
/
file_encrypter.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
# from Crypto import Random
# from Crypto.Cipher import AES
# import os
# import os.path
# from os import listdir
# from os.path import isfile, join
# class Encrypter :
# def __init__(self,key):
# self.key=key
# def pad (self,s):
# return s+b"\0" * (AES.block_size - len(s) % AES.block_size)
# def encrypt (self , message , key , key_size=256):
# message= self.pad(message)
# iv = Random.new().read(AES.block_size)
# cipher = AES.new(key , AES.MODE_CBC, iv)
# return iv + cipher.encrypt(message)
# def encrypt_file(self , file_name):
# with open(file_name,'rb') as fo:
# plainText = fo.read
# enc = self.encrypt(plainText,self.key)
# with open(file_name + ".enc","wb") as fo:
# fo.write(enc)
# os.remove(file_name)
# def decrypt(self,cipherText, key):
# iv = cipherText[:AES.block_size]
# cipher = AES.neew(key, AES.MODE_CBC, iv)
# plainText = cipher.decrypt(cipherText[AES.block_size:])
# return plainText.rstrp(b"\0")
# def decrypt_file(self,file_name):
# with open(file_name, 'rb') as fo:
# cipherText = fo.read()
# dec = self.decrypt(cipherText, self.key)
# with open(file_name[-4], 'wb') as fo:
# fo.write(dec)
# os.remove(file_name)
# def getAllFiles(self):
# dir_path = os.path.dirname(os.path.realpath(__file__))
# dirs = []
# for dirName, subDirList, filelist in os.walk(dir_path):
# for fname in filelist:
# if (fname!= 'aes.py' and fname!='data.text.enc'):
# dirs.append(dirName+"\\"+fname)
# return dirs
# def encrypt_all_files(self):
# dirs = getAllFiles()
# for file_name in dirs:
# self.encrypt_file(file_name)
# def decrypt_all_files(self):
# dirs = getAllFiles()
# for file_name in dirs:
# self.decrypt_file(file_name)
# key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e'
# enc = Encryptor(key)
# clear = lambda: os.system('cls')
# if os.path.isfile('data.txt.enc'):
# while True:
# password = str(input("Enter password: "))
# enc.decrypt_file("data.txt.enc")
# p = ''
# with open("data.txt", "r") as f:
# p = f.readlines()
# if p[0] == password:
# enc.encrypt_file("data.txt")
# break
# while True:
# clear()
# choice = int(input(
# "1. Press '1' to encrypt file.\n2. Press '2' to decrypt file.\n3. Press '3' to Encrypt all files in the directory.\n4. Press '4' to decrypt all files in the directory.\n5. Press '5' to exit.\n"))
# clear()
# if choice == 1:
# enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
# elif choice == 2:
# enc.decrypt_file(str(input("Enter name of file to decrypt: ")))
# elif choice == 3:
# enc.encrypt_all_files()
# elif choice == 4:
# enc.decrypt_all_files()
# elif choice == 5:
# exit()
# else:
# print("Please select a valid option!")
# else:
# while True:
# clear()
# password = str(input("Setting up stuff. Enter a password that will be used for decryption: "))
# repassword = str(input("Confirm password: "))
# if password == repassword:
# break
# else:
# print("Passwords Mismatched!")
# f = open("data.txt", "w+")
# f.write(password)
# f.close()
# enc.encrypt_file("data.txt")
# print("Please restart the program to complete the setup")
# time.sleep(15)
from Crypto import Random
from Crypto.Cipher import AES
import os
import os.path
from os import listdir
from os.path import isfile, join
import time
class Encryptor:
def __init__(self, key):
self.key = key
def pad(self, s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(self, message, key, key_size=256):
message = self.pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def encrypt_file(self, file_name):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = self.encrypt(plaintext, self.key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
os.remove(file_name)
def decrypt(self, ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
def decrypt_file(self, file_name):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = self.decrypt(ciphertext, self.key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
os.remove(file_name)
def getAllFiles(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
dirs = []
for dirName, subdirList, fileList in os.walk(dir_path):
for fname in fileList:
if (fname != 'script.py' and fname != 'data.txt.enc'):
dirs.append(dirName + "\\" + fname)
return dirs
def encrypt_all_files(self):
dirs = self.getAllFiles()
for file_name in dirs:
self.encrypt_file(file_name)
def decrypt_all_files(self):
dirs = self.getAllFiles()
for file_name in dirs:
self.decrypt_file(file_name)
key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e'
enc = Encryptor(key)
clear = lambda: os.system('cls')
if os.path.isfile('data.txt.enc'):
while True:
password = str(input("Enter password: "))
enc.decrypt_file("data.txt.enc")
p = ''
with open("data.txt", "r") as f:
p = f.readlines()
if p[0] == password:
enc.encrypt_file("data.txt")
break
while True:
clear()
choice = int(input(
"1. Press '1' to encrypt file.\n2. Press '2' to decrypt file.\n3. Press '3' to Encrypt all files in the directory.\n4. Press '4' to decrypt all files in the directory.\n5. Press '5' to exit.\n"))
clear()
if choice == 1:
enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
elif choice == 2:
enc.decrypt_file(str(input("Enter name of file to decrypt: ")))
elif choice == 3:
enc.encrypt_all_files()
elif choice == 4:
enc.decrypt_all_files()
elif choice == 5:
exit()
else:
print("Please select a valid option!")
else:
while True:
clear()
password = str(input("Setting up stuff. Enter a password that will be used for decryption: "))
repassword = str(input("Confirm password: "))
if password == repassword:
break
else:
print("Passwords Mismatched!")
f = open("data.txt", "w+")
f.write(password)
f.close()
enc.encrypt_file("data.txt")
print("Please restart the program to complete the setup")
time.sleep(15)