-
Notifications
You must be signed in to change notification settings - Fork 3
/
decrypt_code.py
147 lines (118 loc) · 3.51 KB
/
decrypt_code.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from cryptography.fernet import Fernet
from PyInquirer import Separator, prompt
from termcolor import colored
from stegano import lsb
import encryption.aes
import steganography.lsb
from util.key_gen import key_gen
# Defining the decryption function
def decrypt_func() -> None:
# Asking the user for a prompt
enc_info = prompt([
{
'type': 'list',
'qmark': '>',
'name': 'type_of_data',
'message': 'What do you want to decrypt?',
'choices': [
Separator(),
{
'name': 'Text',
},
{
'name': 'File',
},
{
'name': 'Image',
},
],
},
])
if 'type_of_data' not in enc_info:
# user hit Ctrl+C
return
# Storing the type of data in a variable
type_of_data: str = enc_info['type_of_data']
# Calling the appropriate function as per data
if type_of_data == 'File':
handle_file_dec()
elif type_of_data == 'Image':
handle_image_dec()
else:
handle_text_dec()
def handle_text_dec() -> None:
# Using decryption information
decrypt_info = prompt([
{
'type': 'input',
'qmark': '>',
'name': 'data',
'message': 'Enter the text to decrypt.',
},
{
'type': 'password',
'qmark': '>',
'name': 'password',
'message': 'Enter password:',
},
])
if 'data' not in decrypt_info:
# user hit Ctrl+C
return
# Storing data in variables
encrypted_secret = decrypt_info['data']
password = decrypt_info['password']
decrypted_text = encryption.aes.decrypt_text(encrypted_secret, password)
# Printing the text on the console
print(colored('The decrypted text is: ', 'white') +
colored(decrypted_text, 'green'))
def handle_file_dec() -> None:
# Getting the file info
file_info = prompt([
{
'type': 'input',
'qmark': '>',
'name': 'file_path',
'message': 'Enter the path to the file.',
},
{
'type': 'password',
'qmark': '>',
'name': 'password',
'message': 'Enter the password: ',
},
])
if 'file_path' not in file_info:
# user hit Ctrl+C
return
# Storing the password in a variable
password = file_info['password']
file_path = file_info['file_path']
encryption.aes.decrypt_file(file_path, password)
print(colored('File decrypted succesfully.', 'green'))
# print(colored("Ran into an issue.", "red"))
def handle_image_dec():
# Using decryption information
decrypt_info = prompt([
{
'type': 'input',
'qmark': '>',
'name': 'image_path',
'message': 'Enter the path of the image to decrypt.',
},
{
'type': 'password',
'qmark': '>',
'name': 'password',
'message': 'Enter password:',
},
])
image_path = decrypt_info['image_path']
password = decrypt_info['password']
# Trying to decrypt text
data = steganography.lsb.decrypt_image(image_path)
decrypted_text = encryption.aes.decrypt_text(data, password)
# Printing the text on the console
print(colored('The decrypted text is: ', 'white') +
colored(decrypted_text, 'green'))