-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
51 lines (38 loc) · 1.45 KB
/
cipher.py
File metadata and controls
51 lines (38 loc) · 1.45 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
50
51
import os
import pyAesCrypt
from pathlib import Path
# Function of encrypting file
def encrypt(file_path, password, delete_file=False):
# Check if file exists
if Path(file_path).exists():
directory = Path(file_path).parent
filename = Path(file_path).stem
# Check if file isn't encrypted
if Path(file_path).suffix == '.aes':
return 'The file is encrypted', False
# Encrypt the file
encrypt_file = f'{Path(directory).joinpath(filename)}.aes'
pyAesCrypt.encryptFile(file_path, encrypt_file, password)
# Delete the file
if delete_file:
os.remove(file_path)
return 'Success', True
return 'No file', False
# Function of decrypting file
def decrypt(file_path, password, extension='.txt', delete_file=False):
# Check if file exists
if Path(file_path).exists():
directory = Path(file_path).parent
filename = Path(file_path).stem
# Check if file is encrypted
if Path(file_path).suffix != '.aes':
return 'Error extension', False
extension = extension if extension[0] == '.' else f'.{extension}'
# Decrypt the file
decrypt_file = f'{Path(directory).joinpath(filename)}{extension}'
pyAesCrypt.decryptFile(file_path, decrypt_file, password)
# Delete the file
if delete_file:
os.remove(file_path)
return 'Success', True
return 'No file', False