-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
43 lines (34 loc) · 1.85 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
import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
public class Server {
public static void main(String[] args) throws Exception {
int port = 4000;
// Load the server keystore
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream("serverkey.jks"), "stpass123".toCharArray()); // Update with your keystore password
// Initialize KeyManagerFactory with the server's keystore
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "stpass123".toCharArray()); // Update with your key password
// Initialize SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
// Create SSL ServerSocket
SSLServerSocketFactory serverSocketFactory = sslContext.getServerSocketFactory();
SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(port);
System.out.println("Secure server started on port " + port);
while (true) {
SSLSocket clientSocket = (SSLSocket) serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress());
// Communication with the client
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
String message;
while ((message = input.readLine()) != null) {
System.out.println("Received from client: " + message);
output.println("Hello, client! Secure communication established.");
}
clientSocket.close();
}
}
}