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:
- SwapCipher - which swaps adjacent characters in a StringBuilder.
- CaesarCipher - which rotates the characters of a StringBuilder
forward in the alphabet.
- VigenereCipher - which adds the letters of a keyword to the
letters in a StringBuilder.
- 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:
- What is the role of the variable temp inside SwapCipher's
encrypt() method? Is it necessary, or could it be
eliminated?
- Why does SwapCipher's decrypt() method just
call the encrypt() method?
- What does file.hasNextLine() stand for in the while
loop in EncryptionDemo?
- 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.
- Modify it so that it encrypts
using a CaesarCipher object initialized to rotate 12
characters forward.
Hint: If you change more than one line of code, you are
changing too many!
- Compile and run the main() method in EncryptionDemo,
again using mcgee.dat as your input file.
What appears in the output window should begin
FTQ ODQYMFUAZ AR EMY YOSQQ
NK DANQDF I. EQDHUOQ
- Modify DecryptionDemo so that it decrypts using a CaesarCipher
object initialized with 12.
The file you want to test it on is mcgee.rot12
Hint: Again the modifications are VERY simple.
Change the filename and the line where the encryptor object
is declared.
Try running the program.
Oooops! The decrypt() method in the CaesarCipher
class is only a stub.
- Reopen the CaesarCipher class and complete the decrypt()
method.
- Test it using the DecryptionDemo class.
Hint: You must check to see that the rotated character hasn't
moved
backwards past 'A' and add 26 if it has.
Call us over when you have it running correctly.
2 Be ready to answer the following questions:
- What type of encryption would we get if the number of characters
rotated were 26?
- What would we have to modify in encrypt() to make it
rotate both upper and lower case characters?
- 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.
- Modify the EncryptionDemo class to have it
encrypt mcgee.dat using a Vigenere cipher with a keyword of
"DAGGER".
Hint: You will probably have to look at the VigenereCipher
class in order
to determine how to construct the Vigenere object.
- Compare the resulting output from mcgee.DAGGER to the
file mcgee.dat. Except for the quotation marks, they should
be identical.
- Use the more command as you did in checkpoint 1 to see
the values stored in the file.
- Modify the DecryptionDemo class to have it decrypt the
file mcgee.DAGGER.
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.
- Get out pencil and paper for these steps.
- Look at the plaintext file (mcgee.dat) and the
ciphertext file
(mcgee.xxx) by using the more command in your
terminal window to
examine each file individually.
Use your understanding of the the algorithm to determine the
keyword,
tracing the differences between the two files on paper.
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.
- Open class SubstitutionCipher. Like in checkpoint 2, decrypt()
is only a stub. Complete this method.
- Modify EncryptionDemo and DecryptionDemo to
work with the SubstitutionCipher class.
- Try encrypting mcgee.dat.
The file to try to decrypt is named mcgee.QWERTYUIOPASDFGHJKLZXCVBNM.
This should give you a BIG hint as to what encryption string
to use.
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.