Project Description:
This project asks you to work on a simple symmetric (conventional) cryptosystem. I am giving you a skeleton Java program below that implements the Caesar cipher on each byte, using a rotation from 0 to 255.
A few hints:
You are welcome to ignore these hints if you want.
What To Hand In:
The items below must appear in what you hand in, numbered the same way and not hand-written, except for the annotations on listings. You should strive for short bulleted items, rather than long boring paragraphs.
Java Code for a Skeleton System:
The first class below (Crypto) just accesses command line arguments (which say whether to encrypt or decrypt, give the key, give the name of the input file, and give the name of the output file), reads the key, opens the files, and creates an instance of the other class that does the work, and invokes a method in that class (transform).
// Crypto: simple encode and decode of a file, byte-at-a-time
import java.io.*;
public class Crypto {
static InputStream in; // input file args[2]
static OutputStream out; // output file args[3]
public static void openFiles(String infile, String outfile) {
try {
in = new FileInputStream(infile);
out = new FileOutputStream(outfile);
} catch (IOException e) {
System.err.println("Error opening files");
System.exit(-1);
}
}
public static void main(String[] args) {
boolean encode = true; // encode or decode
int key = 0;
String usage =
"Usage: java Crypto (-encode | -decode) key infile outfile";
if (args.length != 4) {
System.err.println(usage);
System.exit(-1);
}
try {
key = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.err.println("Error converting key \"" +
args[1] + "\" to int");
System.exit(-1);
}
if (args[0].equals("-encode")) encode = true;
else if (args[0].equals("-decode")) encode = false;
else {
System.err.println(usage);
System.exit(-1);
}
openFiles(args[2], args[3]);
Code code = new Code(encode, key, in, out);
code.transform();
}
}
The second class below (Code) actually reads and writes the files, byte-at-a-time. Between reading and writing a byte, it either encodes or decodes the byte, depending on the value of a boolean switch encode. As mentioned before, your main work (in a simple version of the assignment) could be to find more complicated functions to use for the methods encodeByte and decodeByte (but they must be inverses of one another).
// Code: encode or decode a file
import java.io.*;
public class Code {
int key; // input key
InputStream in; // input file
OutputStream out; // output file
boolean encode = false; // encode or decode
// code: constructor, pass parameters
public Code(boolean mode, int keyP,
InputStream inP, OutputStream outP) {
encode = mode;
key = keyP;
in = inP;
out = outP;
}
// transform: read bytes, encode or decode each byte, write
public void transform() {
try {
int inB; // input byte
int outB; // output byte
// read input file, byte-at-a-time
while ((inB = in.read()) != -1) { // till end-of-file
// make a simple change
if (encode) outB = encodeByte(key, inB);
else outB = decodeByte(key, inB);
writeByte(outB);
} // end of while
} catch (IOException e) {
System.err.println("Error reading input file");
System.exit(-1);
} // end try
}
// encodeByte: encode a byte
private int encodeByte(int key, int inB) {
return (inB + key)%256; // encode
}
// decodeByte: decode a byte
private int decodeByte(int key, int inB) {
return (inB - key)%256; // decode
}
// writeByte: then write byte
private void writeByte(int outB) {
try {
out.write(outB);
} catch (IOException e) {
System.err.print("Error writing file");
System.exit(-1);
}
}
}
Here are the results of a simple run on a Unix box, using the JDK directly. User input is in boldface. Notice that after encoding and decoding, the original file is recovered.
% cat mess.text
Now is the time for all good men to come to the aid of their party
% java Crypto -encode 13 mess.text cipher2.text
% java Crypto -decode 13 cipher2.text mess2.text
% vi cipher2.text
[|\x84-v\x80-\x81ur-\x81vzr-s|^?-nyy-t||q-zr{-\x81|-p|zr-\x81|-\x81ur-nvq-|s-\x8
1urv^?-}n^?\x81\x86^W
% cat mess2.text
Now is the time for all good men to come to the aid of their party