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

create a program that takes in a sentence from the user and encrypts it. You can

ID: 3689615 • Letter: C

Question

create a program that takes in a sentence from the user and encrypts it. You can create any encryption pattern you want however; your program only has to encrypt English letters. The final program should allow the user to enter a sentence and provide the option to encrypt, decrypt, and exit. Your program must have the following to receive full credit a minimum of 2 arrays, a method that prints out the main menu, a method for encrypting, and a method for decrypting.

To help you get started, remember that a string is an array of characters, and that in java strings are considered to be classes and therefore have methods. Please conceder the following example.

String temp = “hello”;

For(int i = 0 ; i < temp.length(); i++){

            char t = temp.charAt(i);

            System.out.println(t);

     }

This will print the following:

h

e

l

l

o

Also you might fined this helpful as well in java you can add strings together to make one bigger string, and the following will show you how.

           

            String temp = “ “;

For(int i = 0 ; i < 5; i++){

            Temp += i;

}

System.out.println(temp);

This will print out the following string: “01234”

This is an example to help you: 1) encrypt: 2) decrypt: 3) exit: Pleaz enter the text you want to encrypt: abc d mnb v 1) encrypt: 2) decrypt: 3) exit: 2 Pleaz enter the text you want to decrypt: mnb v abc d 1) encrypt: 2) decrypt: 3) exit: Good by -

Explanation / Answer

EncryptDecrypt.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class EncryptDecrypt {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       char enc[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
       char dec[] = {'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L'};
       String text = "";
       BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
       while(true){
       System.out.println("Enter your Choice: 1) Encrypt 2) Decrypt 3) Exit");
       int choice = Integer.parseInt(scan.readLine());
       if(choice == 1){
           text = doEncrypt(scan, enc, dec);
           System.out.println(text);
       }
       else if(choice == 2){
           text = doDecrypt(scan, enc, dec);
           System.out.println(text);
       }
       else if(choice == 3){
           break;
       }
      
       }
   }
   public static String doEncrypt(BufferedReader scan, char enc[], char dec[]) throws IOException{
       System.out.println("Please enter the text you want to decrypt");
       String text = scan.readLine();
       String str = "";
       for(int i=0; i<text.length(); i++){
           for(int j=0; j<enc.length; j++){
           if(Character.toUpperCase(text.charAt(i)) == Character.toUpperCase(enc[j])){
               str = str + dec[j];
               break;
           }
           }
       }
       return str;
   }
   public static String doDecrypt(BufferedReader scan, char enc[], char dec[]) throws IOException{
       System.out.println("Please enter the text you want to decrypt");
       String text = scan.readLine();
       String str = "";
       for(int i=0; i<text.length(); i++){
           for(int j=0; j<dec.length; j++){
           if(Character.toUpperCase(text.charAt(i)) == Character.toUpperCase(dec[j])){
               str = str + enc[j];
               break;
           }
           }
       }
       return str;
   }  

}

Output:

Enter your Choice:
1) Encrypt
2) Decrypt
3) Exit
1
Please enter the text you want to decrypt
ANMG
MZYS
Enter your Choice:
1) Encrypt
2) Decrypt
3) Exit
2
Please enter the text you want to decrypt
MZYS
ANMG
Enter your Choice:
1) Encrypt
2) Decrypt
3) Exit
3