-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.py
56 lines (45 loc) · 1.14 KB
/
keys.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
from cryptography.fernet import Fernet
from globals import globals as g
import os
import struct
def generate_key_file():
key = Fernet.generate_key()
filename = './breakfast/hashbrowns.txt'
f = open(filename,"wb")
f.write(key)
f.close()
os.chmod(filename, 0o600)
def load_key():
filename = './breakfast/hashbrowns.txt'
return open(filename, "rb").read()
def encrypt_file(message, filename):
key = load_key()
encoded_message = message.encode()
f = Fernet(key)
encrypted_message = f.encrypt(encoded_message)
f = open(filename,"wb")
f.write(encrypted_message)
f.close()
os.chmod(filename, 0o600)
def decrypt_file(filename):
f = open(filename, "r")
if f.mode == 'r':
message =f.read()
decrypt_this = rawbytes(message)
key = load_key()
f = Fernet(key)
decrypted_message = f.decrypt(decrypt_this)
return decrypted_message.decode()
def rawbytes(s):
outlist = []
for cp in s:
num = ord(cp)
if num < 255:
outlist.append(struct.pack('B', num))
elif num < 65535:
outlist.append(struct.pack('>H', num))
else:
b = (num & 0xFF0000) >> 16
H = num & 0xFFFF
outlist.append(struct.pack('>bH', b, H))
return b''.join(outlist)