-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerator.py
336 lines (312 loc) · 14.5 KB
/
Generator.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os
import secrets
import string
from cryptography.fernet import Fernet
def creating_a_password(size_of_strength_password):
password = ""
for i in range(size_of_strength_password):
# Alphabet = 0, Numbers = 1, Symbols = 2.
random_temporary_number = random_generator.randrange(3)
if random_temporary_number == 0:
password += random_generator.choice(alphabet)
elif random_temporary_number == 1:
password += random_generator.choice(numbers)
elif random_temporary_number == 2:
password += random_generator.choice(symbols)
return password
def confering_strong_password(password, size_of_strength_password):
while True:
ok1 = False
if (any(alphabet for k in password)
and any(numbers for k in password)
and any(symbols for k in password)):
ok1 = True
size_of_password = len(password)
character = {
"lower_letter": 0,
"upper_letter": 0,
"numbers": 0,
"symbols":0
}
min_distribution = 0.1 * size_of_password
max_distribution = 0.4 * size_of_password
for i in password:
if i in alphabet and i.islower():
character["lower_letter"] += 1
elif i in alphabet and i.isupper():
character["upper_letter"] += 1
elif i in numbers:
character["numbers"] += 1
elif i in symbols:
character["symbols"] += 1
ok2 = False
if ((character["lower_letter"] >= min_distribution
and character["lower_letter"] <= max_distribution)
and (character["upper_letter"] >= min_distribution
and character["upper_letter"] <= max_distribution)
and (character["numbers"] >= min_distribution
and character["numbers"] <= max_distribution)
and (character["symbols"] >= min_distribution
and character["symbols"] <= max_distribution)):
ok2 = True
if ok1 and ok2:
break
password = creating_a_password(size_of_strength_password)
return password
def confering_the_strength(password):
size_of_password = len(password)
character = {
"lower_letter": 0,
"upper_letter": 0,
"numbers": 0,
"symbols":0
}
min_distribution = 0.1 * size_of_password
max_distribution = 0.4 * size_of_password
for i in password:
if i in alphabet and i.islower():
character["lower_letter"] += 1
elif i in alphabet and i.isupper():
character["upper_letter"] += 1
elif i in numbers:
character["numbers"] += 1
elif i in symbols:
character["symbols"] += 1
if size_of_password < 8:
return 1
elif size_of_password >= 8 and size_of_password < 10:
if (character["lower_letter"] == size_of_password
or character["upper_letter"] == size_of_password
or character["numbers"] == size_of_password
or character["symbols"] == size_of_password):
return 1
else:
return 2
elif size_of_password >= 10 and size_of_password < 12:
if (character["lower_letter"] == size_of_password
or character["upper_letter"] == size_of_password
or character["numbers"] == size_of_password
or character["symbols"] == size_of_password):
return 1
elif (character["lower_letter"] > 0
and character["upper_letter"] > 0
and character["numbers"] > 0
and character["symbols"] > 0):
return 3
else:
return 2
elif size_of_password >= 12:
if (character["lower_letter"] == size_of_password
or character["upper_letter"] == size_of_password
or character["numbers"] == size_of_password
or character["symbols"] == size_of_password):
return 1
elif (character["lower_letter"] > 0
and character["upper_letter"] > 0
and character["numbers"] > 0
and character["symbols"] > 0):
if ((character["lower_letter"] >= min_distribution
and character["lower_letter"] <= max_distribution)
and (character["upper_letter"] >= min_distribution
and character["upper_letter"] <= max_distribution)
and (character["numbers"] >= min_distribution
and character["numbers"] <= max_distribution)
and (character["symbols"] >= min_distribution
and character["symbols"] <= max_distribution)):
return 4
else:
return 3
else:
return 2
def create_key():
key = Fernet.generate_key()
return key
def encrypt(password, key):
password = password.encode()
encrypt_password = Fernet(key).encrypt(password)
encrypt_password = encrypt_password.decode()
return encrypt_password
def decrypt(password, key):
password = password.encode()
decrypt_password = Fernet(key).decrypt(password)
decrypt_password = decrypt_password.decode()
return decrypt_password
def save_password_to_file(password, filename="passwords.txt"):
with open(filename, "a") as file:
file.write(password + "\n")
# MAIN FUNCTION
# Language selection menu
print("#----------------------------------------------------#")
print("Select Language:")
print("1. Portuguese")
print("2. English")
print("#----------------------------------------------------#")
print()
language = int(input("Choose your language (1/2): "))
if language == 1:
# Portuguese Menu
print("#----------------------------------------------------#")
print('# MENU DO PROJETO "STRONG-PASSWORD-GENERATOR":"')
print(" 1. Conferir a força de uma senha qualquer.")
print(" 2. Gerar uma senha forte aleatória.")
print(" 3. Gerar uma senha forte criptografada.")
print(" 4. Descriptografar uma senha.")
print(" 5. Criptografar uma senha com uma chave específica.")
print("#----------------------------------------------------#")
print()
elif language == 2:
# English Menu
print("#----------------------------------------------------#")
print('# MENU FOR THE PROJECT "STRONG-PASSWORD-GENERATOR":')
print(" 1. Check the strength of any password.")
print(" 2. Generate a random strong password.")
print(" 3. Generate an encrypted strong password.")
print(" 4. Decrypt a password.")
print(" 5. Encrypt a password with a specific key.")
print("#----------------------------------------------------#")
print()
# Creating an instance of SystemRandom
random_generator = secrets.SystemRandom()
alphabet = string.ascii_letters
numbers = string.digits
symbols = string.punctuation
user_input = int(input("Choose an option (1/2/3/4/5): "))
entrada_valida = False
while not entrada_valida:
match user_input:
case 1:
entrada_valida = True
if language == 1:
senha_do_usuario = input("Digite a senha: ")
if language == 2:
senha_do_usuario = input("Digit the password: ")
password_strength = confering_the_strength(senha_do_usuario)
if password_strength == 1:
if language == 1:
print("Nível 1: Fraca\n")
if language == 2:
#Level 1: Weak\n
print("Level 1: Weak\n")
elif password_strength == 2:
if language == 1:
print("Nível 2: Moderada\n")
if language == 2:
print("Level 2: Moderate\n")
elif password_strength == 3:
if language == 1:
print("Nível 3: Semi-Forte\n")
if language == 2:
print("Level 3: Semi-Strong\n")
elif password_strength == 4:
if language == 1:
print("Nível 4: Forte\n")
if language == 2:
print("Level 4: Strong\n")
case 2:
entrada_valida = True
print()
if language == 1:
print("Português: Qual tamanho você deseja (Obrigatório 16+ caracteres)? ")
if language == 2:
print("English: What size you wish (Mandatory 16 characters or more)? ")
size_of_strength_password = int(input("R = "))
while size_of_strength_password < 16:
if language == 1:
print("Português: Entrada inválida. A senha deve conter 16 ou mais caracteres!")
size_of_strength_password = int(input("Digite um tamanho válido: "))
if language == 2:
print("English: Invalid input. The password must contain 16 characters or more!")
size_of_strength_password = int(input("Digit a valid size: "))
password = creating_a_password(size_of_strength_password)
password_confered = confering_strong_password(password, size_of_strength_password)
if language == 1:
print("A senha forte criada é: {0}".format(password_confered))
save_option = input("Você deseja salvar essa senha em um arquivo? (sim/nao): ").strip().lower()
if language == 2:
print("The strong password created is: {0}".format(password_confered))
save_option = input("Do you want to save the password to a file? (yes/no): ").strip().lower()
if save_option == 'yes' or save_option == 'sim':
save_password_to_file(password_confered)
if language == 1:
print("Sua senha foi salva em um arquivo.")
if language == 2:
print("Password has been saved to file.")
case 3:
entrada_valida = True
print()
if language == 1:
print("Português: Qual tamanho você deseja (Obrigatório 16+ caracteres)? ")
if language == 2:
print("English: What size you wish (Mandatory 16 characters or more)? ")
size_of_strength_password = int(input("R = "))
while size_of_strength_password < 16:
if language == 1:
print("Português: Entrada inválida. A senha deve conter 16 ou mais caracteres!")
size_of_strength_password = int(input("Digite um tamanho válido: "))
if language == 2:
print("English: Invalid input. The password must contain 16 characters or more!")
size_of_strength_password = int(input("Digit a valid size: "))
password = creating_a_password(size_of_strength_password)
key = create_key()
print()
if language == 1:
print("Português: Atenção, a chave de criptografia usada é de extrema importância. Guarde-a com todo o cuidado possível!")
if language == 2:
print("English: Attention, the encryption key used is extremely important. Keep it with the utmost care!\n")
print("Key: {0}".format(key))
encrypt_password = encrypt(password, key)
if language == 1:
print("Senha criptografada: {0}".format(encrypt_password))
save_option = input("Você deseja salvar essa senha em um arquivo? (sim/nao): ").strip().lower()
if language == 2:
print("Encrypted password: {0}".format(encrypt_password))
save_option = input("Do you want to save the password to a file? (yes/no): ").strip().lower()
if save_option == 'yes' or save_option == 'sim':
save_password_to_file(password_confered)
if language == 1:
print("Sua senha foi salva em um arquivo.")
if language == 2:
print("Password has been saved to file.")
case 4:
entrada_valida = True
print()
if language == 1:
password = input("Por favor, digite a senha criptografada: ")
key = input("Por favor, digite a chave de criptografia dentro de b': '" )
if language == 2:
password = input("Please, enter the encrypted password: ")
key = input("Please, enter the encryption key inside b' ': ")
decrypt_password = decrypt(password, key)
print()
if language == 1:
print("Senha: {0}".format(decrypt_password))
if language == 2:
print("Password: {0}".format(decrypt_password))
case 5:
entrada_valida = True
if language == 1:
password = input("Por favor, digite a senha criptografada: ")
key = input("Por favor, digite a chave de criptografia dentro de b': '" )
if language == 2:
password = input("Please, enter the encrypted password: ")
key = input("Please, enter the encryption key inside b' ': ")
encrypt_password = encrypt(password, key)
if language == 1:
print("Senha criptografada: {0}".format(encrypt_password))
save_option = input("Você deseja salvar essa senha em um arquivo? (sim/nao): ").strip().lower()
if language == 2:
print("Encrypted password: {0}".format(encrypt_password))
save_option = input("Do you want to save the password to a file? (yes/no): ").strip().lower()
if save_option == 'yes' or save_option == 'sim':
save_password_to_file(password_confered)
if language == 1:
print("Sua senha foi salva em um arquivo.")
if language == 2:
print("Password has been saved to file.")
case _:
if language == 1:
print("Português: Entrada inválida. Por favor, digite 1, 2, 3, 4 ou 5 para uma entrada válida.")
user_input = int(input("Escolha uma opção (1/2/3/4/5): "))
if language == 2:
print("English: Invalid input. Please, enter 1, 2, 3, 4 or 5 for a valid input.")
user_input = int(input("Choose an option (1/2/3/4/5): "))