-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseServer.java
98 lines (85 loc) · 4.45 KB
/
DatabaseServer.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
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.net.*;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DatabaseServer
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
private DataOutputStream out = null;
PrintWriter writer = new PrintWriter(new File("Database_Log.txt"));
// constructor with port
public DatabaseServer(int port) throws FileNotFoundException {
// starts server and waits for a connection
try
{
Commands commands = new Commands();
server = new ServerSocket(port);
socket = server.accept();
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
String nonceO = String.valueOf(commands.generateNonce());
String step3 = in.readUTF();
String [] parts = step3.split(",");
String t = commands.util();
t = commands.DecryptRSA(t,System.getProperty("user.dir") + "/Database.jks");
String [] parts2 = t.split(",");
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Alice->Database : "+"Alice , "+commands.util()+" , "+parts2[3]);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Ticket Decrpyted : "+t);
writer.flush();
String nonce = commands.decrypt(parts[2],commands.getSessionKey(parts2[3]));
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Message Decrpyted : N1="+nonce);
writer.flush();
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Database->Alice : "+String.valueOf(Integer.parseInt(nonce)+1)+" , "+nonceO);
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Database->Alice : "+commands.encrypt(String.valueOf(Integer.parseInt(nonce)+1)+","+nonceO,commands.getSessionKey(parts2[3])));
writer.flush();
out.writeUTF(commands.encrypt(String.valueOf(Integer.parseInt(nonce)+1)+","+nonceO,commands.getSessionKey(parts2[3])));
String step5 = in.readUTF();
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Alice->Database : "+step5);
step5 = commands.decrypt(step5,commands.getSessionKey(parts2[3]));
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Message Decrpyted : "+step5);
writer.flush();
if(step5.equals(String.valueOf(Integer.parseInt(nonceO)+1))){
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Database->Alice : Authentication is completed!");
writer.flush();
out.writeUTF("1");
}
else{
writer.println(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now())+" Database->Alice : Authetication is failed");
writer.flush();
out.writeUTF("0");
}
// reads message from client until "Over" is sent
}
catch(IOException i)
{
System.out.println(i);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws FileNotFoundException {
DatabaseServer ms = new DatabaseServer(3003);
}
}