-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchForFile.java
More file actions
404 lines (325 loc) · 11.1 KB
/
SearchForFile.java
File metadata and controls
404 lines (325 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import java.util.Scanner;
import java.util.ArrayList;
import java.nio.file.Paths;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
import java.net.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
import java.util.Scanner;
import java.nio.file.Paths;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import org.apache.commons.codec.binary.Base64;
import java.nio.file.*;
class SearchForFile
{
private String fileNameToSearch;
private List<String> result = new ArrayList<String>();
private String kpath="";
private String kname="";
private String fname;
private String fpath;
private String keystr;
private long startTime;
private long stopTime;
public final static int DEF_PORT=9;
public final static int MAX_SIZE=65507;
private String algo;
private String path;
private String rpath;
private String rname;
public void decrypt() throws Exception
{
//generating same key
byte k[] = "HignDlPs".getBytes();
SecretKeySpec key = new SecretKeySpec(k,algo.split("/")[0]);
//creating and initialising cipher and cipher streams
Cipher decrypt = Cipher.getInstance(algo);
decrypt.init(Cipher.DECRYPT_MODE, key);
//opening streams
FileInputStream fis = new FileInputStream(path);
try(CipherInputStream cin=new CipherInputStream(fis, decrypt))
{
try(FileOutputStream fos =new FileOutputStream(path.substring(0,path.lastIndexOf("."))))
{
copy(cin,fos);
}
}
}
private void copy(InputStream is,OutputStream os) throws Exception
{
byte buf[] = new byte[4096]; //4K buffer set
int read = 0;
while((read = is.read(buf)) != -1) //reading
os.write(buf,0,read); //writing
}
public static void decode(String sourceFile, String targetFile) throws Exception
{
byte[] decodedBytes = Base64.decodeBase64(loadFileAsBytesArray(sourceFile));
writeByteArraysToFile(targetFile, decodedBytes);
}
public static byte[] loadFileAsBytesArray(String fileName) throws Exception
{
File file = new File(fileName);
int length = (int) file.length();
BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[length];
reader.read(bytes, 0, length);
reader.close();
return bytes;
}
public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException
{
File file = new File(fileName);
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
writer.write(content);
writer.flush();
writer.close();
}
public String getFileNameToSearch()
{
return fileNameToSearch; //return the file name to search
}
public void setFileNameToSearch(String fileNameToSearch)
{ //setting the file name to search
this.fileNameToSearch = fileNameToSearch;
}
public List<String> getResult()
{
return result; //for file search
}
public String searchDirectory(File directory, String fileNameToSearch) throws Exception
{
setFileNameToSearch(fileNameToSearch); //setting the file name to search
if (directory.isDirectory())
{
search(directory); //if path is directory then search file in directory
}
else
{
System.out.println(directory.getAbsoluteFile() + " is not a directory!");
}
return fpath;
}
private void search(File file) throws Exception
{
if (file.isDirectory()) //if directory
{
System.out.println("\n\nSearching Main Database" +file);
if (file.canRead()) //when file has permission to read
{
for (File temp : file.listFiles())
{
if (temp.isDirectory())
{
search(temp); //again if subdirectory then search file in sub directory
}
else
{ //if file
String tempp = temp.getName();
//System.out.println("\n"+temp);
//System.out.println("\n"+tempp);
String tempaddr=temp.getAbsolutePath();
//System.out.println("\n"+temp);
//System.out.println("\n"+tempp);
Boolean found;
found=matchcontent(tempaddr,keystr);
if(found)
{
fname=tempp;
result.add(temp.getAbsoluteFile().toString()); //requested file found in sender DB. add it to "result" list
//fpath=temp.getAbsoluteFile().toString(); //setting path where file found
//System.exit(0);
}
//System.out.println(found);
}
}
}
else
{
System.out.println(file.getAbsoluteFile() + "Permission Denied"); //when file dont hav permission to access
}
}
}
public Boolean matchcontent(String faddr,String keytosearch)throws Exception
{
//System.out.println("\n"+faddr);
//System.out.println("\n"+keytosearch);
InputStream inn = new FileInputStream(new File(faddr));
BufferedReader reader = new BufferedReader(new InputStreamReader(inn));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
out.append(line);
}
//System.out.println(out.toString()); //Prints the string content read from input stream
reader.close();
String fcontent=out.toString(); //reading the content of key and storing it in string
//System.out.println(fcontent);
Boolean a=fcontent.contains(keytosearch);
if(a)
{
fpath=faddr;
return true;
}
else
{
//System.out.println("not found");
return false;
}
}
public void getkey(String addr)
{ //search for key received
File directory = new File(addr);
File[] fList = directory.listFiles();
for (File file : fList)
{
kname=file.getName();
kpath=file.toString();
// Path path = Paths.get(kpath);
}
}
/* public static void sendToDir(String desloc, byte[] content) throws IOException
{
File fileee = new File(desloc);
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(fileee)); //sending key to main DB method
writer.write(content);
writer.flush();
writer.close();
}
*/
static void copy(String src) throws IOException
{
try{
InetAddress server=InetAddress.getByName("127.0.0.1");
Socket soc = new Socket(server, 8023);
FileInputStream fis = new FileInputStream(src);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
ObjectOutputStream oos = new ObjectOutputStream(soc.getOutputStream()) ;
oos.writeObject(buffer);
oos.close();
soc.close();
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
public static void main(String[] args) throws Exception
{
SearchForFile ob=new SearchForFile();
ob.getkey("F:\\Received Key"); //find d key received and store in string
//System.out.println(ob.kpath);
System.out.println("\n\n\n\nKey Found :"+ob.kname);
InputStream inn = new FileInputStream(new File(ob.kpath));
BufferedReader reader = new BufferedReader(new InputStreamReader(inn));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
out.append(line);
}
//System.out.println(out.toString()); //Prints the string content read from input stream
reader.close();
ob.keystr=out.toString(); //reading the content of key and storing it in string
//System.out.println(ob.keystr);
//System.out.println(ob.keystr.substring(0,14));
//System.out.println(ob.keystr.substring(14,38));
String pathstr=ob.keystr.substring(14,38);
ob.keystr=ob.keystr.substring(0,14);
//System.out.println(pathstr);
//System.out.println(ob.keystr);
String pathaddr="F:\\Received Key\\Newkey.txt.enc";
writeByteArraysToFile( pathaddr, pathstr.getBytes());
decode( pathaddr, pathaddr);
ob.algo="DES/ECB/PKCS5Padding";
ob.path=pathaddr;
ob.decrypt();
String content = new String(Files.readAllBytes(Paths.get("F:\\Received Key\\Newkey.txt")));
/* String contents = new String(Files.readAllBytes(Paths.get("F:\\Received Key\\Newkey.txt")));
*/ System.out.println("Contents (Java 7) : " + content);
String ppp="";
if(content.equals("apple.com"))
{
ppp="F:\\Database\\apple";
}
else if(content.equals("samsung.com"))
{
ppp="F:\\Database\\samsung";
}
else if(content.equals("nokia.com"))
{
ppp="F:\\Database\\nokia";
}
String addr="F:\\Received Key\\key.txt.enc";
writeByteArraysToFile( addr, ob.keystr.getBytes()); //re writing "key" wch contains key to search in file in main DB
System.out.println("\nEnter 1 to search for file :");
Scanner in = new Scanner(System.in);
int choice=in.nextInt(); //sending key to main DB
if(choice==1)
{ //F:\\Database
try
{
ob.startTime = System.nanoTime();
String path = ob.searchDirectory(new File(ppp),""); //search the file in DB
ob.stopTime = System.nanoTime();
System.out.println("\n\n\nTime taken to search " +(double)(ob.stopTime - ob.startTime)/1000000000+" seconds");
}
catch(Exception e)
{
System.out.println(e);
}
int count = ob.getResult().size();
if(count == 0)
{
System.out.println("\nNo file found!\nKey not matching!"); //when file not in sender DB
System.exit(0);
}
else
{
//System.out.println("\n\nFile : " +fsend +" Found at: " + path); //print found
System.out.println("\nFound " + count + " File!\nMatching Key :-)");
for (String matched : ob.getResult())
{
System.out.println("\n\nLocation : " + matched);
}
//System.out.println(ob.fpath);
}
System.out.println("\nEnter 1 to send the file to receiver :");
int choice1=in.nextInt();
in.close();
if(choice1==1)
{
//String recloc="D:\\receiver\\Received File\\"; //set the path where the key has to be sent in main DB
//String fdesloc=recloc+ob.fname;
copy(ob.fpath);
System.out.println("\n\nFile sent Successfully!");
System.out.println("\n\nThank You!");
}
else
{
System.out.println("Wrong input!");
System.exit(0);
}
}
else
{
System.out.println("Wrong input!");
System.exit(0);
}
}
}