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.
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!
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
Please keep the following ideas in mind:
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.