-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptFile.py
More file actions
30 lines (23 loc) · 773 Bytes
/
encryptFile.py
File metadata and controls
30 lines (23 loc) · 773 Bytes
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
from cryptography.fernet import Fernet
# Create file to demonstrate
key = Fernet.generate_key()
file = open("exampleText.txt", "w")
file.write("Deep dark secret BUT this time it is in a .txt file.")
file.close()
# Generate key / (If key has all ready been generated skip this step)
key = Fernet.generate_key()
file = open("key.key", "wb")
file.write(key)
file.close()
# Read key / Get the key from the file
file = open("key.key", "rb")
key = file.read() # The key will be type 'bytes'
file.close()
# Open the file to encrypt
with open("exampleText.txt", "rb") as f:
data = f.read()
fernet = Fernet(key)
encrypted_file = fernet.encrypt(data)
# Write the encrypted data to a new file
with open("exampleText.txt.encrypted", "wb") as f:
f.write(encrypted_file)