-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.py
131 lines (100 loc) · 4.4 KB
/
program.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os, sys, time
import crypto
from os import remove, path
from filedirectorydialog import SelectFile, SelectDirectory, createFile, ToBackupFile, \
FileValidation, ReplaceExistingFileInput, Exists, FileName
def PrivateKeyValidation(key):
if not bool(key): return False
elif " " in key: return False
return True
def CloseProgram():
print("\nFechando o programa...")
time.sleep(2)
sys.exit()
def Run(options_input):
encryptedFile = False
readBackupFile = ''
if int(options_input) == 1 or int(options_input) == 2:
# select a file
file = SelectFile()
if not file:
print("\n>>> Selecione um Arquivo!")
else:
fileName = FileName(file)
filePath_modified = FileValidation(int(options_input), file, fileName)
if filePath_modified == '':
return False
fileName_modified = FileName(filePath_modified)
# substituir arquivo já existente
replacedFile = ReplaceExistingFileInput(filePath_modified, fileName_modified)
if replacedFile == 0:
return False
if int(options_input) == 1:
try:
# criptografar
encryptedFile = crypto.encrypt(file)
except ValueError as error:
print(error)
if Exists(filePath_modified):
remove(filePath_modified)
finally:
if encryptedFile:
remove(file)
time.sleep(3)
print("Arquivo Criptografado")
return False
else:
# número máximo de tentativas de chave privada
maxPrivateKeyAttempts = 3
while maxPrivateKeyAttempts > 0:
print(f"\n[ Restam ({maxPrivateKeyAttempts}) Tentativa(s) ]")
privateKey = input("Digite a chave de decriptação (chave privada): ")
if replacedFile == 3:
# substituir arquivo já existente
replacedFile = ReplaceExistingFileInput(filePath_modified, fileName_modified)
if not replacedFile:
break
if PrivateKeyValidation(privateKey):
if not Exists(file):
print(f"\n>>> O arquivo '{fileName}' pode ter sido Excluído (ou modificado)\n")
break
else:
try:
# fazendo backup do arquivo
readBackupFile = ToBackupFile(filePath_modified)
# descriptografar
decryptedFile = crypto.decrypt(file, privateKey)
except ValueError as error:
print(error)
if readBackupFile:
with open(filePath_modified, "wb") as newFile:
newFile.write(bytes(readBackupFile))
else:
remove(filePath_modified)
maxPrivateKeyAttempts -= 1
continue
if decryptedFile:
remove(file)
time.sleep(3)
print("Arquivo Descriptografado")
break
else:
maxPrivateKeyAttempts -= 1
print("\n>>> Chave Privada Inválida")
else:
print("\n>>> Você atingiu o limite das tentativas de chave privada!")
elif int(options_input) == 4:
os.system('cls' if os.name == 'nt' else 'clear')
return False
else:
directory = SelectDirectory()
if not directory:
print("\n>>> Selecione um Diretório!")
else:
print(f"\nPasta: {path.basename(directory)}")
print(f"Caminho: {directory}")
fileCreated = createFile(directory)
if fileCreated:
print("\n>>> Arquivo Criado Com Sucesso!")
else:
print("\n>>> A operação foi finalizada!")