Search
Reverse Cipher Revisited

Reverse Cipher Revisited

You've now covered enough programming concepts to implement the Reverse Cipher covered in Chapter 2, Section 1. There will be room for improvement on this first attempt, especially after we learn more programming concepts and syntax.

Define the Algorithm

Before writing any code, you should revisit the operations you perform by hand and write out the steps you wish to have the computer do for you. While this step may seem tedious or time consuming, it will help you form a strategy to writing your code and identify any operations you don't yet know how to do with computer code.

A potential algorithm for implementing the Reverse Cipher.

  1. Define the plaintext message
  2. Identify the last character in the message.
  3. Store this character in the first position of the ciphertext message.
  4. Identify the preceding character in the plaintext message.
  5. Store this character in the next position of the ciphertext message.
  6. Repeat steps 4-5 until the first character of the plaintext message has been stored in the ciphertext message.
  7. Display the ciphertext message

Here is some sample Python Code that implements this message:

This first cell of code will perform Step 1 in the algorithm by defining the plaintext message to be the variable plaintext which is a str type with value spyglass. It also creates the variable ciphertext which is assigned to an empty string. Even though it has no characters, an empty string is still a string.

plaintext = 'spyglass'
ciphertext = ''

The next cell of code reassigns the value of ciphertext several times. Each time it takes the existing value of ciphertext and concatenates the character found at a particular index in the string plaintext, and stores this new value to the string ciphertext. Doing this repeatedly accumulates characters to the string ciphertext. Can you see any issues with this particular block of code that may cause issues under different circumstances?

ciphertext = ciphertext + plaintext[7]
ciphertext = ciphertext + plaintext[6]
ciphertext = ciphertext + plaintext[5]
ciphertext = ciphertext + plaintext[4]
ciphertext = ciphertext + plaintext[3]
ciphertext = ciphertext + plaintext[2]
ciphertext = ciphertext + plaintext[1]
ciphertext = ciphertext + plaintext[0]

Once the previous block of code finishes, the string ciphertext should hold all 8 characters of the string plaintext, just in a reversed order. Printing plaintext will let us see it's value.

print( ciphertext )
ssalgyps

You can visualize all 3 cells of code running in sequence using the Online Python Tutor hosted by the University of Waterloo [Reverse Cipher Code Vizualization]. This link will load this code in it's entirety, and let you run it one execution at a time by clicking Forward. Keep an eye on the different variables displayed in the Global frame. Here you'll see how Python creates objects, assigns their values, and then discards objects once they no longer have references.

Limitations of this Code

You may notice that this program is not very flexible and was pretty repetitive to write. Try using this code with a different length message and see what happens. Unless you change the code by adding new lines, or deleting existing lines, it will create an error. That's because this program coded to only successfully run with a plaintext string of length 8. You can see that in the code, since it only makes references to indices 0 through 7. You could alter your code for each new string you wish to encode, but it would likely be beneficial to learn other strategies to make our code flexible enough so that it works for strings of various lengths, not only one specific length.