CSC 171 LAB #11
FALL 2001
GOAL:
1.
Become
familiar with File IO
2.
Become
familiar with simple cryptography
TASKS:
Write a JAVA application that
reads and writes files while encrypting and decrypting text.
BACKGROUND:
The simplest kind of encryption is
the shift cipher, also known as the Caesar cipher. In Caesar’s case to encrypt
each letter in a message would be moved forward by a number (referred to as the
key). So, if the key were 2, ‘a’ would become ‘c’, ‘b’ would become ‘d’, etc.
The cipher is said to wrap around from the end – ‘y’ goes to ‘a’, ‘z’ goes to
‘b’.
Abetter encryption algorithm is
a Random Monoalphabetic Cipher. Instead of using a number for the key,
use a word. Suppose the key word is FEATHER. First remove all the duplicate
letters, yielding FEATHR, and append the other letters of the alphabet in
reverse order. Then encrypt/decrypt according to the mapping:
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
F |
E |
A |
T |
H |
R |
Z |
Y |
X |
W |
V |
U |
S |
Q |
P |
O |
N |
M |
L |
K |
J |
I |
G |
D |
C |
B |
Write
a JAVA method to implement a Random Monoalphabetic Cipher. Two strings are
parameters – the message, and the key word.
Using the same data techniques you used in the Caesar Cipher. Only now
you need to have two data strings.
Your
program should operate from the command line. The program takes the following
comman line arguments:
·
An
optional –d flag to indicate decryption instead of encryption
·
A
keyword key used to generate the mapping
·
The
input file name
·
The
output file name
For
example
java Crypt –d –FEATHER encrypt.txt
output.txt
decrypts
a file using the keyword FEATHER. It is an error not to supply a keyword.
STEPS:
1.
Start
by writing an application that deals with the command line arguments. Remember
to do the error checking.
2.
Write
a method that simply copies the input file to the output file, character by
character
a.
For
input the java.io.FileReader class with the associated read() method will
suffice.
b.
For
output use the java.io.FileWriter class and the associated write(char) methods.
3.
Write
a method that generates the encryption mapping using string manipulation.
4.
Write
a method that encrypts/decrypts a single character.
5.
Insert
a call to the character encrypter/decrypter into your “file copy” method.
6.
Your
hardcopy hand in should include your code and a copy of your encrypted and
decrypted file.
7.
When
you get the paper ready call your lab TA over and demo the program for her/him.
Demonstrate your code by taking a small file, encrypt it then decrypt it. The
decrypted version should match the original
8.
Give
the paper to your TA. This completes the hand in process. The deadline is one
week – no late assignments accepted.
9.
The
demo must run from the local machine. All that should be in the directory is
your class file. Use a command line invocation for the both the window and non
window application and load the html into the