I'm new on Python, so I'm looking at these YouTube videos of Python projects to practice, and I saw this one of a Password Manager with encryption, but the encryption part couldnt use password, because the module used (cryptography.fernet) would need more modules to be able to use password. So I started trying to make an function to make this, and ended up with that function.
It is working basically, but i want advice about modules that i can use to get same output or simillar and dont need other modules to work fine.
i want to know other functions that i can change in my project to make it faster, since i dont know so much of then;
I want to know another way to store and encrypt the strings, since lists are bigger, and every caracter on the message is turning into an 3-times-larger information at least.
And, (if you can help), i need help to make the commented code part work:
all, except this first "caracters-scrambler" is working fine, and i cant understand what is the wrong function on that part, because it doesnt make an exception, and the outcome comes allmost like how it should when the password is right, but it is still scrambled. You can see it if you de-comment the commented code, i made this to keep the code still working right when you try it.
import random
def coddecod (senha: str, texto: str | list, modo = 'd', debug = False) -> list|str:
'''String <=> List\n|
debug (True / False) is an option that show more information about the process if marked as True\n
texto (String / List) is the text to be Encrypted or Decrypted.\n
modo ( 'c' / 'd' ) is how the function will be used, it can be 'c' (Encrypt) or 'd' (Decrypt).\n
senha (String) is the password to be used to Encrypt/Decrypt (Needs to be the same on both sides).\n
note: Not every keyboard key was added to this function, such "\" or "/" or "§"
This function creates an list (like string) with some caracters of the password, then a code to the password with the list made, which is allways the same for the same password.\n
Then checks to the mode:\n
If 'c', Encrypts:\n
It, randomlly, makes an string with 85 caracters, making 1 in 120 options, making to it an number (data) to be identfied.\n
Then data * password-code to be "hiden"\n
Adds the result to the text and Encrypts the text.\n
If 'd', Decrypts:\n
It re-makes the caracters-string used to Encrypt using the identifier number.\n
And Decrypt using the password code and the string.\n
If the password is REALLY wrong:\n
It makes an random text.\n'''
if debug == True:
print(f"Password used: {senha}\nText used: {texto}\nMode: {modo}\nDebug: True")
lista = ["0bm1!d2Mafgh3TtijkcheH4lnEou5pqUsr67Svwx8yz9BCDFGHIJKLNOPQRVWXYZ! |@#$%&*()_+-=[]°ºª^~`", 0, "", [], "", [0, 0, 0, 0, 0]]
step = 0
for _ in range(5):
lista[3].append(lista[0][0 + step: 17 + step])
step += 17
if debug == True:
print(f"blocks of string to be used: {lista[3]}")
#needs some fix, de-comment this to see.
'''
for idx, char in enumerate(senha): #this is the code i need help. look at the '#string scrambler' to see an better code than this here with same objective.
for step in range(len(lista[3])):
if lista[5][step] == 1:
continue
for idx_fromstep, char_fromstep in enumerate(lista[3][step]):
if char == ch and idx_fromstep <= 4:
lista[2] += "".join(lista[3][step])
lista[5][step] = 1
if idx == len(senha) - 1 and lista[5][0] + lista[5][1] + lista[5][2] + lista[5][3] + lista[5][4] != 5:
for i, ch in enumerate(lista[5]):
if ch == 1:
continue
else:
lista[5][i] = 1
lista[2] += "".join(lista[3][i])
lista[5] = [0, 0, 0, 0, 0]
if debug == True:
print(f"list of the string scrambled by the password: {lista[2]}")'''
for idx, char in enumerate("0" + senha):
for i2, c2 in enumerate(lista[0]): #change to lista[2] when code be fixed
if char == c2:
lista[1] += i2 * idx
if lista[1] == 0:
lista[1] += len(senha)
if debug == True:
print(f"code made from password: {lista[1]}")
if modo == 'c':
if debug == True:
print("mode: Encrypting")
lista[2] = ""
senha = []
lista[4] += str(random.randint(1, 9))
while True: #string scrambler
teste2 = random.randint(0, 4)
if lista[5][teste2] == 1:
continue
lista[2] += "".join(lista[3][teste2])
lista[4] += str(teste2)
lista[5][teste2] = 1
if lista[5][0] + lista[5][1] + lista[5][2] + lista[5][3] + lista[5][4] == 5:
break
data = int(lista[4]) * lista[1]
if debug == True:
print(f"Code of the scambled caracters: {lista[4]}\nList of scrambled caracters: {lista[2]}")
senha.append(data)
for _, char in enumerate(texto):
for idx, c2 in enumerate(lista[2]):
if char == c2:
senha.append(idx * lista[1])
return senha
if modo == 'd':
if debug == True:
print("mode: Decrypting")
s = []
data = str(texto[0] // lista[1])
if debug == True:
print(f"String code got from text: {data}")
try:
for idx, char in enumerate(data):
if idx == 0:
continue
lista[2] += "".join(lista[3][int(char)])
if debug == True:
print(f"list of scrambled caracters got from code: {lista[2]}")
for i in range(len(texto) - 1):
letra = texto[i + 1] // lista[1]
s.append(lista[2][letra])
senha = "".join(s)
return senha
except:
if debug == True:
print(f"Password was really wrong.")
reallywrongpassword = ""
for _ in range(random.randint(5, 80)):
reallywrongpassword += random.choice(lista[0][:])
return reallywrongpassword
test = coddecod("FeijaoTropeiro", "Elias", "c")
wrong = coddecod("54hvrvrwe4ij", test)
wrong2 = coddecod("Feijao", test, "d")
right = coddecod("FeijaoTropeiro", test, "d")
print(f"The text is: 'Elias'.\nThis is the message encrypted: {test}\nThese 2 are the outcomes with wrong passwords: '{wrong}' and '{wrong2}'.\nThis is the message with the right password: {right}.\nRun this code more times to see the encrypted message change.")