Client-Server Programming: Java Socket, RSA Encryption, and Decryption

Verified

Added on  2022/08/10

|9
|1289
|365
Project
AI Summary
This project details the implementation of client-server programming using the Java programming language, focusing on secure communication through socket programming and the RSA algorithm. The project involves the creation of four Java classes: Server.java, Client.java, RSA.java, and Message.java. Server.java establishes the server using ServerSocket, accepting client connections. Client.java creates the client application, connecting to the server with a specified hostname, port number, and user ID. RSA.java handles encryption, decryption, key generation, and SHA-256 hash generation for user identification, ensuring secure message transmission. Message.java stores message details, including encrypted messages, keys, and SHA-256 hashes. The program encrypts messages using the private key and decrypts them using the public key, ensuring that the messages are secure. The project includes output examples demonstrating server startup, client connection, message sending, and decryption, showcasing the functionality and security of the client-server interaction.
Document Page
Running head: CLIENT SERVER PROGRAMMING
CLIENT SERVER PROGRAMMING
Name of the Student
Name of the University
Author Note
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1CLIENT SERVER PROGRAMMING
Client-Server Programming
Client-Server programming is used to established connection between clients and
communicate with each other. In this, the Java programming language is used to implement client-
server programming using Socket programming. Socket programming provided the ability to create a
server and established connecting between server and client to interact with each other. Basically,
socket programming offers a way to connect nodes on a network to communicate with each other.
In this program, java socket programming is used for establishing a connection between
client and server and RSA algorithm is used for encryption and decryption of the message sends by
the client for transmitting message securely. Java socket programming helps to connect with the
server if both server and client runs on the same machine or in the different machine it used for
connecting server and client.
In this java program, different java class is used to perform client-server communication. In
this program, four different class is used, and every class performs a different operation. The first
class of this program is Server.java which is used to create a server using the Server socket class with
the appropriate port number. Server.java class plays a very important role in client-server
communication without this class; it is not possible to perform client-server communication.
This java program is used to create client-server programming where client and server both
interact with each other privately because the message sends and received by the client and server is
encrypted using RSA algorithm so no one can read the message without key. In the RSA algorithm, a
public key is used for decryption, and the private key is used for encryption. This encryption and
decryption method helps to sends and receive messages securely.
The second class called Client.java is used for creating the client, and every client has to
connect with the server by providing appropriate hostname and port number for communicating with
Document Page
2CLIENT SERVER PROGRAMMING
the server. Another class which is used in this program is RSA.java which is used to performs
operations like encryption, decryption, key generation and SHA-256 hash generation for user id.
The fourth java class called message.java is used to store all the necessary information of the
message. In this class, all the encrypted message, keys along with SHA256 hash is stored. All the
classes are described below with some codes.
Server.java
Server.java class is a very important class of this client-server program because in this class a
server is created. For creating server Socket API’s socket class is used. For creating server
application first create an instance of the ServerSocket class.
ServerSocket ss = new ServerSocket(4567);
The above line of code is used to create an instance of ServerSocket with the appropriate port
number. After the above operation, Server.java class is ready to accept client request to connect with
the server for communicating with each other.
After the instance of server-socket is created successfully, then accept method of the socket is
waits for the client's request for connecting to communicate with the server.
Socket s = ss.accept();
The above line of the Server class is used to accept client request to connect with the server at
this point of time server connection is established, and the server waits for the client.
Client.java
Client.java class is used to create client application, and for that, it is necessary to create an
instance of Socket class and pass the hostname of the server along with a port number. In this
program, localhost is used for hostname because both server and client is going to run on the same
Document Page
3CLIENT SERVER PROGRAMMING
machine. In this program hostname, port number and the user id of the client is passed through the
command line.
Socket s = new Socket(args[0], Integer.parseInt(args[1])); // Creating Instance of Socket
String userID = args[2]; // Reading user id from Command line as argument
The above lines of codes create an instance of the socket with hostname and port number of
the server. If the hostname or port number which is provided at the times execution is incorrect, then
the program throws an error and the program terminates.
m.setEncryptedMsg(rsa.encrypt(str.getBytes()));
The above lines of code are used to store the encrypted message by using the setter method,
which is declared in Message.java.
RSA.java
RSA.java class is used to perform all the encryption and decryption process along with SHA
256 and key generation. In this class, many different methods are created, and every method
performs a specific operation.
public byte[] encrypt(byte[] encryptedMsg)
The above method is used for message encryption and returns an encrypted message.
public byte[] decrypt(byte[] message) this method is used for message decryption, and this method
returns a decrypted message.
public String SHA256() method is used for generating an SHA-256 hash of the user id. In
this method, the Message Digest class is used for generating a hash of user id. Message Digest is a
predefined class in java which is available in java. security package. The RSA.java class is
responsible for converting plain text to cypher text which helps to protect confidential messages. The
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4CLIENT SERVER PROGRAMMING
message which is sent by the client is encrypted using private key then the encrypted message is sent
by the socket to the server, and if the server wants to read the message, then the server has to decrypt
the message to see the contents.
Message.java
Message.java class is used to store all the necessary information of the message and keys
along with the SHA-256 hash of the user id of the client. For storing the message, getter and setter
methods are used to encapsulate the information mentioned above. Some of the getter and setter
methods are discussed below:
public static String get recipientHash() {
return recipientHash;
}
public static void setRecipientHash(String recipientHash) {
Message.recipientHash = recipientHash;
}
The above getter and setter method is for RecipientHash which is generated using SHA-256
hash.
public static Date getTimestamp() {
return timestamp;
}
public static void setTimestamp(Date timestamp) {
Message.timestamp = timestamp;
Document Page
5CLIENT SERVER PROGRAMMING
}
The above getter and setter method is for getting message timestamp from the client and
encapsulating this to the Timestamp array.
Output
Output-1: Server Started
Output-2: Providing Program Arguments
Document Page
6CLIENT SERVER PROGRAMMING
Output-3: Client Connected to Server and ready to send message
Output-4: Message Send by Bob Client
Output-5: Server receives the message and decrypts the message
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7CLIENT SERVER PROGRAMMING
Output-6: Another message from Bob
Output-7: If client write stop then the connection of the client is closed
Document Page
8CLIENT SERVER PROGRAMMING
Bibliography
Bloch, J., 2016. Effective java. Pearson Education India.
Gangadharan, B.P., Ji, W., Selitser, B., Zhu, H.F. and Loganathan, K., Oracle International
Corp, 2016. Java API for programming web real-time communication applications. U.S.
Patent 9,509,745.
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]