Skip to content

Commit dc91583

Browse files
Add files via upload
1 parent 2017245 commit dc91583

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

script.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/python3
2+
3+
from Crypto import Random
4+
from Crypto.Cipher import AES
5+
import os
6+
import os.path
7+
from os import listdir
8+
from os.path import isfile, join
9+
10+
11+
class Encryptor:
12+
def __init__(self, key):
13+
self.key = key
14+
15+
def pad(self, s):
16+
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
17+
18+
def encrypt(self, message, key, key_size=256):
19+
message = self.pad(message)
20+
iv = Random.new().read(AES.block_size)
21+
cipher = AES.new(key, AES.MODE_CBC, iv)
22+
return iv + cipher.encrypt(message)
23+
24+
def encrypt_file(self, file_name):
25+
with open(file_name, 'rb') as fo:
26+
plaintext = fo.read()
27+
enc = self.encrypt(plaintext, self.key)
28+
with open(file_name + ".enc", 'wb') as fo:
29+
fo.write(enc)
30+
os.remove(file_name)
31+
32+
def decrypt(self, ciphertext, key):
33+
iv = ciphertext[:AES.block_size]
34+
cipher = AES.new(key, AES.MODE_CBC, iv)
35+
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
36+
return plaintext.rstrip(b"\0")
37+
38+
def decrypt_file(self, file_name):
39+
with open(file_name, 'rb') as fo:
40+
ciphertext = fo.read()
41+
dec = self.decrypt(ciphertext, self.key)
42+
with open(file_name[:-4], 'wb') as fo:
43+
fo.write(dec)
44+
os.remove(file_name)
45+
46+
def getAllFiles(self):
47+
dir_path = os.path.dirname(os.path.realpath(__file__))
48+
dirs = []
49+
for dirName, subdirList, fileList in os.walk(dir_path):
50+
for fname in fileList:
51+
if (fname != 'script.py' and fname != 'password.txt.enc'):
52+
dirs.append(dirName + "\\" + fname)
53+
return dirs
54+
55+
def encrypt_all_files(self):
56+
dirs = self.getAllFiles()
57+
for file_name in dirs:
58+
self.encrypt_file(file_name)
59+
60+
def decrypt_all_files(self):
61+
dirs = self.getAllFiles()
62+
for file_name in dirs:
63+
self.decrypt_file(file_name)
64+
65+
66+
key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e'
67+
enc = Encryptor(key)
68+
clear = lambda: os.system('cls')
69+
70+
if os.path.isfile('data.txt.enc'):
71+
while True:
72+
password = str(input("Enter password: "))
73+
enc.decrypt_file("data.txt.enc")
74+
p = ''
75+
with open("data.txt", "r") as f:
76+
p = f.readlines()
77+
if p[0] == password:
78+
enc.encrypt_file("data.txt")
79+
break
80+
81+
while True:
82+
clear()
83+
choice = int(input(
84+
"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"))
85+
clear()
86+
if choice == 1:
87+
enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
88+
elif choice == 2:
89+
enc.decrypt_file(str(input("Enter name of file to decrypt: ")))
90+
elif choice == 3:
91+
enc.encrypt_all_files()
92+
elif choice == 4:
93+
enc.decrypt_all_files()
94+
elif choice == 5:
95+
exit()
96+
else:
97+
print("Please select a valid option!")
98+
99+
else:
100+
while True:
101+
clear()
102+
password = str(input("Setting up stuff. Enter a password that will be used for decryption: "))
103+
repassword = str(input("Confirm password: "))
104+
if password == repassword:
105+
break
106+
else:
107+
print("Passwords Mismatched!")
108+
f = open("data.txt", "w+")
109+
f.write(password)
110+
f.close()
111+
enc.encrypt_file("data.txt")
112+
print("Please restart the program to complete the setup")
113+
114+
115+

0 commit comments

Comments
 (0)