-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypt.py
55 lines (37 loc) · 1.03 KB
/
encrypt.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
#!/usr/bin/env python3
# Import libraries
import os
from cryptography.fernet import Fernet
print("... libraries imported")
# Collect list of files
def get_file_list():
files = []
for file in os.listdir():
if file == "encrypt.py" or file == "decrypt.py" or file == "key_file.key":
continue
if os.path.isfile(file):
files.append(file)
print("Found files are:", files)
return files
# Generate Key
def generate_key_file():
key = Fernet.generate_key()
print("Key is:", key)
with open("key_file.key", "wb") as f:
f.write(key)
print("...key-file generated")
return key
# Encrypt
def encryption_process(files, key):
print("Encryption started...")
for file in files:
with open(file, "rb") as f:
content = f.read()
encrypted_content = Fernet(key).encrypt(content)
with open(file, "wb") as f:
f.write(encrypted_content)
print("...encryption complete")
# Main function calls
files = get_file_list()
key = generate_key_file()
encryption_process(files, key)