-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileNameEncryption.java
More file actions
330 lines (307 loc) · 11.1 KB
/
FileNameEncryption.java
File metadata and controls
330 lines (307 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
(1) Name: Dakota Morgan
(2) Date: 10/21/2019
(3) Instructor: Ms Tucker
(4) Class: CIT249 Java II
(5) Purpose: Use recursion to explore a file structure, encrypt the file and folder names.
**/
import java.io.File;
public class FileNameEncryption implements Encryptable
{
/* ---------------------------------------------------------------------------
* Class Data:
*
* fullDirectory: Formattted output which represents the directory structure
* directorList[ ]: Array of File objects for the directory structure
* --------------------------------------------------------------------------*/
private String fullDirectory = "";
private File directoryList[ ];
private boolean isEncrypted;
private String fileType;
/*
* initialize indent
*/
private String indent = "";
private static final char[] key = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N'
,'O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','0','1','2','3','4','5','6','7','8','9','[',']','.'};
private static final char[] cipher = {'C','O','P','Y','R','I','G','H','T','E','D',' ','2','0'
,'1','9','A','B','F','J','K','L','M','N','Q','S','U','V','W','X','Z','3','4','5','6','7','8','[',']','.'};
/* ---------------------------------------------------------------------------
* THIS IS A RECURSIVE METHOD - DO NOT CHANGE THE PARAMETERS
* OR RETURNED DATA TYPE.
*
* RECUSION IS NOT USED FOR FILES IN THE STRUCURE - ONLY FOLDER PROCESSING
* IS RECURSIVE.
*
* Parameters:
* directory: A File object which is an array of File objects (files and folders)
* level: Used to tab at the appropriate level in the display.
* --------------------------------------------------------------------------*/
private void createFullDirectory(File[] directory, int index)
{
/*
* if index reaches directory length, done
*/
if(index >= directory.length){
return;
}
else{
/*
* if object at index is file, print file name
*/
if(directory[index].isFile()){
fullDirectory += indent + "FILE:\t" + directory[index].getName() + "\n";
}
/*
* if object at index is directory, print directory name
*/
else{
fullDirectory += indent + "FOLDER:\t[" + directory[index].getName() + "]\n";
/*
* add an indention
*/
indent += " ";
/*
* explore directory with recursion
*/
createFullDirectory(directory[index].listFiles(), 0);
/*
* remove indention
*/
indent = indent.substring(0, indent.length() - 5);
}
/*
* check next index
*/
createFullDirectory(directory, index + 1);
}
}
/* ---------------------------------------------------------------------------
* Class constructor
*
* The directory is validated and then a File array is created if valid.
* The createFullDirectory method is called to create the formatted display of
* of the directory.
* --------------------------------------------------------------------------*/
public FileNameEncryption(String path)
{
if(path != null && path != "")
{
File maindir = new File(path);
if ( maindir.exists() && maindir.isDirectory() )
{ directoryList = maindir.listFiles();
fullDirectory = "";
createFullDirectory(directoryList, 0);
isEncrypted = false;
}
}
}
/* ---------------------------------------------------------------------------
* Accessors for fullDirectory and isEncrypted
* --------------------------------------------------------------------------*/
public String getFullDirectory()
{
return fullDirectory;
}
public boolean directoryIsEncrypted()
{
return isEncrypted;
}
/* ---------------------------------------------------------------------------
* encrypt method for fullDirectory
* --------------------------------------------------------------------------*/
public final void encrypt()
{
/**
* Replace each character in string with cipher[] character equivalent
*/
if (!directoryIsEncrypted()){
String masked = "";
int index = 0;
boolean eReady = false;
boolean lineDone = false;
boolean found = false;
/*
* loop through string one index at a time
*/
while(index < fullDirectory.length()){
/*
* Do operations for one line at a time
*/
while(lineDone != true){
/*
* if new line is encountered, line is over
*/
if(fullDirectory.charAt(index) == '\n'){
lineDone = true;
}
/*
* if colon is encountered, rest of line is ready to be encrypted, add colon to string
*/
else if(fullDirectory.charAt(index) == ':' && !eReady){
eReady = true;
masked = masked + fullDirectory.charAt(index);
}
/*
* record text before colon unencrypted
*/
else if(!eReady){
masked = masked + fullDirectory.charAt(index);
}
/*
* if colon has been encountered, string is ready to be encrypted
*/
if(eReady){
/*
* for each char in cipher, check if char at index matches key
*/
for (int j = 0; j < key.length; j++){
/*
* if index matches key and a char hasn't already been found at this index
*/
if (fullDirectory.charAt(index) == (char) key[j] && !found){
/*
* add matching cipher char to encrypted string
*/
masked = masked + (char) (cipher[j]);
/*
* mark char found
*/
found = true;
}
/*
* if index matches lowercase version of key
* and a char hasn't already been found at this index
*/
else if (fullDirectory.charAt(index) == Character.toLowerCase(key[j])
&& !found && Character.isLetter(fullDirectory.charAt(index))){
/*
* add matching cipher char to encrypted string
*/
masked = masked + Character.toLowerCase(key[j]);
/*
* mark char found
*/
found = true;
}
}
}
/*
* increment index, reset found
*/
index++;
found = false;
}
/*
* new line, reset eReady and lineDone, add newline
*/
eReady = false;
lineDone = false;
masked = masked + '\n';
}
/*
* set fullDirectory to encrypted string
*/
fullDirectory = masked;
isEncrypted = true;
}
}
/* ---------------------------------------------------------------------------
* decrypt method for fullDirectory
*
* --------------------------------------------------------------------------*/
public final String decrypt()
{
/**
* Replace each character in string with key[] character equivalent
*/
String unmasked = "";
if (directoryIsEncrypted()){
int index = 0;
boolean eReady = false;
boolean lineDone = false;
boolean found = false;
/*
* loop through string one index at a time
*/
while(index < fullDirectory.length()){
/*
* Do operations for one line at a time
*/
while(lineDone != true){
/*
* if new line is encountered, line is over
*/
if(fullDirectory.charAt(index) == '\n'){
lineDone = true;
}
/*
* if colon is encountered, rest of line is ready to be encrypted, add colon to string
*/
else if(fullDirectory.charAt(index) == ':' && !eReady){
eReady = true;
unmasked = unmasked + fullDirectory.charAt(index);
}
/*
* record text before colon unencrypted
*/
else if(!eReady){
unmasked = unmasked + fullDirectory.charAt(index);
}
/*
* if colon has been encountered, string is ready to be encrypted
*/
if(eReady){
/*
* for each char in cipher, check if char at index matches cipher
*/
for (int j = 0; j < cipher.length; j++){
if (fullDirectory.charAt(index) == (char) cipher[j] && !found){
/*
* add matching key char to unencrypted string
*/
unmasked = unmasked + (char) (key[j]);
/*
* mark char found
*/
found = true;
}
/*
* if index matches lowercase version of key
* and a char hasn't already been found at this index
*/
else if (fullDirectory.charAt(index) == Character.toLowerCase(cipher[j])
&& !found && Character.isLetter(fullDirectory.charAt(index))){
/*
* add matching key char to unencrypted string
*/
unmasked = unmasked + Character.toLowerCase(cipher[j]);
/*
* mark char found
*/
found = true;
}
}
}
/*
* increment index, reset found
*/
index++;
found = false;
}
/*
* new line, reset eReady and lineDone, add newline
*/
eReady = false;
lineDone = false;
unmasked = unmasked + '\n';
}
/*
* set fullDirectory to unencrypted string
*/
isEncrypted = false;
fullDirectory = unmasked;
}
return unmasked;
}
}