2

I have it set to ask the user a word to encrpyt which works fine using my own alphabet.

My issue is trying to also get it to return the deciphered text.

So far I have it either returning the encrypted message twice or sending back a different version of the encrypted message.

I have tried using - instead of + in my for char, and it gives me a error which I thought was the correct way to do it.

alphabet = "abcdefghijklmnopqrstuvwxyz"
key      = "zjrekydnqoluaxmicvpgtfbhws"


def decryptMessage(ciphertext, key):
    
    plaintext = ""
    for char in ciphertext:
        
        if alphabet.find(char) < +1:
             plaintext += key[alphabet.find(char)]
        else:
                 plaintext += char
    
    
    return plaintext


def encryptMessage(plaintext, key):
   
    ciphertext = ""

    
    for char in plaintext:
       
        if alphabet.find(char) > -1:
            ciphertext += key[alphabet.find(char)]
        else:
            ciphertext += char
    
    
    return ciphertext


message = input("Type a message to encrypt: ")


encrypted = encryptMessage(message, key)


decrypted = decryptMessage(encrypted, key)


print("Plaintext message: " + message)
print("Encrypted message: " + encrypted)
print("Decrypted message: " + decrypted)

2 Answers 2

3

you should use the builtin str.translate

message = "hello world"
alphabet = b"abcdefghijklmnopqrstuvwxyz"
key      = b"zjrekydnqoluaxmicvpgtfbhws"
encrypted = message.translate(dict(zip(alphabet,key)))
print("E:",encrypted)
decrypted = encrypted.translate(dict(zip(key,alphabet)))
print("D:",decrypted)
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to keep with the theme of your original code:

You need to modify as follows:

alphabet = "abcdefghijklmnopqrstuvwxyz"
key      = "zjrekydnqoluaxmicvpgtfbhws"


def decryptMessage(ciphertext, key):
    
    plaintext = ""
    for char in ciphertext:
        
        if key.find(char) > -1:
             plaintext += alphabet[key.find(char)]
        else:
             plaintext += char
    
    return plaintext


def encryptMessage(plaintext, key):
   
    ciphertext = ""
    for char in plaintext:
       
        if alphabet.find(char) > -1:
            ciphertext += key[alphabet.find(char)]
        else:
            ciphertext += char
    
    return ciphertext

message = input("Type a message to encrypt: ")
encrypted = encryptMessage(message, key)
decrypted = decryptMessage(encrypted, key)

print("Plaintext message: " + message)
print("Encrypted message: " + encrypted)
print("Decrypted message: " + decrypted)

2 Comments

Thank you, this solved my problem. I can't believe it was right under my eyes the entire time haha.
@DamanVanSteenis - hidden in plain sight :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.