Problem 2 (80 points): Caesar Cipher You are going to write a simple encryption/
ID: 3670270 • Letter: P
Question
Problem 2 (80 points): Caesar Cipher You are going to write a simple encryption/decryption program. The algorithm used is very easy to break, so do not start using your program to "securely store your bank account! Encryption is a process of encoding a message or information in such a way that only authorized parties can read it. (At least this is the idea until somebody discovers how to break an encryption pattern.) Caesar cipher is one of the simplest encryption techniques. It was used by Julius Caesar to encrypt his private correspondence. It is sometimes called shift cipher because each letter in the unencrypted message is replaced by a different letter which is a fixed number of places down the alphabet (hence shifting) "Caesar cipher left shift of three" by Matt Crypto-http://en.wikipedia. org/wiki/File:Caesar3.png. Licensed under Public Domain via Wikime- dia Commons. Accessed June 2015 GHEI ABICIDIEF The figure above illustrates a left shift of three, so that each occurrence of E in the plaintext becomes B in the ciphertext. When encrypting a message, a person needs to lookup each plain letter, find its corresponding cipher letter and perform a substitution. The following example uses a right shift of three Plain: A B C D E FGH IJKL MNOPQR STUVWXY Z Cipher: DE FGHIJKL MNO PQR STUVWXY zAB C The message "Hello class" is encrypted by replacing each character with a character three spaces to the right in the alphabet resulting in "Khoor fo The message "Ohz Brun" is decrypted by replacing each character with a character three spaces to the left in the alphabet resulting in "New York". It is easy to perform both tasks once we know the shift value and the alphabet. Your Assignment Write a program that performs encryption/decryption based on the Caesar cipher. Here is a detailed outline of the program: e Display the application banner: Encrypt/Decrypt Tool . Ask the user if they want to perform encryption or decryption. What would you like to do? (E)ncrypt or (D) ecrypt? The program should accept both upper and lower case letters. If the user enters a different letter, the program should display an error message ERROR: invalid choice. and terminate immediatel y. . Ask the user for the key value. The key value is the shift amount. It should be an integer in the range -26-26 (inclusive). A shift of zero indicates that no encryption is performed since each letter in the plaintext is replaced with itself in the ciphertext. A positive number means shift right during encryption and left during decryption. A negative number means shift left during encryption and right during decryption. Your program should verify that the key is in the valid range. If the key is not valid, your program should display an error message and terminate immediately * Ask the user for the message. You may assume that the message is contained in one line of text, but may contain spaces and punctuation Compute the encrypted/decrypted message and print it to the screen. All alphabetic characters should be replaced by their encrypted/decrypted equivalents. Any other characters (including digits) should be left unmodified. Your program should preserve case of the letters: encrypting 'a using the above cipher should result in ,d, and encrypting 'W, should result in 'z.Explanation / Answer
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What would you like to do? (E)ncrypt or (D)ecrypt?");
String tool = input.nextLine(); //leaves new line character
tool = tool.toUpperCase();
if (tool.equals("E") || tool.equals("D")) {
System.out.println("Enter your encryption key: ");
int shiftKey = Integer.parseInt(input.nextLine()); // leaves new line character
System.out.println("Enter your message: ");
String userMessage = input.nextLine();
if (tool.equals("E"))
System.out.println(encrypt(userMessage, shiftKey));
if (tool.equals("D"))
System.out.println(decrypt(userMessage, shiftKey));
} else {
System.out.println("ERROR: Invalid choice!");
}
input.close();
}
private static String encrypt(String message, int shiftKey) {
return String.format("NOT IMPLEMENTED message:{%s} shiftKey:(%d}", message, shiftKey);
}
private static String decrypt(String message, int shiftKey) {
return String.format("NOT IMPLEMENTED message:{%s} shiftKey:(%d}", message, shiftKey);
}
}
output
What would you like to do? (E)ncrypt or (D)ecrypt?
E
Enter your encryption key:
5
Enter your message:
Lord of the Rings
NOT IMPLEMENTED
message:{Lord of the Rings}
shiftKey:(5}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.