-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.py
63 lines (40 loc) · 1.49 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
57
58
59
60
61
62
import glob
import os
from cryptography.fernet import Fernet
# Generate a symmetric key
def generate_symmetric_key():
key = Fernet.generate_key()
return key
# Save the symmetric key
def save_key_to_ssh_folder(key, key_name):
ssh_folder = os.path.expanduser("~/.ssh")
key_file_path = os.path.join(ssh_folder, key_name)
with open(key_file_path, "wb") as key_file:
key_file.write(key)
print(f"Key saved to {key_file_path}")
# Load a symmetric key from a file
def load_key_from_file(key_name):
ssh_folder = os.path.expanduser("~/.ssh")
key_file_path = os.path.join(ssh_folder, key_name)
with open(key_file_path, "rb") as key_file:
key = key_file.read()
return key
# Encrypt a file using a symmetric key
def encrypt_file(input_file, output_file, key):
fernet = Fernet(key)
with open(input_file, "rb") as file:
file_data = file.read()
encrypted_data = fernet.encrypt(file_data)
with open(output_file, "wb") as encrypted_file:
encrypted_file.write(encrypted_data)
# Decrypt a file using a symmetric key
def decrypt_file(input_file, output_file, key):
fernet = Fernet(key)
with open(input_file, "rb") as file:
encrypted_data = file.read()
decrypted_data = fernet.decrypt(encrypted_data)
with open(output_file, "wb") as decrypted_file:
decrypted_file.write(decrypted_data)
def list_keys():
ssh_folder = os.path.expanduser("~/.ssh")
return glob.glob(os.path.join(ssh_folder, "*"))