0% found this document useful (0 votes)
46 views5 pages

Ex 5

This document describes how to use SSL to provide message integrity and confidentiality between a Java server and client. It includes code to generate a keystore for the server certificate, setup the SSL context on both the server and client sides, and send/receive an encrypted message between them. The server accepts connections from the client and prints the received message, while the client connects to the server, sends a message, and prints the response.

Uploaded by

4074- Rashmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views5 pages

Ex 5

This document describes how to use SSL to provide message integrity and confidentiality between a Java server and client. It includes code to generate a keystore for the server certificate, setup the SSL context on both the server and client sides, and send/receive an encrypted message between them. The server accepts connections from the client and prints the received message, while the client connects to the server, sends a message, and prints the response.

Uploaded by

4074- Rashmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex.

No: 5 Check Message IntegrityAnd Confidentiality Using SSL

Program:

KeyStore

Command to create KeyStore file:

keytool -genkeypair -keyalg RSA -keysize 2048 -validity 365 -alias myserverkey -keystore
samlKeystore.jks -storepass password -keypass password -dname
"CN=localhost,OU=Unknown,O=Unknown,L=Unknown,ST=Unknown,C=Unknown"

Server.java

import javax.net.ssl.*;
import java.io.*;
import java.security.*;

public class Server {


public static void main(String[] args) {
try {
// Load the keystore
char[] keystorePassword = "password".toCharArray();
char[] keyPassword = "password".toCharArray();
KeyStore keyStore = KeyStore.getInstance("JKS");
try (FileInputStream fis = new FileInputStream("samlKeystore.jks")) {
keyStore.load(fis, keystorePassword);
}

// Set up the key manager factory


KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, keyPassword);

// Set up the SSL context


SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), null, null);

// Create the server socket factory


SSLServerSocketFactory ssf = sslContext.getServerSocketFactory();
SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket(9999);

System.out.println("Server started. Waiting for client connection...");

// Accept client connections


SSLSocket socket = (SSLSocket) serverSocket.accept();

// Set up input and output streams


BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Read message from client


String message = in.readLine();
System.out.println("Received message from client: " + message);

// Send response back to client


out.println("Message received by server");

// Close streams and socket


out.close();
in.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Client.java

import javax.net.ssl.*;
import java.io.*;
import java.security.*;

public class Client {


public static void main(String[] args) throws Exception {
// Load the truststore
char[] truststorePassword = "password".toCharArray();
KeyStore trustStore = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("samlKeystore.jks");
trustStore.load(fis, truststorePassword);

// Set up the trust manager factory


TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(trustStore);

// Set up the SSL context


SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

// Create the socket factory


SSLSocketFactory sf = sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) sf.createSocket("localhost", 9999);

// Set up input and output streams


PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

// Send message to server


out.println("Hello from client");

// Read response from server


String response = in.readLine();
System.out.println("Response from server: " + response);

// Close streams and socket


out.close();
in.close();
socket.close();
}
}
Output:

You might also like