-
Notifications
You must be signed in to change notification settings - Fork 0
/
SF-2.py
87 lines (72 loc) · 2.69 KB
/
SF-2.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
'''
Version 2:
Encrypts files and overwrites each file's conent with random data before deletion and removes folder structure
In other words
'The files sheds itself so its unformable again'
'''
import os
import sys
import shutil
from cryptography.fernet import Fernet
def generate_key():
# Generate and save a key for encryption
key = Fernet.generate_key()
with open("encryption.key", "wb") as key_file:
key_file.write(key)
return key
def load_key():
# Load the saved key
return open("encryption.key", "rb").read()
def encrypt_file(file_path, key):
fernet = Fernet(key)
with open(file_path, "rb") as file:
file_data = file.read()
encrypted_data = fernet.encrypt(file_data)
# Write the encrypted file
with open(file_path + ".enc", "wb") as file:
file.write(encrypted_data)
# Securely delete original file after encryption
secure_delete(file_path)
def encrypt_folder(folder_path):
key = generate_key()
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
encrypt_file(file_path, key)
def secure_delete(file_path, passes=3, zero_out=False):
# Overwrite the file with random data multiple times
with open(file_path, "ba+") as delfile:
length = delfile.tell()
for _ in range(passes):
with open(file_path, "br+") as delfile:
delfile.seek(0)
delfile.write(os.urandom(length))
delfile.write(b"\x00" * length if zero_out else os.urandom(length)) # Set binary values to 0 at file level/not raw disk method
os.remove(file_path)
def delete_folder(folder_path):
# Securely delete all files in the folder and then delete the folder
for root, dirs, files in os.walk(folder_path, topdown=False):
for file in files:
secure_delete(os.path.join(root, file))
for directory in dirs:
os.rmdir(os.path.join(root, directory))
os.rmdir(folder_path)
def delete_key():
#Securely delete the encryption key file
if os.path.exists("encryption.key"):
secure_delete("encryption.key")
if __name__ == "__main__":
# Get the folder path from the command-line argument
if len(sys.argv) != 2:
print("Usage: python script_name.py <folder_path>")
sys.exit(1)
folder_to_encrypt = sys.argv[1]
# Check if folder exists
if not os.path.isdir(folder_to_encrypt):
print(f"Error: Folder '{folder_to_encrypt}' does not exist.")
sys.exit(1)
# Encrypt and delete the folder
encrypt_folder(folder_to_encrypt)
delete_folder(folder_to_encrypt)
delete_key()
print("Folder encrypted and securely deleted.")