MP3: CaesarCipher

Learning Objectivies

Introduction

Julius Caesar used a simple encryption scheme to encrypt military messages. His military orders (written in Latin) were encoded by translating an 'A' to 'D', B to E etc.

  1. Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  2. Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC

Typically a secret is encoded using an offset of 13, so this encoding is known as 'ROT13'. (A Puzzle for you: what's special about 13? Cryptic Clue:13+13=26). Today, this substitution algorithm is still used to hide passwords from casual reading.

Your assignment is to create a Java program, CaesarCipher.java, that encodes and decodes text messages. Here's a description of what your program will initially do:

  1. Ask the user for the offset to use. (For example the user might enter '3' to encode a message, '-3' to later decode the message).
  2. Check the user entered a valid value (accept -25 to -1 (inclusive) and 1 to 25 (inclusive). Do not accept 0 (why not?), or any values >25 or <-25.
  3. Request a line of text. If the user enters a blank line, print 'Bye' and exit.
  4. Print the line of text entered.
  5. Convert the line into uppercase then process the line using the shift offset.
  6. Print the result.
  7. Go back to step 3.

Caesar's Decoder Ring


Clarification

Reverse Engineering

For the reverse engineering task you will have to look at the following test cases and recognize what pattern is being used.

Reverse Engineering Test Cases

public void testPositionShift() {
        CheckInputOutput
                .setInputCaptureOutput("999\nThere are two ways to write error-free programs, but only the third one works.\n\n");
        String expected = "Please enter the shift value (between -25..-1 and 1..25)\n"
                + "Using position shift\n"
                + "Please enter the source text (empty line to quit)\n"
                + "Source   :There are two ways to write error-free programs, but only the third one works.\n"
                + "Processed:TIGUI GYM DHA KPOJ MI SOGSE GUVTX-NAOP CFDWISFM, YSS PPOC ZOM DSUER EEW QJNHQ.\n"
                + "Please enter the source text (empty line to quit)\n"
                + "Bye.\n";
 
        CaesarCipher.main(new String[0]);
        int line = CheckInputOutput.checkCompleteOutput(expected);
        if (line > 0)
            fail("Review incorrect output on line " + line);
    }
 
    public void testDecodePositionShift() {
        CheckInputOutput
                .setInputCaptureOutput("-999\nTIGUI GYM DHA KPOJ MI SOGSE GUVTX-NAOP CFDWISFM, YSS PPOC ZOM DSUER EEW QJNHQ.\n\n");
        String expected = "Please enter the shift value (between -25..-1 and 1..25)\n"
                + "Using position shift\n"
                + "Please enter the source text (empty line to quit)\n"
                + "Source   :TIGUI GYM DHA KPOJ MI SOGSE GUVTX-NAOP CFDWISFM, YSS PPOC ZOM DSUER EEW QJNHQ.\n"
                + "Processed:THERE ARE TWO WAYS TO WRITE ERROR-FREE PROGRAMS, BUT ONLY THE THIRD ONE WORKS.\n"
                + "Please enter the source text (empty line to quit)\n"
                + "Bye.\n";
        CaesarCipher.main(new String[0]);
        int line = CheckInputOutput.checkCompleteOutput(expected);
        if (line > 0)
            fail("Review incorrect output on line " + line);
    }

Example Output

* Example Output 1: (User enters 0\n26\n3\nABCD EFGHIJKLMNOPQRSTUVWXYZ!\n\n)
 
Please enter the shift value (between -25..-1 and 1..25)
0 is not a valid shift value.
Please enter the shift value (between -25..-1 and 1..25)
26 is not a valid shift value.
Please enter the shift value (between -25..-1 and 1..25)
Using shift value of 3
Please enter the source text (empty line to quit)
Source   :ABCD EFGHIJKLMNOPQRSTUVWXYZ!
Processed:DEFG HIJKLMNOPQRSTUVWXYZABC!
Please enter the source text (empty line to quit)
Bye.
 
 
* Example Output 2: (User enters -3\ndefg hijklmnopqrstuVWXYZABC!\n\n)
 
Please enter the shift value (between -25..-1 and 1..25)
Using shift value of -3
Please enter the source text (empty line to quit)
Source   :DEFG HIJKLMNOPQRSTUVWXYZABC!
Processed:ABCD EFGHIJKLMNOPQRSTUVWXYZ!
Please enter the source text (empty line to quit)
Bye.