In: Computer Science
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());
}
}
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;
}
}