-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.py
53 lines (41 loc) · 1.47 KB
/
decrypt.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
import os
from cryptography.fernet import Fernet
from time import sleep
from tqdm import tqdm
# Load the encryption key from a file
def load_key(key_path):
with open(key_path, 'rb') as key_file:
return key_file.read()
def count_all_files(directory):
total_files = 0
for root, dirs, files in os.walk(directory):
total_files += len(files)
return total_files
# Decrypt a file
def decrypt_file(file_path, fernet):
with open(file_path, 'rb') as file:
encrypted = file.read()
decrypted = fernet.decrypt(encrypted)
with open(file_path, 'wb') as decrypted_file:
decrypted_file.write(decrypted)
# Decrypt all files in a directory (and its subdirectories)
def decrypt_directory(directory_path, fernet):
for root, dirs, files in os.walk(directory_path):
for file_name in files:
file_path = os.path.join(root, file_name)
decrypt_file(file_path, fernet)
def main():
target_directory = input("Enter the target directory to decrypt: ")
if not os.path.isdir(target_directory):
print("The provided path is not a directory")
return
key_path = 'encryption_key.key' # Path to the encryption key
fileCount = count_all_files(target_directory)
# Load the encryption key
key = load_key(key_path)
fernet = Fernet(key)
# Decrypt the directory
decrypt_directory(target_directory, fernet)
print (str(fileCount) + " Files Decrypted")
if __name__ == '__main__':
main()