Cryptography Lab

Objectives

Create a program that assists in a basic form of cryptography, a substitution cypher. Write a program that will accept a phrase and convert it into code by substituting letters according to a key.

This program will also be an example of functions. You will be given a main function body. This function refers to a number of other functions which you will have to create. It's part of your job to figure out how these functions should be created and what their parameters and output should be.

The program is based on a standard text-based menu. You'll need to create methods (function) to display the menu, get input from it, and handle the details. Your program should encrypt and decrypt messages.

Sample Run

Here's a sample run of the basic program in action.

Crypto Machine

Select an option
0) Quit
1) Encrypt a phrase
2) Decrypt a phrase

Please enter your choice
1
Please enter unencrypted phrase
Java Rocks
RUDUEQWFN
Crypto Machine

Select an option
0) Quit
1) Encrypt a phrase
2) Decrypt a phrase

Please enter your choice
2
Please enter encrypted phrase
RUDUEQWFN
JAVAROCKS
Crypto Machine

Select an option
0) Quit
1) Encrypt a phrase
2) Decrypt a phrase

Please enter your choice
0
Goodbye!
        
      

Starter Code

You may copy and paste the following code into your editor to get started. You may also type the code by hand, but your main method must be similar to the one posted here:

import java.util.Random;
import java.util.Scanner;

public class Crypto {

  static Scanner input = new Scanner(System.in);
  static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static String key = "UBWKOVGAPRFJLCQHZENSXDMYTI";
    
  public static void main(String[] args) {
    boolean keepGoing = true;
    while(keepGoing){

      String response = menu();

        if (response.equals("1")){
          System.out.println("Please enter unencrypted phrase");
          String plain = input.nextLine();
          plain = plain.toUpperCase();
          System.out.println(encrypt(plain));

        } else if (response.equals("2")){
          System.out.println("Please enter encrypted phrase");
          String code = input.nextLine();
          code = code.toUpperCase();
          System.out.println(decrypt(code));

        } else if (response.equals("0")){
          System.out.println("Goodbye!");
          keepGoing = false;

        } else {
          System.out.println("Sorry. I didn't understand");
        } // end if
      } // end while
   } // end main    
} // end Crypto
    
      

Notes

Please keep the following ideas in mind:

This is a substitution cypher
Each letter is replaced by another letter with no repetition
Ignore spaces and punctuation
You must account for spaces (I ignored them, but there are other legitimate responses.)

The best program will also account for punctuation and other symbols. (Ignoring them is fine.)
Manage Capitalization
In most cryptography applications, all letters are converted to upperCase. Please follow this convention or deal with potential case conversion problems in another way.
Use Methods
You may not make significant changes to the main method. Determine which functions are necessary and create them. (The blackbelt project does allow for one slight modification, noted below.) Do not turn in a program with only a main() method!!
Pass data between methods
Your methods may require some input and return some output. Part of the task is to determine how the methods are to be created.
Using a key
There's several workable answers, but I used a string with all the alphabet characters and a second string with a "scrambled" version of the alphabet.
Useful String methods
This program is easier if you use appropriate methods of the String object. I made use of the following methods:
  • length()
  • charAt()
  • indexOf()
Don't send spy messages with this
The encryption used here is laughably weak. This is an interesting exercise in cryptography, but it's nowhere near useable as an encryption tool. Don't encode your secret recipe with this thing and expect it to remain secret.
Test your program
The best way to test your program is to encrypt a phrase, and then decrypt the encoded phrase to see if you got the original (sans spaces and other punctuation.)

Black Belt Option

You can improve this program dramatically by adding the ability to generate a new random key. The new key should have the following characteristics:

You can add a new menu item to allow the user to generate a random key. You'll need a new function for the random key. You'll also need to modify the main() function and the menu to accept new input.

Note that array manipulation is not necessary for this project. All work can be done directly with strings. You may need some exception-handling, though.