-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_encryptor.py
89 lines (68 loc) · 2.55 KB
/
file_encryptor.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
from cryptography.fernet import Fernet
import os
def generate_key(password):
"""
Generates a key from the provided password.
Args:
password (str): The password to generate the key.
Returns:
bytes: A derived key.
"""
return Fernet.generate_key() # Simplified for now. Password-based key derivation can be added later.
def encrypt_file(file_path, password):
"""
Encrypts a file using a password.
Args:
file_path (str): The path to the file to encrypt.
password (str): The password to encrypt the file with.
"""
key = generate_key(password)
cipher = Fernet(key)
try:
with open(file_path, "rb") as file:
data = file.read()
encrypted_data = cipher.encrypt(data)
with open(file_path + ".enc", "wb") as encrypted_file:
encrypted_file.write(encrypted_data)
os.remove(file_path)
print(f"Encrypted: {file_path}")
except Exception as e:
print(f"Error encrypting file '{file_path}': {e}")
def decrypt_file(file_path, password):
"""
Decrypts a file using a password.
Args:
file_path (str): The path to the file to decrypt.
password (str): The password to decrypt the file with.
"""
key = generate_key(password)
cipher = Fernet(key)
try:
with open(file_path, "rb") as encrypted_file:
encrypted_data = encrypted_file.read()
decrypted_data = cipher.decrypt(encrypted_data)
original_file_path = file_path.replace(".enc", "")
with open(original_file_path, "wb") as decrypted_file:
decrypted_file.write(decrypted_data)
os.remove(file_path)
print(f"Decrypted: {file_path}")
except Exception as e:
print(f"Error decrypting file '{file_path}': {e}")
def main():
while True:
action = input("\nChoose an action (encrypt/decrypt/exit): ").strip().lower()
if action == "encrypt":
file_path = input("Enter the file path to encrypt: ").strip()
password = input("Enter the password: ").strip()
encrypt_file(file_path, password)
elif action == "decrypt":
file_path = input("Enter the file path to decrypt: ").strip()
password = input("Enter the password: ").strip()
decrypt_file(file_path, password)
elif action == "exit":
print("Exiting the program. Goodbye!")
break
else:
print("Invalid action. Please choose 'encrypt', 'decrypt', or 'exit'.")
if __name__ == "__main__":
main()