-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_text_encryptor.py
More file actions
52 lines (44 loc) · 1.75 KB
/
Copy pathpython_text_encryptor.py
File metadata and controls
52 lines (44 loc) · 1.75 KB
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
from cryptography.fernet import Fernet
def generate_key() -> bytes:
"""Generate a new Fernet key."""
return Fernet.generate_key()
def encrypt_message(key: bytes, message: str) -> bytes:
"""Encrypt a message using the provided key."""
fernet = Fernet(key)
return fernet.encrypt(message.encode())
def decrypt_message(key: bytes, encrypted_message: bytes) -> str:
"""Decrypt an encrypted message using the provided key."""
fernet = Fernet(key)
return fernet.decrypt(encrypted_message).decode()
def main() -> None:
print("Text Encryptor")
while True:
print("\nSelect an option:")
print("1. Generate a new key")
print("2. Encrypt a message")
print("3. Decrypt a message")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
key = generate_key()
print(f"Generated key: {key.decode()}")
elif choice == '2':
key = input("Enter the key: ").encode()
message = input("Enter the message to encrypt: ")
encrypted_message = encrypt_message(key, message)
print(f"Encrypted message: {encrypted_message.decode()}")
elif choice == '3':
key = input("Enter the key: ").encode()
encrypted_message = input("Enter the encrypted message: ").encode()
try:
decrypted_message = decrypt_message(key, encrypted_message)
print(f"Decrypted message: {decrypted_message}")
except Exception as e:
print(f"Decryption error: {e}")
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
main()