Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need Help with this JAVA MAZE RUNNER PROJECT.. Can you please help me with the c

ID: 3605105 • Letter: N

Question

Need Help with this JAVA MAZE RUNNER PROJECT.. Can you please help me with the code and also add comments so I understand the situation..

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 ‘F’ because F 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 user’s 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. Note that at the ends of the alphabet, the ciphertext character may go outside the bounds of the normal alphabet, into characters like punctuation marks or accented letters. This is completely fine.

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 one point.

• 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

• The main method contains only method calls and control flow constructs – everything else is done inside the other methods

Explanation / Answer

Hi.. I have written the programs that meets your requirement. the program will promt user to enter from the menu. Please check the below code.

CipherText.java

package com.cipher;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class CipherText {

static Scanner sc = new Scanner(System.in);

final static String alphabet="abcdefghijklmnopqrstuvwxyz";

public static int getMenuOption(){

System.out.println("1. Encrypt");

System.out.println("2. Decrypt");

System.out.println("3. Exit");

System.out.println("What would you like to do?");

int input = sc.nextInt();

return input;

}

public static void encrypt(File inputFile, int key) throws IOException {

if(inputFile.exists()){

String fileName = inputFile.getName();

String extension = fileName.substring(fileName.lastIndexOf(".") + 1);

if(extension.equals("txt")){

BufferedReader b = new BufferedReader(new FileReader(inputFile));

String readLine = "";

String s = inputFile.getParent();

//System.out.println("Reading file using Buffered Reader"+s);

StringBuffer sb = new StringBuffer();

while ((readLine = b.readLine()) != null) {

//System.out.println(readLine);

readLine = readLine.trim();

String values[] = readLine.split(" ");

String total = "";

for(int i=0;i<values.length;i++){

String enc = values[i];

for(int k=0;k<enc.length();k++){

int val = alphabet.indexOf(enc.charAt(k))+1;

if(val!=0){

//System.out.println("index::"+val+" "+key);

int maxval = val+key;

if(maxval>26){

maxval = maxval-26;

}

//System.out.println(" "+alphabet.charAt(maxval-1));

total += alphabet.charAt(maxval-1);

}else{

total += enc.charAt(k);

}

}

total +=" ";

}

total+=" ";

sb.append(total);

//System.out.println("Total...."+total);

  

}

//System.out.println(sb);

s = s.replace("\", "/");

//System.out.println(s+" "+fileName);

String fff[] = fileName.split("\.");

s+="/"+fff[0]+".enc";

File destFile = new File(s);

if(destFile.exists())

destFile.delete();

destFile.createNewFile();

BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));

writer.write(sb.toString());

writer.close();

}else{

System.out.println("Please specify valid format. It only accepts txt files only");

}

}else{

System.out.println("File not exits");

}

}

public static void decrypt(File inputFile, int key) throws IOException {

if(inputFile.exists()){

String fileName = inputFile.getName();

String extension = fileName.substring(fileName.lastIndexOf(".") + 1);

if(extension.equals("enc")){

BufferedReader b = new BufferedReader(new FileReader(inputFile));

String readLine = "";

String s = inputFile.getParent();

//System.out.println("Reading file using Buffered Reader"+s);

StringBuffer sb = new StringBuffer();

while ((readLine = b.readLine()) != null) {

//System.out.println(readLine);

readLine = readLine.trim();

String values[] = readLine.split(" ");

String total = "";

for(int i=0;i<values.length;i++){

String enc = values[i];

for(int k=0;k<enc.length();k++){

int val = alphabet.indexOf(enc.charAt(k))+1;

if(val!=0){

//System.out.println("index::"+val+" "+key);

int maxval = val-key;

if(maxval<=0){

maxval = maxval+26;

}

//System.out.println(" "+alphabet.charAt(maxval-1));

total += alphabet.charAt(maxval-1);

}else{

total += enc.charAt(k);

}

}

total +=" ";

}

total+=" ";

sb.append(total);

  

}

s = s.replace("\", "/");

String fff[] = fileName.split("\.");

s+="/"+fff[0]+".txt";

File destFile = new File(s);

if(destFile.exists())

destFile.delete();

destFile.createNewFile();

BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));

writer.write(sb.toString());

writer.close();

}else{

System.out.println("Please specify valid format. It only accepts enc files only");

}

}else{

System.out.println("File not exits");

}

}

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

boolean flag = true;

while(flag){

int input = getMenuOption();

switch (input) {

case 1:

System.out.println("In encrypt");

System.out.println("Please specify the filename including path");

String s1="";

boolean m = true;

while(m){

s1 = sc.nextLine();

if(!s1.isEmpty()){

m=false;

}

  

}

File f1 = new File(s1);

System.out.println("Please enter key");

int key = sc.nextInt();

encrypt(f1,key);

break;

case 2:

System.out.println("In decrypt");

System.out.println("Please specify the filename including path");

s1="";

m = true;

while(m){

s1 = sc.nextLine();

if(!s1.isEmpty()){

m=false;

}

  

}

f1 = new File(s1);

System.out.println("Please enter key");

key = sc.nextInt();

decrypt(f1,key);

break;

case 3:

System.out.println("Exit");

flag=false;

break;

default:

System.out.println("Invalid option.Try Again!");

break;

}

}

}

}

Output:

1. Encrypt
2. Decrypt
3. Exit
What would you like to do?
1
In encrypt
Please specify the filename including path
cwfwv
Please enter key
5
File not exits
1. Encrypt
2. Decrypt
3. Exit
What would you like to do?
1
In encrypt
Please specify the filename including path
c:santhosh/test2.txt
Please enter key
5
File not exits
1. Encrypt
2. Decrypt
3. Exit
What would you like to do?
2
In decrypt
Please specify the filename including path
c:/santhosh/test2.enc
Please enter key
5
1. Encrypt
2. Decrypt
3. Exit
What would you like to do?
3
Exit

Please execute it and check the code once and let me know any querries.

Thanks a lot. all the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote