-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderLockerApp.java
184 lines (169 loc) · 6.06 KB
/
FolderLockerApp.java
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
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
public class FolderLockerApp
{
public static void main(String[] args)
{
String folderPath,password,answer;
Scanner scanner = new Scanner(System.in);
System.out.println("Folder Locker Application");
System.out.println("==================================================");
// Get folder path from the user
System.out.print("Enter the path of the folder: ");
folderPath = scanner.nextLine();
// Check if the folder existsFF
File folder = new File(folderPath);
if (!folder.exists() || !folder.isDirectory())
{
System.out.println("Invalid folder path or folder does not exist.");
return;
}
//Encryption Or Decryption
System.out.print("Do you want to lock or unlock the folder?:(L/U) ");
String fun=scanner.nextLine();
if (fun.equalsIgnoreCase("Lock") || fun.equalsIgnoreCase("L"))
{
// Get a password from the user to lock the folder
System.out.print("Enter a password to lock the folder:");
password = scanner.nextLine();
// Encrypt the folder with the given password
if (lockFolder(folder, password))
{
System.out.println("Folder locked successfully!");
}
else
{
System.out.println("Failed to lock the folder. Please try again.");
}
scanner.close();
}
else
{
System.out.print("Enter the password to unlock the folder:");
password = scanner.nextLine();
// Decrypt the folder with the given password
if (unlockFolder(folder, password))
{
System.out.println("Folder unlocked successfully!");
}
else
{
System.out.println("Failed to unlock the folder. Please try again.");
}
scanner.close();
}
}
private static boolean lockFolder(File folder, String password)
{
try
{
// Create a temporary folder to store the encrypted files
File tempFolder = new File(folder.getParent(), folder.getName() + "_temp");
if (!tempFolder.exists())
{
tempFolder.mkdir();
}
File[] files = folder.listFiles();
if (files != null)
{
for (File file : files)
{
if (file.isFile())
{
byte[] fileContent = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
byte[] encryptedContent = encrypt(fileContent, password);
Files.write(Paths.get(tempFolder.getAbsolutePath(), file.getName()), encryptedContent);
}
}
}
// Remove the original folder and rename the temporary folder
deleteFolder(folder);
tempFolder.renameTo(folder);
return true;
}
catch (Exception e)
{
System.out.println("caught: "+e);
return false;
}
}
private static boolean unlockFolder(File folder, String password)
{
try
{
// Create a temporary folder to store the encrypted files
File tempFolder = new File(folder.getParent(), folder.getName() + "_temp");
if (!tempFolder.exists())
{
tempFolder.mkdir();
}
File[] files = folder.listFiles();
if (files != null)
{
for (File file : files)
{
if (file.isFile())
{
byte[] fileContent = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
byte[] encryptedContent = decrypt(fileContent, password);
Files.write(Paths.get(tempFolder.getAbsolutePath(), file.getName()), encryptedContent);
}
}
}
System.out.print("Folder unlocked!");
// Remove the original folder and rename the temporary folder
deleteFolder(folder);
tempFolder.renameTo(folder);
return true;
}
catch (Exception e)
{
System.out.println("caught: "+e);
return false;
}
}
private static byte[] encrypt(byte[] data, String password)
{
byte[] encryptedData = new byte[data.length];
int passwordInt = Integer.parseInt(password);
for (int i = 0; i < data.length; i++)
{
//System.out.println(data[i]+" "+passwordInt);
encryptedData[i] = (byte) (data[i]+(int)passwordInt);
}
return encryptedData;
}
private static byte[] decrypt(byte[] data, String password)
{
byte[] encryptedData = new byte[data.length];
int passwordInt = Integer.parseInt(password);
for (int i = 0; i < data.length; i++)
{
//System.out.println(data[i]+" "+passwordInt);
encryptedData[i] = (byte) (data[i]-(int)passwordInt);
}
return encryptedData;
}
private static void deleteFolder(File folder)
{
File[] files = folder.listFiles();
if (files != null)
{
for (File file : files)
{
if (file.isDirectory())
{
deleteFolder(file);
}
else
{
file.delete();
}
}
}
folder.delete();
}
}