CSCE 2004 - Programming Assignment 5
Due: Wednesday, April 6, 2011 at 11:59pm

1. Problem Statement:

The purpose of this assignment is to become familiar with strings, operations on strings, and simple cryptography. In this assignment, you will implement a simple substitution encryption algorithm called Caesar Cipher.

The basic concept of the Caesar Cipher is to take a message (called the plaintext) and shift each letter to the right a predetermined value (called the key). The resulting scrambled message is referred to as the ciphertext. Afterward, we should be able to use the ciphertext and key to reconstruct the plaintext when desired.

Example: Take the word EXAMPLE and encrypt it using the key C. Since C is the third letter in the alphabet, the "offset" is 3, meaning each letter in the plaintext will be shifted three places to the right, becoming a new letter in the ciphertext. So, after applying the offset to each letter in EXAMPLE we get the following:

  • Plaintext: EXAMPLE
  • Key: C
  • Offset: 3
  • Ciphertext: HADPSOH

  • This assignment will consist of 4 steps:

    Important: Each of the following steps should appear together in your HW5 submission. We recommend creating a "main menu" loop to allow each of the following options to be selected and run in your final program. This implies that there will be a pair of encryption() and decryption() functions for each part. However, the choice of overall program structure is up to you. (Choose wisely!)

  • Part 1: Caesar Cipher in the key of C++ (Yeah, we're funny.)

    The user is to be prompted for a short message that is entered using the keyboard. The program should read the message (the plaintext) into a string variable then echo it to the screen. Then, the plaintext should be encrypted by "adding" the offset for letter 'C' to each character of the plaintext (the encrypted plaintext is the ciphertext). The ciphertext can overwrite the original or be stored in a second string variable.

    Remember to handle wrapping at the end of the alphabet so that X + C -> A.

    Hint: Convert lower case letters to upper case letters in the plaintext (using the toupper function from the <iomanip> library.)

    Then, the ciphertext should be printed to the screen. Finally, the program should reverse the above operation to decrypt the ciphertext and recover the plaintext. The resulting plaintext should be displayed on the screen. It should be should be identical to the original plaintext, except possibly all in uppercase letters.

  • Part 2: Caesar Cipher with a variable key

    The same as Part 1, except that the user is also prompted for the letter to be used as the key. That is, instead of using 'C' every time, the message is encrypted using whatever letter the user types in. The same key should be used for the entire encryption/decryption process.

    Hint: We still strongly recommend converting lower case letters in the plaintext to uppercase letters before applying the key. In addition, consider converting the key to uppercase as well to avoid any extra difficulties.

  • Part 3: Caesar Cipher with an obscured key

    The same as Part 2 except that the key is read from a file instead of the user. The program should use File I/O to open and read from "TheFallOfTheLeaf.txt", an excerpt of the poem "Autumn - The Fall of the Leaf" by S. Moore. The file may be found on the class website.

    Then, the user should be prompted for a number between 1 and 100. This number will be used to find a character inside the file that will be used as the key. For example, if the user enters 1, the first character in the file will be used (in our case, 'S'). If they enter a 4, the 4th letter will be used (i.e. 'm' -> 'M') as the key.

    If the spot chosen by the user happens to be white space or punctuation, loop through the file until you find a valid letter of the alphabet that can be used for encryption.

    The program should print the key generated in this way to the screen. Then, the letter is used to encrypt the plaintext using the same process as Part 1. The program should then display the ciphertext, decrypt the ciphertext, and display the resulting plaintext.

  • Part 4: Caesar Cipher as a One-Time Pad

    This part is similar to Part 3, except that the user enters a number between 1 and 100 that should serve as a starting point for encryption. In fact, we will implement a version of the One-Time Pad encryption scheme.

    The first letter in the message is encrypted with the letter found at the start location in the file, as specified by user input. The second letter in the message is then encrypted with the next letter in the file, and so forth. Make sure to skip over white space and punctuation.

    (This is how many WWII spies actually encrypted and decrypted messages! All they needed was a book, e.g. Oliver Twist, and a previously agreed upon start location for any particular date.)

    Once again, the program should display the original plaintext and ciphertext, decrypt the ciphertext, and the display the resulting plaintext. The following is an example of how the encryption should work:

  • Example:
  • User-entered Plaintext: TEST
  • User enters 4 as the starting location.
  • Reading from the file, the key becomes "m, e, r..." from the 4th, 5th, and 6th locations in the file, respectively.
  • Ciphertext: (characters of the ciphertext highlighted in bold)
    T + M = G,
    E + E = J,
    S + R = K,
    ...and so forth until the entire plaintext is encrypted.
  • 2. Design:

    In order to effectively design this assignment you need to build on knowledge attained since the beginning of the semester. You may use any design you like (you will be graded on your choices), but here are some suggested tips to get you started:

  • Use the following equations to get the offset to be added to the message and the encrypted letter:

  • offset = (KeyLetter - 'A') + 1
  • encryptedLetter = 'A' + (original distance from 'A' + offset) % 26
  • encryptedLetter = 'A' + (plainTextLetter - 'A' + offset) % 26

  • Example:
  • plainTextLetter = 'X', KeyLetter = 'C'
  • offset = 'C' - 'A' + 1 = 3
  • encryptedLetter = 'A' + (('X' - 'A') + 3) % 26)
  • = 'A' + (23 + 3) % 26
  • = 'A' + 0
  • = 'A'

  • Make the cipher text and the plain text all uppercase to aid in encryption/decryption. This can be done by including <iomanip> and calling toupper. Review your HW#3 submission for examples of usage of toupper.
  • Only encrypt letters. You do not have to encrypt white space or punctuation. However, you should maintain spaces during encryption/decryption.
  • The plain text message should be no more than 50 characters long.
  • 3. Implementation:

    Start your program with comments based on your design and (the following cannot be emphasized enough..) add portions of code a little at a time. Compile and run your program on a regular basis, so you always have something that runs, even if it only does part of the job. Make sure you have completed the first task, e.g., printing your name to the screen, before you start writing code for the second task.

    4. Testing and Debugging:

    Test your program to check that it operates correctly for all of the requirements listed above. Also check for the error handling capabilities of the code. For debugging purposes, it is useful to "echo" any variables you input, and to print intermediate results as they are calculated.

    You should comment out your debugging code before you hand in your final version of the program.

    5. Documentation:

    When you have completed your C++ program, write a short report (less than half of a page long; a few sentences will suffice) describing what the objectives were, what you did, and the status of the program. Does it work properly for all test cases? Are there any known problems?

    Include this report in the comments at the top of your source code file.

    Finally, when you are finished, run your full program from start to finish. Copy/paste the entire screen output from at least one full run of your program at the bottom of your source code. Use comments around this output (that is, /* and */) to ensure your program still compiles as-submitted after this addition.

    6. Project Submission:

    To Submit Homework 5:

    1. Begin an email to your TA with the exact title "CSCE 2004 HW5".
    2. Copy/paste the contents of your source code file into the body of the email.

      (Note: Do not add anything extra to the body of the email! And make sure your FULL name and uark ID number are printed out at the beginning of your program...)

    3. Send the email!

    The date/time-stamp on your email will be used to verify that you met the due date above. All late projects will receive reduced credit (10% off if less than 24 hours late, no credit if more than 24 hours late), so hand in your best effort on the due date.