Question: This is my assignment: Project 3: Caesar Cipher A Caesar cipher is a type of rotation cipher, which is a classical way to encode data.
This is my assignment:
Project 3: Caesar Cipher
A Caesar cipher is a type of rotation cipher, which is a classical way to encode data. The basic idea is that there is an integer key k, and each character in a file is replaced with the character that is k digits away from it. For example, if the key is 5 and the text file begins with A then the first character in the encrypted file will be E because E is five characters away from A. This type of encryption is not actually secure among other problems, it is susceptible to frequency analysis attacks. In fact, rotation ciphers are often used as puzzles for people to solve.
Your goal for this project is to write a program that encrypts and decrypts text files using a Caesar cipher. Your program should contain the following methods with these precise signatures:
public static int getMenuOption()
This program should display a menu with the following options to the user:
1. Encrypt
2. Decrypt
3. Quit
What would you like to do?
The method should read in the users option, verify that it is valid (i.e. that it is either 1, 2 or 3) and then return that option. If the user enters an invalid option, the method should display an error message to the user and reprompt them until a valid option is entered.
public static void encrypt(File inputFile, int key)
This method should accept a text file and key as input and create an encrypted file using the supplied key and the Caesar cipher. Your program should only encrypt files with a .txt extension. The corresponding encrypted file should have the same filename, but the txt extension should be replaced with an enc extension. For example, if the input file is called myTest.txt then the output file would be called myTest.enc. If the user tries to encrypt a file that does not have a txt extension, then your program should display an error message and redisplay the menu.
public static void decrypt(File inputFile, int key)
This method should accept an encrypted file and key as input and create a decrypted file using the supplied key and the Caesar cipher. Your program should only decrypt files with a .enc extension. The corresponding decrypted file should have the same filename, but the enc extension should be replaced with a txt extension. For example, if the input file is called myTest.enc then the output file would be called myTest.txt. If the user tries to decrypt a file that does not have an enc extension, then your program should display an error message and redisplay the menu.
Your program should have any other methods you feel are necessary for it to operate correctly. Your main method should not do anything significant by itself it should only be a series of method calls with minimal control flow statements (if, switch, or loop constructs) around them.
Your project will be evaluated according to the following rubric. Each item is worth ten points.
The getMenuOption method meets the specified requirements
The encrypt method meets the specified requirements
The decrypt method meets the specified requirements
The Caesar cipher is correctly implemented
Invalid user inputs are handled as specified in the requirements
Programming style and documentation (including that the main method contains only method calls and control flow constructs)
**Right now, I'm having problems figuring out how to get the user to input a file name and then read that specific file so that I can encrypt or decrypt it. I made a new file inside of my project folder and I was trying to read from that specific file, but all I'm getting is a FileNotFound Exception. I'm relatively new with java so any help would be great.
This is my code:
package project3_kreger;
import java.io.File; import java.io.PrintWriter; import java.util.Scanner;
public class Project3_Kreger {
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static int getMenuOption() throws Exception { Scanner input = new Scanner(System.in); System.out.print("1. Encrypt 2. Decrypt 3. Quit What would you like to do? "); int options = input.nextInt();
while (options < 1 || options > 3) { System.out.println("You can only choose numbers 1-3. Please try again!"); options = input.nextInt(); }
if (options == 3) { System.exit(0); }
return options;
}
public static boolean extensionChecktxt(String inputFile) throws Exception { boolean ext = true; File file = new File(inputFile); String[] fname = inputFile.split("\\."); if (fname[1].equals("txt")) { ext = true; } else { return false; } return ext; }
public static boolean extensionCheckenc(String inputFile) throws Exception { boolean ext = true; File file = new File(inputFile); String[] fname = inputFile.split("\\."); if (fname[1].equals("enc")) { ext = true; } else { return false; } return ext; }
public static void encrypt(File inputFile, int key) throws Exception { Scanner input = new Scanner(inputFile); while (input.hasNext()) { String line = input.nextLine(); line = line.toLowerCase(); String crypt = ""; int count = 0; for (int i = 0; i < line.length(); i++) { int position = ALPHABET.indexOf(line.charAt(i)); int keyValue = (key + position) % 26; char replace = ALPHABET.charAt(keyValue); crypt += replace; } } input.close();
}
public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); getMenuOption(); System.out.println("Enter file name:"); String fileName = input.next(); File file = new File(fileName); System.out.println("What is the value for the key?"); int key = input.nextInt(); if (extensionChecktxt(fileName) == true) { encrypt(file, key); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
