Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Caesar Cipher (30 points) The Caesar cipher is a (very insecure) method for en

ID: 3601908 • Letter: 1

Question

1 Caesar Cipher (30 points) The Caesar cipher is a (very insecure) method for encrypting text dating back to the Romans. It is the same alphabetic shift cipher as is used in the Secret Decoder Rings that no longer come as prizes in breakfast cereals and Cracker Jacks. Each letter in a text is replaced by the letter that occurs some fixed number of positions later in the alphabet. For instance, if the cipher implemented a shift of 3 positions, 'A' would be transformed to 'D', 'B' to 'E', 'L' to 'O' and so forth. Letters at the end of the alphabet wrap around to the beginning, so 'Z' would get coded as 'C'. To decode the message, simply reverse the shift Java characters are represented in Unicode, which is a complex character representation that is supposed to allow writing in the alphabet of every human language, including Cyrillic, Tagalog, Hmong, Egyptian hieroglyphics, Chinese and (ahem) Klingon. However, the first 128 characters in Unicode are the same as the English-only ASCII representation that has been around since the 1950s. In ASCII, printable English characters start at 32 (space) and end at 126 (tilde), with all of the alphabetic characters in between. The letter 'A' is 65, 'B' is 66, 'a' is 97, etc. We can generalize the Caesar cipher to handle all of these characters, using an arbitrary shift size as our key. The algorithm is, then if charValue 126 outputChar - (char) charValue // leave alone charvalue charvalue + key // We include both of these so that we can encode by using a positive numbeir // and decode using the same number as a negative if charvalue > 126 charvalue = charvalue - 95 // ( 127-32) if charvalue

Explanation / Answer

Caesar.java

import java.io.*;

public class Caesar {
    public static void main(String[] args)
    {
       //Close and print help if incorrect arguments
        if (args.length < 2 || args.length > 3) {
            usageHelp();
            return;
        }

        //Determine File or Screen output
        boolean OutputToFile = false;
        if (args.length == 3)
        {
            OutputToFile = true;
        }

        //Argument constants
        final int key = Integer.parseInt(args[0]);
       final String inFileName = args[1];
        final String outFileName;
       if (OutputToFile)
       {
            outFileName = args[2];
        }
        else
        {
            outFileName = null;
        }

        //input setup
        BufferedReader input;
        try{
            input = new BufferedReader(new FileReader(inFileName));
        }
        catch(IOException e)
        {
            e.printStackTrace();
            return; //so we don't hit while loop without intialized input
        }

        //output setup
        BufferedWriter output;
        if (OutputToFile) {
            try
            {
                output = new BufferedWriter(new FileWriter(outFileName));
            }
            catch(IOException e)
            {
                e.printStackTrace();
                return; // so we don't hit while loop without initialized output
            }
        }
        else
        {
            output = null;
        }


        //Main processing of data
        while(true)
       {
            try
            {
                int next = input.read();
                if(next == -1)
                    break;
                if (OutputToFile) {
                    output.write(caesarShift((char)next, key));
                    output.flush();
                } else {
                    System.out.print(caesarShift((char)next,key));
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
                break;
            }

        }

    }

    public static char caesarShift(char charToShift, int shiftAmount)
    {
        int charValue = (int)charToShift + shiftAmount;
        if (charToShift < 32 || charToShift > 126)
            return charToShift; //don't touch non-english characters
        if (charValue > 126)
           charValue -= 95;
       else if (charValue < 32)
           charValue += 95;
       return (char)(charValue);      
   }

    public static void usageHelp()
    {
        System.out.println("Invalid number of arguments. Usage: java Caesar key infile [outfile]");
    }
}