1from cryptography.fernet import Fernet
2message = "my deep dark secret".encode()
3
4f = Fernet(key)
5encrypted = f.encrypt(message)
6
1# encrypting
2from cryptography.fernet import Fernet
3message = "my deep dark secret".encode()
4
5f = Fernet(key)
6encrypted = f.encrypt(message)
7# decrypting
8from cryptography.fernet import Fernet
9encrypted = b"...encrypted bytes..."
10
11f = Fernet(key)
12decrypted = f.decrypt(encrypted)
1text = input()
2
3def encrypt(t):
4 chars = list(text)
5 allowed_characters = list(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?!")
6
7 for char in chars:
8 for i in allowed_characters:
9 if char == i:
10 chars[chars.index(char)] = allowed_characters.index(i)
11 return chars
12
13print(encrypt(text))
1from cryptography.fernet import Fernet
2
3def generate_key():
4 """
5 Generates a key and save it into a file
6 """
7 key = Fernet.generate_key()
8 with open("secret.key", "wb") as key_file:
9 key_file.write(key)
10
11def load_key():
12 """
13 Load the previously generated key
14 """
15 return open("secret.key", "rb").read()
16
17def encrypt_message(message):
18 """
19 Encrypts a message
20 """
21 key = load_key()
22 encoded_message = message.encode()
23 f = Fernet(key)
24 encrypted_message = f.encrypt(encoded_message)
25
26 print(encrypted_message)
27
28if __name__ == "__main__":
29 encrypt_message("encrypt this message")
30