Question

In: Computer Science

JAVA There is a folder named Recursive folder at the same location as these below files,...

JAVA

There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted.

You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt()

The names of files and folders are stored in one string. I don't know where to begin.

functions.

Can anyone help me write encrypt and decrypt method using keyword encryption to encrypt and decrypt directory files and folders. The names of the files and folders are stored in fullDirectory now it needs to be encrypted and decrypted. Only the file names and extension names need to be encrypted and decrypted. leaving (File:, Folder: and ., [, and ,] characters unchanged

Thanks in advance

// Encryptable interface

public interface Encryptable
{
public void encrypt();
public void decrypt();
}

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

// file encryption class that implements Encyptable interface

import java.io.File;

public class FileNameEncryption implements Encryptable
{

private String fullDirectory = "";
private File directoryList[ ];
private boolean isEncrypted;
  public FileNameEncryption(String path)
{
  
if(path != null && path != "")
{   
File maindir = new File(path);
  
if ( maindir.exists() && maindir.isDirectory() )
{ directoryList = maindir.listFiles();
fullDirectory = "";
createFullDirectory(0);
isEncrypted = false;
}
}
}

private void createFullDirectory(int index){
  
  
if (index == directoryList.length){
return ;
}
  
else{
if(directoryList[index].isDirectory()){
fullDirectory +="Folders: " +"[" + directoryList[index].getName() + "]\n";
}

if (directoryList[index].isFile()){
fullDirectory += "File:\t " + directoryList[index].getName() + "\n";
}   
createFullDirectory(index + 1);
}
}

public String getFullDirectory()
{
return fullDirectory;
}
public boolean directoryIsEncrypted()
{
return isEncrypted;
}

public void encrypt(){
  
}

public void decrypt()
{
  
}

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

// Driver class

import java.io.File;
  
public class DirectoryDriver
{

  
public static void main(String[] args)
{

String mainDirectory = "Recursion Folder"; //this directory contains three folders and two files/ only the names need to be encrypted not the files and folders themselves
       FileNameEncryption directoryToList = new FileNameEncryption(mainDirectory);
       System.out.println(directoryToList.getFullDirectory());

directoryToList.encrypt();
System.out.println(directoryToList.getFullDirectory());

directoryToList.decrypt();
System.out.println(directoryToList.getFullDirectory());

          
}
}

Solutions

Expert Solution

The Encrypt and decrypt method is implemented

// file encryption class that implements Encyptable interface

import java.io.File;

public class FileNameEncryption implements Encryptable {

    private String fullDirectory = "";
    private File directoryList[];
    private boolean isEncrypted;
    private String plaintext = "abcdefghijklmnopqrstuvwxyz"; //Plain text for ciphering
    private final String KEYWORD = "KEYWORD"; //Use the suitable keyword for cipher of your choice.

    public FileNameEncryption(String path) {

        if (path != null && path != "") {
            File maindir = new File(path);

            if (maindir.exists() && maindir.isDirectory()) {
                directoryList = maindir.listFiles();
                fullDirectory = "";
                createFullDirectory(0);
                isEncrypted = false;
            }
        }
    }

    private void createFullDirectory(int index) {


        if (index == directoryList.length) {
            return;
        } else {
            if (directoryList[index].isDirectory()) {
                fullDirectory += "Folders: " + "[" + directoryList[index].getName() + "]\n";
            }

            if (directoryList[index].isFile()) {
                fullDirectory += "File:\t " + directoryList[index].getName() + "\n";
            }
            createFullDirectory(index + 1);
        }
    }

    public String getFullDirectory() {
        return fullDirectory;
    }

    public boolean directoryIsEncrypted() {
        return isEncrypted;
    }

    public void encrypt() {
        String[] parsedList = parseString(fullDirectory); //parse the 'fullDirectory' to a string array
        fullDirectory = encrypt(parsedList); //Encrypt and store 'fullDirectory'
    }

    public void decrypt() {
        String[] parsedList = parseString(fullDirectory); //parse the 'fullDirectory' to a string array
        fullDirectory = decrypt(parsedList); //Decrypt and store 'fullDirectory'
    }

    //Method for parsing the input string to list of strings on the basis of given delimiters
    String[] parseString(String str) {
        ArrayList<String> parsedRes = new ArrayList<String>();
        return str.split("((?<=:)|(?=:))|((?<=\\.)|(?=\\.))|((?<=\\[)|(?=\\[))|((?<=\\])|(?=\\]))");

    }
    //Encrypting the list of strings on the basis of our requirement
    String encrypt(String[] list) {
        for (int i = 0; i < list.length; i++) {
            switch (list[i]) {
                case "Folder":
                case "File":
                case "\\.":
                case "\\:":
                case "\\[":
                case "\\]":
                    continue;
                default:
                    list[i] = encryptWord(list[i]); //Encrypt each word in list
                    break;
            }
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.length; i++) {
            sb.append(list[i]);
        }
        String str = sb.toString();
        return str;
    }
    //Decrypting the list of strings on the basis of our requirement
    String decrypt(String[] list) {
        for (int i = 0; i < list.length; i++) {
            switch (list[i]) {
                case "Folder":
                case "File":
                case "\\.":
                case "\\:":
                case "\\[":
                case "\\]":
                    continue;
                default:
                    list[i] = decryptWord(list[i]); //Decrypting each word
                    break;

            }
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.length; i++) {
            sb.append(list[i]);
        }
        String str = sb.toString();
        return str;
    }
    //The Encrypting algorithm for keyword cipher
    String encryptWord(String word) {
        String encoded = encoder(KEYWORD.toCharArray());
        String cipher = "";

        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) >= 'a' && word.charAt(i) <= 'z') {
                int pos = word.charAt(i) - 97;
                cipher += encoded.charAt(pos);
            } else if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') {
                int pos = word.charAt(i) - 65;
                cipher += encoded.charAt(pos);
            } else {
                cipher += word.charAt(i);
            }
        }
        return cipher;
    }

    //The decrypting algorithm for keyword cipher
    String decryptWord(String encodedWord) {
        String encoded = encoder(KEYWORD.toCharArray());
        Map<Character, Integer> enc = new HashMap<Character, Integer>();
        for (int i = 0; i < encoded.length(); i++) {
            enc.put(encoded.charAt(i), i);
        }

        String decipher = "";

        for (int i = 0; i < encodedWord.length(); i++) {
            if (encodedWord.charAt(i) >= 'a' && encodedWord.charAt(i) <= 'z') {
                int pos = enc.get(encodedWord.charAt(i) - 32);
                decipher += plaintext.charAt(pos);
            } else if (encodedWord.charAt(i) >= 'A' && encodedWord.charAt(i) <= 'Z') {
                int pos = enc.get(encodedWord.charAt(i));
                decipher += plaintext.charAt(pos);
            } else {
                decipher += encodedWord.charAt(i);
            }
        }
        return decipher;
    }

    //Helper method for encoding the keyword chosen.
    String encoder(char[] keyword) {
        String encoded = "";

        boolean[] arr = new boolean[26];

        for (int i = 0; i < keyword.length; i++) {
            if (keyword[i] >= 'A' && keyword[i] <= 'Z') {
                if (arr[keyword[i] - 65] == false) {
                    encoded += (char) keyword[i];
                    arr[keyword[i] - 65] = true;
                }
            } else if (keyword[i] >= 'a' && keyword[i] <= 'z') {
                if (arr[keyword[i] - 97] == false) {
                    encoded += (char) (keyword[i] - 32);
                    arr[keyword[i] - 97] = true;
                }
            }
        }
        for (int i = 0; i < 26; i++) {
            if (arr[i] == false) {
                arr[i] = true;
                encoded += (char) (i + 65);
            }
        }
        return encoded;
    }
}

Related Solutions

Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder....
Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder. For this assignment you will be creating a total of 2 classes that meet the definitions below. This assignment will allow the user to enter in the coefficients for a polynomial function of degree 2. The user will then be asked to enter a number. The program will use that number as an argument to the function, and will print the corresponding function value....
Use the data set named Store_Visits located in the folder Data Files for HW Assignment (outside...
Use the data set named Store_Visits located in the folder Data Files for HW Assignment (outside of Minitab folder) in the K-drive. The response variable y is the number of visits of a customer to a particular food store in a large suburban area within the period of a month, and the independent variable x is the distance (in miles) of the customer’s home to the store. Fit a simple linear regression model to the data, and answer the following...
Use the data set named Store_Visits located in the folder Data Files for HW Assignment (outside...
Use the data set named Store_Visits located in the folder Data Files for HW Assignment (outside of Minitab folder) in the K-drive. The response variable y is the number of visits of a customer to a particular food store in a large suburban area within the period of a month, and the independent variable x is the distance (in miles) of the customer’s home to the store. Fit a simple linear regression model to the data, and answer the following...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
I. At the beginning, create a folder named "test" and add the folder into the version...
I. At the beginning, create a folder named "test" and add the folder into the version control tool tracking system. Then create one file named "001" under this folder. Commit any change if necessary. II. Next, add another file named "002" in the same folder and commit the change ("adding") in the version control tool. III. Followed by adding that file, create a branch (namely, "branch-A") in the version control tool and make some changes to the contents in file...
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
Week 4 Project - Submit Files Hide Submission Folder Information Submission Folder Week 4 Project Instructions...
Week 4 Project - Submit Files Hide Submission Folder Information Submission Folder Week 4 Project Instructions Changes in Monetary Policy Assume that the Bank of Ecoville has the following balance sheet and the Fed has a 10% reserve requirement in place: Balance Sheet for Ecoville International Bank ASSETS LIABILITIES Cash $33,000 Demand Deposits $99,000 Loans 66,000 Now assume that the Fed lowers the reserve requirement to 8%. What is the maximum amount of new loans that this bank can make?...
What is the name of the folder in the Windows system folder that contains files used in the boot process and regularly opened by other programs?
What is the name of the folder in the Windows system folder that contains files used in the boot process and regularly opened by other programs? 1. User 2. Journal 3. svchost 4. Prefetch
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT