-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonCipher.py
More file actions
51 lines (41 loc) · 1.44 KB
/
Copy pathPythonCipher.py
File metadata and controls
51 lines (41 loc) · 1.44 KB
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
from cryptography.fernet import Fernet
import time
class CipherFile:
def encrypt_file(self, key_path, file_path):
try:
with open(key_path, 'r') as file:
key = file.read()
fernet = Fernet(key)
except:
print("Key file not found, please create one first.")
return 0
try:
with open(file_path, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(file_path, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
except:
print("File not found, please check the path.")
return 0
def decrypt_file(self, key_path, file_path):
try:
with open(key_path, 'r') as file:
key = file.read()
fernet = Fernet(key)
except:
print("Key file not found, please create one first.")
return 0
try:
with open(file_path, 'rb') as file:
encrypted = file.read()
decrypted = fernet.decrypt(encrypted)
with open(file_path, 'wb') as decrypted_file:
decrypted_file.write(decrypted)
except:
print("File not found, please check the path.")
return 0
def create_key(self):
key = Fernet.generate_key()
with open('filekey.key', 'wb') as file:
file.write(key)