0% found this document useful (0 votes)
9 views3 pages

NS Ex5

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

NS Ex5

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

MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.:311521104035

EX. NO : 05 CHECK MESSAGE INTEGRITY AND


CONFIDENTIALITY USING SSL

AIM:

To check message Integrity and Confidentiality using SSL with java code.

OBJECTIVE:

To learn and understand about checking message Integrity and Confidentiality using SSL with java code.

SOFTWARE REQUIRED:

JAVA

ALGORITHM:

Step 1: Create a class and Load the keystore.

Step 2: Initialize the SSL context.

Step 3: Create SSL server socket and Accept the client connection.

Step 4: Create I/O streams to receive and send messages → Close the connection.

Step 5: In the client side, load the truststore.

Step 6: Create and initialize SSL socket, SSL factory

Step 7: Create I/O streams to receive and send messages → Close the connection.

PROGRAM:

Server:

import javax.net.ssl.*;
import java.io.*; import java.security.*; public class SSLServer
{
public static void main(String[] args) throws Exception

Page No.:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.:311521104035

{
int port = 12345;
char[] passphrase = “password”.toCharArray(); KeyStore keyStore = KeyStore.getInstance(“JKS”);
keyStore.load(new FileInputStream(“server_keystore.jks”), passphrase); KeyManagerFactory
keyManagerFactory = KeyManagerFactory.getInstance(“SunX509”);
keyManagerFactory.init(keyStore,passphrase);
SSLContext context = SSLContext.getInstance(“TLS”);
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); context.init(keyManagers, null,
null);
SSLServerSocketFactory sslServerSocketFactory = context.getServerSocketFactory(); SSLServerSocket
serverSocket = (SSLServerSocket)
sslServerSocketFactory.createServerSocket(port);
System.out.println(“Server started. Waiting for clients…”); SSLSocket clientSocket = (SSLSocket)
serverSocket.accept(); System.out.println(“Client connected.”);
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true); String message;
while ((message = reader.readLine()) != null)
{
System.out.println(“Received: “ + message); writer.println(“Server acknowledges: “ + message);
}
reader.close(); writer.close(); clientSocket.close(); serverSocket.close();
}
}

Client:

import javax.net.ssl.*; import java.io.*; import java.security.*; public class SSLClient


{
public static void main(String[] args) throws Exception
{
String serverHost = “localhost”;
int serverPort = 12345;
char[] passphrase = “password”.toCharArray(); KeyStore trustStore = KeyStore.getInstance(“JKS”);
trustStore.load(new FileInputStream(“client_truststore.jks”), passphrase); TrustManagerFactory
trustManagerFactory =TrustManagerFactory.getInstance(“SunX509”);
trustManagerFactory.init(trustStore);
SSLContext context = SSLContext.getInstance(“TLS”);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

context.init(null, trustManagers, null);


SSLSocketFactory sslSocketFactory = context.getSocketFactory();
SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(serverHost, serverPort);

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Page No.:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.:311521104035

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);


writer.println(“Hello, server!”);
String response = reader.readLine(); System.out.println(“Server response: “ + response); reader.close();
writer.close(); socket.close();
}
}

OUTPUT:

RESULT:

Thus, the java program to check message Integrity and Confidentiality using SSL have been executed
successfully.

Page No.:

You might also like