Create python file ciphers.py
in your cs100/ch3
folder.
Here is an message encrypted by the transposition cipher described in your book. The original message was separated into odd/even characters, and all odd characters were put before all even characters. The encrypted message is: "o r ue wsmyuaespraeoe"
. Write a function called transpositionDecrypt
to decrypt the message and return the decrypted plain text. Call your function print the secret message! What does it say?
def substitutionEncrypt(plainText, key):
alphabet = "abcdefghijklmnopqrstuvwxyz "
plainText = plainText.lower()
cipherText = ""
for ch in plainText:
idx = alphabet.find(ch)
cipherText = cipherText + key[idx]
return cipherText
# add code here to implement the substitutionDecrypt function
key = 'zyxwvutsrqponmlkjihgfedcba '
plainText = 'the quick brown fox'
encryptMsg = substitutionEncrypt(plainText,key)
print( "Encrypted message: " + encryptMsg )
# add code here to call the function to decrypt and test
Add a substitutionDecrypt
function that takes two parameters (an encrypted message and a key) and returns the decrypted message.
substitutionDecrypt
function and displays the result of the message held in encryptMsg
after it is decrypted.Here is a new message encrypted with the substitution cipher: "xsflsklau bgt qgm sjw xstmdgmk"
. It was encrypted using the key: "stuvwxyzabcdefghijklmnopqr "
. Use your decryption function to print out what it says. What’s the secret message?
userTypedMessage = input('Type a secret message here: ')
print(userTypedMessage)