Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java, create a program that uses the Caesar Cipher. For this assignment, you will be creating a basic Caesar Cipher. This is a simple

In java, create a program that uses the Caesar Cipher.

For this assignment, you will be creating a basic Caesar Cipher. This is a simple way to encrypt or decrypt messages using a key. To encrypt a message, each letter is shifted by a number of letters in the alphabet (the key). If the shift results in going beyond the normal letter system, the encryption wraps around to the respective beginning or end depending on whether the shift is performed to the right or left. To decrypt the message, each letter is shifted in the opposite direction by the same number of letters, applying a similar wrapping technique if necessary. Encryption Example using a key of 5: Start: To be encrypted End: yt gj jshwduyji Decryption Example using a key of 5: Start: ymnx bfx ijhwduyji End: This was decrypted For more information about this, http://practicalcryptography.com/ciphers/caesar-cipher/ does a good job at explaining the cipher. Exercises 1. Inside of the Main class, fill in the code to make the caesarEncrypt method work properly. a. In order to convert the entire String to convert to uppercase, research for any methods that can do this. b. In order to convert the entire String to and array of char, research for any methods that can do this. c. In order to convert an int into a char value, research typecasting d. In order to convert an array of char into a String, research for any methods that can do this. e. Once the caesarEncrypt method is believed to be complete, compile and run the program. This should print out the encrypted value of Hello World using a key of 13 to the console. The encrypted value should be uryyb jbeyq. 2. Inside of the Main class, create and fill in a new method called caesarDecrypt. a. Remember, the only difference between encrypting and decrypting with a Caesar Cipher is adding vs subtracting the key. b. Once the caesarDecrypt method is believed to be complete, add another print statement to the Main method, and attempt to compile and run the program with dlsjvtl ihjr av jpz! as the value to decrypt and 7 as the key. The result should be easily recognizable, but the lab TA and Mentor will know the answer. Double check with them if you have questions. 3. Inside of the Main class, edit the Main method to get the value to be decrypted and the key as input from the user. Replace the hard coded values from Exercise 2 with the user input. Try other values to test out the results. 4. Inside of the Main class, fill in the code to make the print method work properly. a. Leave the filePath variable the way it is. This does not need to be changed. b. Dont forget to use TryCatch statements when writing to a file. i. Although it is better to practice catching individual exceptions, using the general Exception is allowed. c. Once the print method is believed to be complete, go back to the Main method and change it so that the decrypted value is printed to a file rather than the console.

// Make sure to import the Scanner for getting user input and the FileWriter and BufferedWriter for writing to a file

// Main class

public class Main {

// Main method is where the program starts from

public static void main(String[] args) {

// Print out the return value of the caesarEncrypt method using the parameters ("Hello World",13)

System.out.println(caesarEncrypt("Hello World",13));

// Create your scanner

// Get the phrase to decipher from the user and store it in a temporary String variable

// Assignment will use (dlsjvtl ihjr av jpz!)

// Get the key from the user and store it in a temporary variable

// Assignment will use (7)

// *Tip: Don't forget to clear the newline character after getting an integer

// Print to file the return value of the caesarDecrypt method using the parameters gathered from the user

}

// Method that takes in a String to be encrypted and the key to encrypt it with, then returns the encrypted String

private static String caesarEncrypt(String phrase, int key){

// Make the entire String Upper Case

// Convert the String into an array of characters (char[])

// Iterate through each character in the array

// If the character is not a letter, skip it (letters are 65-90 and skip using continue)

// Create a temporary integer variable give it the value of the current character

// Convert the ASCII value to 0-25 by subtracting 65

// Shift the integer value by adding the key

// While the integer value is negative, add 26

// Wrap the integer value by modding it by 26

// Convert the 0-25 value to ASCII by adding 65

// Set the current character value to the character value of the temporary integer variable

// Return the String value of the encrypted character array

return;

}

// Method that takes in a String to be decrypted and the key to decrypt it with, then returns the decrypted String

// Method that takes in a String and writes it to a text file

private static void print(String text){

// String variable containing the file path to write to

String filePath = "lab1.txt";

// Attempt to create a FileWriter and BufferedWriter, then write the String to the given file path

// If it is successful, print out a confirmation message

// If it fails, print out the error message

// *Tip: Make sure to close the BufferedWriter!

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Android Graphics

Authors: Wallace Jackson

1st Edition

1430257857, 978-1430257851

More Books

Students also viewed these Programming questions