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...
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...
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
(Java)// Slightly different problem from the others that use the same input files, it has been...
(Java)// Slightly different problem from the others that use the same input files, it has been adapted from a practice problem// // Also if you can keep the code as simple as possible, try to avoid any advanced techniques, so that I can easily understand what is happening in case I decide to use this for future reference (which most likely I am) that'd be super appreciated// Deposit and Withdrawal Files: Use Notepad or another text editor to create a...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are interested in a one­tail test described in the following fashion: Ho: u < or = to 200 CFM; H1: u > 200 CFM. At 5% significance level, we can reject the null hypothesis given the sample information in AFE_Test1. we can reject the null hypothesis given the sample information in AFE_Test2. we cannot reject the null hypothesis. we can reject the null hypothesis given...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT