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

File encryption is the science of writing the contents of a file in a secret cod

ID: 3706731 • Letter: F

Question

File encryption is the science of writing the contents of a file in a secret code. 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. There are very complex encryption techniques, but for ours, will use a simple one from the shift category. We will take each character and shift it one place in the Unicode sequence. So ‘a’ becomes ‘b’, ‘b’ becomes ‘c’, etc. What about ‘z’? You could make it ‘a’ or just the next Unicode character, which is the choice we will take. NOTE: you must be writing and reading a text file, not a serializable one, and you must have a FileDialog ox where the user can choose the file to utilize.

      Write a program names EncryptDecrypt.java that has the following menu choices:

I would like to use this stub methods to write the program:

public class EncryptDecrypt {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       ArrayList<String> file = new ArrayList<String>();
       int ans = 0;
       while (true) {
           menu();
           System.out.println("Make a selection:");
           ans = scan.nextInt();
           if (ans == 1) {
               file = readText();

           }
           if (ans == 2) {
               printToConsole(file);
           }
           if (ans == 3) {
               file = encryptFile(file);
               printToConsole(file);

           }
           if (ans == 4) {
               writeText(file);

           }
           if (ans == 5) {
               file.clear();
           }
           if (ans == 6) {
               file = readText();
           }
           if (ans == 7) {
               file = decryptFile(file);
           }
           if (ans == 8) {
               printToConsole(file);
           }
           if (ans == 9) {
               System.out.println("See you later!");
               System.exit(0);
           }
       }

   }

   //menu item 1 and 6
   public static ArrayList<String> readText() {
           return null;
      
   }
   //menu item 4
   public static void writeText(ArrayList<String> file) {
      
   }
   //menu items 2,3,8
   public static void printToConsole(ArrayList<String> file) {
      
   }
   //menu item 3
   public static ArrayList<String> encryptFile(ArrayList<String> file) {
       return null;

   }
   //menu item 7
   public static ArrayList<String> decryptFile(ArrayList<String> file) {
       return null;
   }

  

   public static void menu() {
       System.out.println(" 1. Read in a file");
       System.out.println("2. Print the file to the console");
       System.out.println("3. Encrypt the file and write it to the console");
       System.out.println("4. Write out the encrypted file to a text file");
       System.out.println("5. Clear the date in memory");
       System.out.println("6. Read in an encrypted file");
       System.out.println("7. Decrypt the file");
       System.out.println("8. Write out the decrypted file to the console");
       System.out.println("9. exit ");
   }

}

Explanation / Answer

I don't understand why you need ArrayList, so I removed it.

Below is the complete program:

package com.chegg;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.util.Scanner;

/**
* Created by Nirupom Biswas on 13-Apr-18.
*/

public class EncryptDecrypt {
    static File selectedFile = null;
    static boolean isEncrypted = false;
    static StringBuilder currentString;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int ans = 0;
        while (true) {
            menu();
            System.out.println("Make a selection:");
            ans = scan.nextInt();
            if (ans == 1) {
                readText();

            }
            if (ans == 2) {
                printToConsole();
            }
            if (ans == 3) {
                encryptFile();
                printToConsole();

            }
            if (ans == 4) {
                writeText();

            }
            if (ans == 5) {
                selectedFile = null;
                currentString = new StringBuilder();
           }
            if (ans == 6) {
                readText();
            }
            if (ans == 7) {
                decryptFile();
            }
            if (ans == 8) {
                printToConsole();
            }
            if (ans == 9) {
                System.out.println("See you later!");
                System.exit(0);
            }
        }

    }

    //menu item 1 and 6
   
public static void readText() {
        if (!getFile())
            return;
        readFile();
        return;

   }

    //menu item 4
   
public static void writeText() {
        if (selectedFile == null) {
            if (!getFile())
                return;
        } else if (!isEncrypted) {
            encryptString(readFile().toString());
        }
        writeFile();
    }

    //menu items 2,3,8
   
public static void printToConsole() {
        if (selectedFile == null) {
            if (!getFile())
                return;
            readFile();
        }
        System.out.println(currentString);
    }

    //menu item 3
   
public static void encryptFile() {
        if (selectedFile == null) {
            if (!getFile())
                return;
        }
        if (!isEncrypted)
            encryptString(readFile().toString());

        return;
    }

    //menu item 7
   
public static void decryptFile() {
        decryptString();
        return;
    }


    public static void menu() {
        System.out.println(" 1. Read in a file");
        System.out.println("2. Print the file to the console");
        System.out.println("3. Encrypt the file and write it to the console");
        System.out.println("4. Write out the encrypted file to a text file");
        System.out.println("5. Clear the date in memory");
        System.out.println("6. Read in an encrypted file");
        System.out.println("7. Decrypt the file");
        System.out.println("8. Write out the decrypted file to the console");
        System.out.println("9. exit ");
    }

    /**
     * Encrypt String data
     *
     * @param
plainText
    
* @return
    
*/
   
public static void encryptString(String plainText) {
        currentString = new StringBuilder();
        //Convert Larger String into one line string
       
int strLength = plainText.length();
        for (int i = 0; i < strLength; i++) {
            //Get Character at each position
           
char character = plainText.charAt(i);
            //Get ASCII Value of the character
           
int val = (int) character;
            char encryptedChar;
            //If in allowed character range, then encrypt else leave it as it is
           
if ((val >= 65 && val <= 90) || (val >= 97 && val <= 122) || (val >= 49 && val <= 57))
                encryptedChar = (char) (val + 1);
            else
               
encryptedChar = character;
            //Append encrypted data in global StringBuilder
           
currentString.append(encryptedChar);
        }
        //Mark program as encrypted
       
isEncrypted = true;
    }


    /**
     * Decrypt String data
     *
     * @return
    
*/
   
public static void decryptString() {
        //Convert Larger String into one line string
       
String plainText = currentString.toString();
        int strLength = plainText.length();
        currentString = new StringBuilder();
        for (int i = 0; i < strLength; i++) {
            //Get Character at each position
           
char character = plainText.charAt(i);
            //Get ASCII Value of the character
           
int val = (int) character;
            char decryptChar;
            //If in allowed character range, then decrypt else leave it as it is
           
if ((val >= 65 && val <= 90) || (val >= 97 && val <= 122) || (val >= 49 && val <= 57))
                decryptChar = (char) (val - 1);
            else
               
decryptChar = character;
            currentString.append(decryptChar);
        }
        isEncrypted = false;
    }

    /**
     * Choose File from file Chooser and return file
     *
     * @return
    
*/
   
public static boolean getFile() {
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = jfc.showOpenDialog(null);
        //Set to select File only
       
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        //Disable multi select
       
jfc.setMultiSelectionEnabled(false);
        //Allow only text files
       
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
        jfc.setFileFilter(filter);
        //If selected the return true else flase
       
if (returnValue == JFileChooser.APPROVE_OPTION) {
            selectedFile = jfc.getSelectedFile();
            System.out.println(selectedFile.getAbsolutePath());
            return true;
        }
        return false;
    }

    /**
     * Read Plain Text Content from a file
     *
     * @return
    
*/
   
public static StringBuilder readFile() {
        currentString = new StringBuilder();
        //Initiate reader
       
try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
            String line;
            //Read Each line and append to global StringBuilder
           
while ((line = reader.readLine()) != null) {
                currentString.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return currentString;
    }

    /**
     * Write to new file
     */
   
public static void writeFile() {
        //Generate new File name
       
String newFileName = selectedFile.getName().replaceFirst("[.][^.]+$", "") + "_" + System.currentTimeMillis() + ".txt";
        //Initiate Writer and write to new file
       
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(selectedFile.getPath().replace(selectedFile.getName(), "") + newFileName)))) {
            writer.write(currentString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

OUTPUT

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

1

C:UsersNirupom BiswasDesktopwww.hon3yhd.to.txt

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

2

Torrent downloaded from www.hon3yhd.toHon3yHD.to :: Bollywood / Hindi / Hollywood / English HD Movies

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

3

Upssfou epxompbefe gspn xxx.ipo4zie.upIpo4zIE.up :: Cpmmzxppe / Ijoej / Ipmmzxppe / Fohmjti IE Npwjft

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

4

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

6

C:UsersNirupom BiswasDesktopwww.hon3yhd.to_1523597360410.txt

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

7

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

8

Torrent downloaded from www.hon3yhd.toHon3yHD.to :: Bollywood / Hindi / Hollywood / English HD Movies

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 date in memory

6. Read in an encrypted file

7. Decrypt the file

8. Write out the decrypted file to the console

9. exit

Make a selection:

9

See you later!

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