CSCI 241 Labs: Lab 14
I've Got A Secret


There are 5 checkpoints , including the clean-up checkpoint, in this lab. You and your partner should work together using just one of your accounts. CHANGE WHO IS CONTROLLING THE COMPUTER AFTER EACH CHECKPOINT! If you need help with any exercise, raise your hand.

Copy the lab materials to your account from /home/student/Classes/Cs241/Labs/Lab14

In this lab you will explore some of the encryption algorithms discussed in the prelab and use them in some demonstration programs.

 

Decrypting

Start running BlueJ, and open project Lab14. It contains numerous classes that implement (or partially implement) encryption algorithms. These are:
  1. SwapCipher - which swaps adjacent characters in a StringBuilder.
  2. CaesarCipher - which rotates the characters of a StringBuilder forward in the alphabet.
  3. VigenereCipher - which adds the letters of a keyword to the letters in a StringBuilder.
  4. SubstitutionCipher - which substitutes letters from a rearranged alphabet for those in a StringBuilder.
It also contains the EncryptionDemo class which is currently set up to read a file one line at a time and encrypt it using a SwapCipher object.

Our task for the first checkpoint is to encrypt and then decrypt a data file. Let's start by looking at the data file. Go to a terminal window and cd into the Lab14 directory. Then, type:

    more mcgee.dat  

This will list the data file to the screen. It is the introductory stanza to the finest poem ever written in the English language, The Cremation of Sam McGee (note: this is Dr. Hansen's personal opinion). Now return to BlueJ.

Open the SwapCipher class and look it over. Make sure you understand how the encryption algorithm works, since there will be questions at the checkpoint regarding it.

Close the SwapCipher class and open the EncryptionDemo class. Examine the code carefully. Do you see how it is processing the entire file? Change the path in the File declaration statement to point to the Lab14 directory in which you are working. Close the EncryptionDemo class.

Compile and run the main() method from EncryptionDemo. Enter the file name: mcgee.dat     You should see the encrypted version of mcgee.dat appear in the output window.

Your instructors have also created a file that contains this output (the encrypted version of the file). It is named mcgee.swap. Unfortunately, the code to decrypt the file is incomplete. Open the class DecryptionDemo. It contains a shell of a main() method. Complete its main() method, imitating the code from EncryptionDemo, but modifying it so that mcgee.swap is opened and decrypted to the screen.
Hint: The two main() methods are very similar. They differ only in that DecryptionDemo opens a different file and decrypts rather than encrypts.
Call us over when you are finished.

1 Be prepared to answer the following questions:

  1. What is the role of the variable temp inside SwapCipher's encrypt() method? Is it necessary, or could it be eliminated?
  2. Why does SwapCipher's decrypt() method just call the encrypt() method?
  3. What does file.hasNextLine() stand for in the while loop in EncryptionDemo?
  4. What are the differences between the main() methods in EncryptionDemo and DecryptionDemo?

 

Caesar Ciphers

Caesar ciphers work by rotating characters a certain number of positions forward in the alphabet. As you saw in the prelab, a Caesar cipher which rotates by 3 positions would move all 'A's to 'D's, 'B's to 'E's, etc. One slightly tricky part is that when we reach 'X', three positions forward takes us past 'Z' and out of the alphabet. We can handle this situation by checking to see if we pass 'Z' and if so, rotating back to the beginning of the alphabet. (Logically, this means moving 3 characters forward and then 26 characters backwards. As you will see shortly, this is exactly what our code does.) We will simplify our work by assuming we only want to rotate uppercase alphabetic characters.

Open the CaesarCipher class in the BlueJ editor. Examine the encrypt() method carefully. Check out the questions at the checkpoint for the type of things you should be noticing.

Open the EncryptionDemo class.

Try running the program. Oooops! The decrypt() method in the CaesarCipher class is only a stub. Call us over when you have it running correctly.

2 Be ready to answer the following questions:

  1. What type of encryption would we get if the number of characters rotated were 26?
  2. What would we have to modify in encrypt() to make it rotate both upper and lower case characters?
  3. How would both the encrypt() and decrypt() methods change if we were returning a String rather than modifying the parameter?

 

Vigenere Ciphers

Vigenere ciphers work by adding characters from a keyword to the plaintext. Each character gets moved forward in the alphabet by a different amount, based on the keyword. We have given you a complete working version of the VigenereCipher class.

Okay, here is the tough part. If you understand how the Vigenere algorithm works, you should be able to figure out the keyword used to do the encryption by looking at two files which represent the original and the encrypted versions.

When you think you have it, try decrypting mcgee.xxx using DecryptionDemo. Call us over when you have the correct answer.

3 Tell us what the keyword is and explain how you found it.

 

Substitution Ciphers

Substitution ciphers work by substituting one character for another character throughout the text. For example, 'A' might translate to 'S' and 'B' might translate to 'H'. The SubstitutionCipher class is instantiated with a String, exactly 26 characters long. The String contains the sequence of translation characters for each letter in the alphabet. In our example, the String would begin "SH...". To translate, we find the index of the plaintext character in the alphabet and take the character with the same index in the encryption string. To translate back, we look up the index of the ciphertext character in the encryption string and take the character with the same index in the alphabet.

4 Show us your decryption method and the two demo programs, as well as the output from the decryption run.

 

Extras for Experts

Note that there is one class in the Lab14 project that we haven't touched. That is PermutationCipher. Permutation ciphers require the use of an array of integers. Since you have already studied arrays, see if you can figure out how it works!

 

After the Lab


5 Show us that you have logged out, cleaned up, turned off your monitor and pushed in your chairs for this last checkpoint.