-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfernet.py
executable file
·100 lines (72 loc) · 3.08 KB
/
fernet.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
90
91
92
93
94
95
96
97
98
99
100
""" Script for Symmetric Key Encryption/Decryption """
from cryptography.fernet import Fernet
import csv
def sym_key_gen(symkey_fn):
key = Fernet.generate_key() # generate new key
# write key to .key file
with open(symkey_fn, 'wb') as symkey_file:
symkey_file.write(key)
def sym_file_encrypt(symkey_fn, target_fn):
# open key file
with open(symkey_fn, 'rb') as symkey_file:
key = symkey_file.read()
fernet = Fernet(key) # initialise fernet object using key
# open target file
with open(target_fn, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original) # encrypt the data
# write encrypted data to file
with open(target_fn, 'wb') as enc_file:
enc_file.write(encrypted)
enc_file.close()
return enc_file
def sym_file_decrypt(symkey_fn, target_fn):
# open key file
with open(symkey_fn, 'rb') as symkey_file:
key = symkey_file.read()
fernet = Fernet(key) # initialise fernet object using key
# open ciphertext file and read data
with open(target_fn, 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted) # decrypt the data
# write decrypted data to file
with open(target_fn, 'wb') as dec_file:
dec_file.write(decrypted)
dec_file.close()
return dec_file
def fernet_read_file(symkey_fn, target_fn):
# open key file
with open(symkey_fn, 'rb') as symkey_file:
key = symkey_file.read()
fernet = Fernet(key) # initialise fernet object using key
# open ciphertext file and read data
with open(target_fn, 'r') as file:
encFile = bytes(file.read(), 'utf-8')
decFile = fernet.decrypt(encFile).decode() # decrypt data
reader = csv.reader(decFile.splitlines()) # parse as csv
return reader
def fernet_write_file(symkey_fn, target_fn, new_string):
# open key file
with open(symkey_fn, 'rb') as symkey_file:
key = symkey_file.read()
fernet = Fernet(key) # initialise fernet object using key
new_string = new_string.lstrip() # remove leading newline
encFile = fernet.encrypt(bytes(new_string, 'utf-8')) # encrypt data
# open ciphertext file and write data
with open(target_fn, 'w') as file:
file.write(encFile.decode("utf-8"))
def fernet_verify_file(symkey_fn, target_fn):
# open key file
with open(symkey_fn, 'rb') as symkey_file:
key = symkey_file.read()
fernet = Fernet(key) # initialise fernet object using key
# open ciphertext file and read data
with open(target_fn, 'r') as file:
encFile = bytes(file.read(), 'utf-8')
fernet.extract_timestamp(encFile) # verify data - throws error on failure
print("File " + target_fn + " verified!")
# # sym_key_gen('files/p_symkey.key')
# sym_file_encrypt('files/u_symkey.key', 'files/users.csv')
# sym_file_encrypt('files/p_symkey.key', 'files/posts.csv')
# sym_file_decrypt('files/u_symkey.key', 'files/users.csv')
# sym_file_decrypt('files/p_symkey.key', 'files/posts.csv')