CS 100

Logo

Scientific Computing

Activity 12 - Ciphers

Create python file ciphers.py in your cs100/ch3 folder.

  1. 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?

  2. Now add the following lines of code. Run it and understand what the code does inside.
    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
    
  3. Add a substitutionDecrypt function that takes two parameters (an encrypted message and a key) and returns the decrypted message.

  4. Add code that calls/invokes the substitutionDecrypt function and displays the result of the message held in encryptMsg after it is decrypted.

If you finish early

  1. 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?

  2. Add the following lines of code to ask the user (whoever runs your program) for a message to be encrypted. When you run the program, you will be prompted to type something in the shell. Type anything you would like in the shell, and then press enter on the keyboard.
    userTypedMessage = input('Type a secret message here: ')
    print(userTypedMessage)
    
  3. Encrypts the message that the user typed in and then decrypt it. Use print statements after each step to visualize your progress.