Question

In: Computer Science

Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...

Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices.

  1. Read in a file
  2. Print the file to the console
  3. Encrypt the file and write it to the console
  4. Write out the encrypted file to a text file
  5. Clear the data in memory
  6. Read in an encrypted file
  7. Decrypt the file
  8. Write out the decrypted file to the console
  9. End

Your encryption program should work like a filter, reading the contents of one file modifying the data into a code, and then writing the coded contents out to a second file. Do not hard code the path location! Use a simple one from the shift category encryption technique. Do not use a serialized file use a text file.

Solutions

Expert Solution

Screenshot

Program

/**
* Program to read and write file and encrypt decrypt data
* Here user get options for different operations
* Read user specified data encode or decode as per the requirements
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class EncryptDecrypt {
   public static void main(String[] args) {
       //Variable for data
       String data="",encrypt="",decrypt="";
       //User options
       int opt=menu();
       //Loop until exit
       while(opt!=9) {
           //Read a file specified by user and store into data
           if(opt==1) {
               data=readFile();
           }
           //Print data value
           else if(opt==2) {
               printData(data);
           }
           //Encrypt data value and display encrypted on console
           else if(opt==3) {
               encrypt=encryptData(data);
               System.out.println("\nEncrypted Data: ");
               printData(encrypt);
           }
           //Write encrypted data into user specified file
           else if(opt==4) {
               writeEncrypt(encrypt);
               System.out.println("\nSuccessfully wrote encrypted data!!");
           }
           //Clear data
           else if(opt==5) {
               data=null;
               encrypt=null;
               decrypt=null;
               System.out.println("\nClear memory of data value!!!");
           }
           //Read a file data for decryption
           else if(opt==6) {
               data=readFile();
           }
           //Decrypt
           else if(opt==7) {
               decrypt=decryptData(data);
               System.out.println("\nDecryption completed");
           }
           //Display decrypted information
           else if(opt==8) {
               System.out.println("\nDecrypted data: ");
               printData(decrypt);
           }
           opt=menu();
       }
       System.out.println("\nExiting Program............");
   }
   //Get menu
   //Show options and prompt for input
   //Error check and return opt
   public static int menu() {
       Scanner sc=new Scanner(System.in);
       System.out.println("USER OPTIONS:");
       System.out.println("\r\n" +
               "1. Read in a file\r\n" +
               "2. Print the file to the console\r\n" +
               "3. Encrypt the file and write it to the console\r\n" +
               "4. Write out the encrypted file to a text file\r\n" +
               "5. Clear the data in memory\r\n" +
               "6.   Read in an encrypted file\r\n" +
               "7.   Decrypt the file\r\n" +
               "8.   Write out the decrypted file to the console\r\n" +
               "9.   End\r\n" +
               "");
       System.out.println("Enter your choice: ");
       int opt=sc.nextInt();
       while(opt<1 || opt>9) {
           System.out.println("Error!!!Option should be 1-9.Pease re-enter");
           System.out.println("Enter your choice: ");
           opt=sc.nextInt();
       }
       sc.nextLine();
       return opt;
   }
   //Read data from file and return string of data
   public static String readFile() {
       Scanner sc=new Scanner(System.in);
       StringBuffer data=new StringBuffer();
       System.out.println("\nEnter file name to read: ");
       String filename=sc.nextLine();
       try {
           Scanner sc1=new Scanner(new File(filename));
           while(sc1.hasNextLine()) {
               data.append(sc1.nextLine()+"\n");
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       return data.toString();
   }
   //Display passed data on console
   public static void printData(String str) {
       System.out.println("\n"+str);
   }
   //Encrypt data using simple shift add method
   //Check each character , if alphebet add with shift value
   //Here i take fixed shift value
   public static String encryptData(String data) {
       char ch;
       int shift =5;
        String encrypt = "";
        for(int i=0; i < data.length();i++)
        {
            ch = data.charAt(i);
            if(ch >= 'a' && ch <= 'z')
            {
             ch = (char) (ch + shift);
             if(ch> 'z') {
                ch = (char) (ch+'a'-'z'-1);
             }
             encrypt = encrypt + ch;
            }
          
            else if(ch >= 'A' && ch <= 'Z') {
             ch = (char) (ch + shift);  
             if(ch > 'Z') {
                 ch = (char) (ch+'A'-'Z'-1);
             }
             encrypt = encrypt + ch;
            }
            else {
             encrypt = encrypt +ch;
            }
        }
        return encrypt;
   }
   //Write encrypted data into user specified file
   public static void writeEncrypt(String data) {
       Scanner sc=new Scanner(System.in);
       System.out.println("\nEnter file name to write encrypted data: ");
       String filename=sc.nextLine();
       try {
           FileWriter fw=new FileWriter(filename);
           fw.write(data);
           fw.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
   //Decrypt data using simple shift sub method
   //Check each character , if alphebet subtract with shift value
   //Here i take fixed shift value
   public static String decryptData(String data) {
       char ch;
       int shift =5;
        String decrypt = "";
       for(int i=0; i < data.length();i++)

        {
             ch = data.charAt(i);
            if(ch >= 'a' && ch <= 'z')
            {
                ch = (char) (ch - shift);
                if(ch < 'a') {
                    ch = (char) (ch-'a'+'z'+1);
                }
                decrypt= decrypt + ch;
            }  
            else if(ch >= 'A' && ch <= 'Z')
            {
                ch = (char) (ch - shift);
                if (ch < 'A') {
                    ch = (char) (ch-'A'+'Z'+1);
                }
                decrypt= decrypt +ch;          
            }
            else
            {
             decrypt = decrypt +ch;          
            }
        }
       return decrypt;
   }
}

---------------------------------------------------

Output

USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
1

Enter file name to read:
datafile.txt
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
2

Your encryption program should work like a filter, reading the contents of one file modifying
the data into a code, and then writing the coded contents out to a second file.

USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
3

Encrypted Data:

Dtzw jshwduynts uwtlwfr xmtzqi btwp qnpj f knqyjw, wjfinsl ymj htsyjsyx tk tsj knqj rtinkdnsl
ymj ifyf nsyt f htij, fsi ymjs bwnynsl ymj htiji htsyjsyx tzy yt f xjhtsi knqj.

USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
4

Enter file name to write encrypted data:
encryptedfile.txt

Successfully wrote encrypted data!!
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
5

Clear memory of data value!!!
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
2

null
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
6

Enter file name to read:
encryptedfile.txt
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
7

Decryption completed
USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
8

Decrypted data:

Your encryption program should work like a filter, reading the contents of one file modifying
the data into a code, and then writing the coded contents out to a second file.

USER OPTIONS:

1. Read in a file
2. Print the file to the console
3. Encrypt the file and write it to the console
4. Write out the encrypted file to a text file
5. Clear the data in memory
6.   Read in an encrypted file
7.   Decrypt the file
8.   Write out the decrypted file to the console
9.   End

Enter your choice:
9

Exiting Program............

------------------------------------------------------

Note:-

I assume you are expecting this way


Related Solutions

Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
java code Write a program that gives the user a menu of six choices (use integers)...
java code Write a program that gives the user a menu of six choices (use integers) to select from. The choices are circle, triangle, cone, cylinder, sphere, and quit. (The formulas are given below.) Once the figure is calculated, an informative message should be printed and the user shown the menu again, so that another choice can be made. The formulas are: Area of circle: a = 3.14 * radius * radius Area of triangle: a = ½ base *...
print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT