Issue
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)
Solution
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)
Answered By - ScottC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.