-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFernet_script.py
More file actions
45 lines (30 loc) · 892 Bytes
/
Fernet_script.py
File metadata and controls
45 lines (30 loc) · 892 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from pprint import pprint
from cryptography.fernet import Fernet, InvalidToken
input_file = 'business'
output_file = 'dumpbusness'
def keyz():
key_ = Fernet.generate_key()
with open('keys.key', 'wb') as damage:
damage.write(key)
pprint(damage.write(key_))
def load_key():
return open('keys.key', 'rb').read()
# encrypt file
with open(input_file, 'rb') as filo:
out = filo.read()
key = load_key()
fernet = Fernet(load_key())
dropkey = fernet.encrypt(out)
with open(output_file, 'wb') as filesave:
filesave.write(dropkey)
pprint(filesave.write(dropkey))
with open(output_file, 'rb') as f:
data = f.read()
# decrypt file
try:
fernet = Fernet(key)
decrypted = fernet.decrypt(data)
with open('busnessdump', 'wb') as fs:
fs.write(decrypted)
except InvalidToken as e:
print("Invalid Key - Unsuccessfully decrypted")