-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.java
152 lines (135 loc) · 3.33 KB
/
server.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
package DiffieHellman;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class server implements host
{
private static final int DEFAULT_PORT = 12323;
private ServerSocket serverSocket;
private Socket socket;
/**
*Open a new connection on the default port.
*/
public server() throws IOException
{
new server(DEFAULT_PORT);
}
/**
*Open a new connection on the specified port.
*/
public server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
}
/**
*Run the server.
*/
public void run() throws IOException
{
while (true)
{
acceptConnection();
int privateKey = getPrivateKey();
String cipher = readMessage();
String decryption = decryptMessage(cipher, privateKey);
printMessages(cipher, decryption);
socket.close();
}
}
/**
*Wait for a connection to the server socket from the client and accept it.
*/
private void acceptConnection() throws IOException
{
System.out.println(getInetAddress() + " Listening on port " +
serverSocket.getLocalPort() + "...");
this.socket = serverSocket.accept();
System.out.println("Accepted connection from " +
socket.getRemoteSocketAddress());
}
private int getPrivateKey() throws IOException
{
diffieHellmanKeyDelivery creator =
new diffieHellmanKeyDelivery(this, false);
return creator.calculateKey();
}
/**
*Get the local IPv4 address.
*/
private String getInetAddress()
{
try
{
return Inet4Address.getLocalHost().getHostAddress();
} catch (UnknownHostException e)
{
return "";
}
}
/**
*Read the input from the client and prints the data in UTF format.
*/
public String readMessage() throws IOException
{
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
return inputStream.readUTF();
}
/**
*Decrypts the cipher sent by the client.
*/
private String decryptMessage(String cipher, int privateKey)
{
encryption scheme = new encryption(privateKey);
return scheme.decrypt(cipher);
}
/**
*Print the encrypted and decrypted messages.
*/
private void printMessages(String cipher, String decryption)
{
System.out.println("\nReceived encrypted message:\n" + cipher + "\n");
System.out.println("Decrypted:\n" + decryption + "\n");
}
/**
*Sends a message to the client.
*/
public void sendMessage(String message) throws IOException
{
DataOutputStream outputStream =
new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(message);
}
/**
*Retrieve the port from the args array.
*/
private static int getPortFromArgs(String[] args)
{
if (args.length > 0)
{
try
{
return Integer.parseInt(args[0]);
} catch (NumberFormatException e)
{
return DEFAULT_PORT;
}
}
return DEFAULT_PORT;
}
public static void main(String[] args)
{
int port = getPortFromArgs(args);
try
{
server s = new server(port);
s.run();
} catch (IOException e)
{
e.printStackTrace();
}
}
}