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

JAVA CODE: A main method that: i) reads they key, ii) creates Array of Letters,

ID: 3697804 • Letter: J

Question

JAVA CODE:

A main method that:

i) reads they key,

ii) creates Array of Letters,

iii) and gives a user the following options in a menu:

1. Read key. 2. Print key. 3. Encrypt message. 4. Decrypt message. 5. Exit Option 1 – Read key will allow the user to provide the name of the file with the key. Option 2 – Print key will print the key as per instructions above. Option 3 – Encrypt message will ask the user for a sentence and print the sentence encrypted. Option 4 – Decrypt message will ask the user for a sentence and print the sentence decrypted. Option 5 – Exit will terminate the program and print “Thanks for visiting my encryption system – [YOUR FIRST NAME, YOUR LAST NAME]”.

Explanation / Answer

//program:


package encryption;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class EncryptDecrypt {

public static void main(String[] args) throws FileNotFoundException {
String key="";
String message="";
String firstname;
String lastname;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your last name : ");
lastname=scan.next();
System.out.println("Enter your first name : ");
firstname=scan.next();
while(true){
System.out.println("1. Read key. 2. Print key. 3. Encrypt message. 4. Decrypt message. 5. Exit");
System.out.println("Enter your choice: ");

int choice=Integer.parseInt(scan.next());
scan.nextLine();
switch(choice)
{
case 1:
System.out.println("Enter your Filename contains key: ");
String file=scan.next();
Scanner fileScan=new Scanner(new FileReader(file));
key=fileScan.next();
break;
case 2:
System.out.println("Key: "+key);
break;
case 3:
System.out.println("Enter the message to encrypt: ");
message=scan.nextLine();
encrypt(message,key);
break;
case 4:
System.out.println("Enter the message to decrypt: ");
scan.next();
message=scan.nextLine();
decrypt(message,key);
break;
case 5:
System.out.println("Thanks for visiting my encryption system – "+firstname+" "+lastname+".");
System.exit(0);
}   
}
}
  
//Decryption using XOR algorithm      

public static void decrypt(String message,String key)
{

char k = key.charAt(0); //use same as per encryption

String unmasked = "";
//Decrypting message character by character
for (int i = 0; i < message.length(); i++)
{
unmasked+= (char)(message.charAt(i) ^ k);
}
System.out.println("Decrypted Message: "+unmasked);
}      
//Encryption using XOR algorithm
public static void encrypt(String message,String key)
{

char k = key.charAt(0); //Any char we can use
String masked = "";
//encrypting message character by character
for (int i = 0; i < message.length(); i++)
{
masked+= (char)(message.charAt(i) ^ k);
}
System.out.println("Encrypted Message: "+masked);
}
}

//input file: keyfile.txt

M