-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
85 lines (73 loc) · 3.45 KB
/
main.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
from src.welcome import welcome, sep
from src.process import encrypt, decrypt
from src.db import dbcon, dbsave, dbget
from src.utils.mask import maskpass
from src.utils.color import color
from src.utils.table import menutab, algotab
def main():
welcome()
while True:
prompt = color('[INPUT]', "Enter the password to connect to MySQL: ", newline=True)
dbpass = maskpass(prompt)
dbname = input(color('[INPUT]', "Enter the name of the database: "))
if dbcon(dbpass, dbname):
break
else:
print(color('[FAIL]', "Please try again..."))
while True:
print('\n', sep, '\n')
menutab()
choice = input(color('[INPUT]', "Enter choice (1, 2 or 3): ", newline=True))
if choice == '1':
while True:
ptext = input(color('[INPUT]', "Enter the text to encrypt: "))
if ptext.strip():
break
else:
print(color('[INFO]', "Text cannot be empty or contain only whitespace(s)!", newline=True))
print(color('[FAIL]', "Please try again...\n"))
print()
algotab()
print(color('[INFO]', "Encryption via AES or ChaCha20Poly1305 is recommended, unless you have specific objective(s).", newline=True))
algchoice = input(color('[INPUT]', "Enter choice (1, 2, 3 or 4): ", newline=True))
if algchoice == '1':
algorithm = 'AES'
elif algchoice == '2':
algorithm = 'TripleDES'
elif algchoice == '3':
algorithm = 'Blowfish'
elif algchoice == '4':
algorithm = 'ChaCha20Poly1305'
else:
print(color('[FAIL]', "Invalid algorithm choice. Please try again...", newline=True))
continue
try:
key, encdata = encrypt(ptext, algorithm)
recid = dbsave(encdata, key, algorithm, dbpass, dbname)
print(color('[SUCCESS]', f"Encrypted text: {encdata}", newline=True))
print(color('[SUCCESS]', f"Algorithm: {algorithm}"))
print(color('[SUCCESS]', f"Record ID: {recid}"))
except Exception as e:
print(color('[FAIL]', f"Error during encryption: {e}", newline=True))
elif choice == '2':
try:
inrec = int(input(color('[INPUT]', "Enter the record ID to decrypt: ")))
rec = dbget(inrec, dbpass, dbname)
if rec:
ctext, key, algorithm = rec
dctext = decrypt(ctext, key, algorithm)
print(color('[SUCCESS]', f"Decrypted text: {dctext}", newline=True))
print(color('[SUCCESS]', f"Algorithm: {algorithm}"))
else:
print(color('[FAIL]', "Record not found!", newline=True))
except ValueError:
print(color('[FAIL]', "Invalid record ID. Please try again...", newline=True))
except Exception as e:
print(color('[FAIL]', f"Error during decryption: {e}", newline=True))
elif choice == '3':
print(color('[INFO]', "Process ended!\n", newline=True))
break
else:
print(color('[FAIL]', "Invalid choice. Please try again...", newline=True))
if __name__ == "__main__":
main()