[Python] Encryption and decryption problem. what is wrong with my code?

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a question. What is wrong with my code? I made two versions to maybe get it right but its still not working. This python program should generate a password, "encrypt" a password or "decrypt" a password. I know its not real encryption but I wanted to try to do something to this because I wanted to manifest the basics and maybe learn some new stuff. But now when I encrypt and decrypt my password then only half of the letters are correct. Disclaimer: I am pretty new into python so if you could help me or give me some tips then please help me. Thank you!

    Here is the code: version 1

    import random as rd
    def psw_encryption():
    str_list = str.maketrans({"a": "b","6": "b","m": "c","g": "d","n": "e","l": "f","y": "g","e": "h","i": "k","h": "j","o": "k","v": "l","z": "m","t": "n","0": "o","d": "p","r": "q","q": "r","f": "s","u": "t","j": "u","c": "f","k": "w","i": "x","a": "z","5": "z","4": "9","s": "5","x": "3","8": "5","9":"6","3": "2","7": "9","p": "0", "8": "x"})
    print("What is your password?")
    psw = str(input())
    psw_new = psw.translate(str_list)
    print("Your encrypted password is: ",psw_new + "di29ens92ned")
    print("If you want to decrypt it then just use the decrypter.")
    def psw_decrypt():
    str_list = str.maketrans({"b": "a","b": "6","c": "m","d": "g","e": "n","f": "l","g": "y","h": "e","k": "i","j": "h","k": "o","l": "v","m": "z","n": "t","o": "0","p": "d","q": "r","r": "q","s": "f","t": "u","u": "j","f": "c","w": "k","x": "i","z": "a","z": "5","9": "4","5": "s","3": "x","5": "8","6":"9","2": "3","9": "7","0": "p", "x": "8"})
    print("what is your encrypted password?")
    psw = str(input())
    psw = psw.replace("di29ens92ned", "")
    psw_old = psw.translate(str_list)
    print("Your decrypted password is:", psw_old)
    def psw_gen():
    abc123_list = ["1","2","3","4","5","6","7","8","9","0","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
    print("Your Super Secret password is:",''.join([rd.choice(abc123_list) for _ in range(13)]))

    print("What do you want to do?")
    print("1/ Generate a Super Secret password.")
    print("2/ Encrypt your password.")
    print("3/ Decrypt your password")
    choice_ = int(input("Put your choice here: "))

    if choice_ == 1:
    psw_gen()
    elif choice_ == 2:
    psw_encryption()
    elif choice_ == 3:
    psw_decrypt()
    else:
    print("Invalid input!")

    list2 = ["b 6 m g n l y e h o 1 v z t 2 0 d q r f u j c k i a 5 4 s x 8 9 3 7 p"]
    #1234567890qwertyuiopasdfghjklzxcvbnm


    version 2

    import random as rd #7bqia

    def psw_encryption():
    str_list = str.maketrans("ba6mglnyehovzt20dqrfuicka549sx8973p", "bzcdjklmnot0rqfujhievx58936psawygzd")
    print("What is your password?")
    psw = input()
    psw_new = psw.translate(str_list)
    print("Your encrypted password is:", psw_new)
    print("If you want to decrypt it then just use the decrypter.")

    def psw_decrypt():
    str_list = str.maketrans("bzcdjklmnot0rqfujhievx58936psawygzd", "ba6mglnyehovzt20dqrfuicka549sx8973p")
    print("what is your encrypted password?")
    psw = input()
    psw_old = psw.replace("di29ens92ned", "")
    psw_old = psw_old.translate(str_list)
    print("Your decrypted password is:", psw_old)

    def psw_gen():
    abc123_list = ["1","2","3","4","5","6","7","8","9","0","q","w","e","r","t","y","u","i","o","p",
    "a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
    print("Your Super Secret password is:", ''.join([rd.choice(abc123_list) for _ in range(13)]))

    print("What do you want to do?")
    print("1/ Generate a Super Secret password.")
    print("2/ Encrypt your password.")
    print("3/ Decrypt your password")

    choice_ = int(input("Put your choice here: "))
    if choice_ == 1:
    psw_gen()
    elif choice_ == 2:
    psw_encryption()
    elif choice_ == 3:
    psw_decrypt()
    else:
    print("Invalid input!")


    I already tried many things but nothing worked.

    Continue reading...

Compartilhe esta Página