Client-Server Application: Secure Communication System with Encryption

Verified

Added on  2022/08/11

|16
|1570
|21
Project
AI Summary
This assignment presents a client-server application implemented in Java, designed for secure communication. The program utilizes encryption and decryption techniques, specifically the AES SHA-256 algorithm, to ensure message confidentiality. The system comprises three classes: Client, Server, and Message, enabling interaction between the client and server through sockets. The client initiates communication by sending encrypted messages to the server, which then decrypts and displays both the original and encrypted messages. The application also includes timestamping to track message transmission times. The code includes detailed implementations of the Client and Server classes, showcasing the use of input/output streams, encryption methods, and socket connections. The program's execution is illustrated with screenshots, demonstrating the server's startup, client initialization with hostname, port, and user ID, message exchange, and the server's response. The assignment provides a practical example of secure network communication and cryptography implementation.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running Head: Client and Server Application
Client and Server Application
Name of the Student
Name of the University
Author Name
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
1Progaming Detalis
Overview of the program
The program is based on an interaction between client and server. At first, the server sends the
start with the port number and went for the client. After that, the client starts with local host, port no
and user id. Let both servers and clients are ready to interact with each other. Client sends a message at
server. Massage send though encryption mode and send to server. In server, message is decrypting and
show both encrypt and decrypt the message. Along with when the time of start client and server both
show the current time and date. Also in the time of sending message date and time also visible.
Description
The program consists of three class Client-Server and Massage. At first start server-class then
server show wait for a client. Then start the Client class. At the start of the client class, there are must
give a hostname, port no and user id. The server class also starts with the port number. Now the client
and server ready for communication. Then client sends a massage to sever through the web socket and
the server received it. Also, Server sends the reply message to client with the socket. In the program,
there is encrypt and decrypt method for security purposes. For the purpose of encryption and
decryption use the AES SHA-256 Algorithm process. This algorithm used a security key for
encrypting or decrypting process. When the client and server send the message, it encrypts and
decrypts with the key.
Represent of Programing code
Client class
package _package;
import java.net.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Document Page
2Progaming Detalis
import java.util.Arrays;
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private byte[] key;
private DataInputStream input = null;
private DataOutputStream out = null;
private BufferedReader br;
private static SecretKeySpec secretKey;
static String address;
static int port;
static String userid;
final String secretKey1 = "ssshhhhhhhhhhh!!!!";
long millis=System.currentTimeMillis();
java.util.Date date=new java.util.Date(millis);
// constructor to put ip address and port
public Client(String address, int port,String userid1) throws IOException
{
Document Page
3Progaming Detalis
this.address=address;
this.port=port;
this.userid=userid1;
MessageDigest sha = null;
// establish a connection
socket = new Socket(address, port);
System.out.println("Connected With Server");
System.out.println("Client"+" "+userid1);
System.out.println(date);
try {
key = userid1.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-256");
key = sha.digest(key);
key= Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
}
catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
// to send data to the server
DataOutputStream dos
= new DataOutputStream(
socket.getOutputStream());
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
4Progaming Detalis
// to read data coming from the server
BufferedReader br
= new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// to read data from the keyboard
BufferedReader kb
= new BufferedReader(
new InputStreamReader(System.in));
String str, str1;
// repeat as long as exit
// is not typed at client
while (!(str = kb.readLine()).equals("exit")) {
// send to the server
String encrypt = Client.encrypt(str, secretKey1);
dos.writeBytes("Massge from=>"+userid1+" "+"Encrypt Mssage==>"+encrypt + " "+"Decrypt
Massage==>"+" "+str+" "+date+"\n");
// receive from the server
str1 = br.readLine();
System.out.println(str1);
}
Document Page
5Progaming Detalis
// close connection.
dos.close();
br.close();
kb.close();
socket.close();
}
public static String encrypt(String strToEncrypt, String secret)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static String decrypt(String strToDecrypt, String secret)
{
Document Page
6Progaming Detalis
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
public static void main(String args[]) throws IOException
{
String address1;
int port1;
String userid1;
Scanner sc=new Scanner(System.in);
System.out.println("Give Host Name");
address1=sc.nextLine();
System.out.println("Give User Name");
userid1=sc.nextLine();
System.out.println("Give Port NO");
port1=sc.nextInt();
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
7Progaming Detalis
Client client = new Client(address1,port1,userid1);
}
}
Server Class
package _package;
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
private DataOutputStream out = null;
long millis=System.currentTimeMillis();
java.util.Date date=new java.util.Date(millis);
// constructor with port
public Server(int port) throws IOException
{
// starts the server and waits for a connection
Document Page
8Progaming Detalis
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client Login...");
socket = server.accept();
System.out.println("Client Requst accepted");
// to send data to the client
PrintStream ps
= new PrintStream(socket.getOutputStream());
// to read data coming from the client
BufferedReader br
= new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// to read data from the keyboard
BufferedReader kb
= new BufferedReader(
new InputStreamReader(System.in));
// server executes continuously
while (true) {
Document Page
9Progaming Detalis
String str, str1;
// repeat as long as the client
// does not send a null string
// read from client
while ((str = br.readLine()) != null) {
System.out.println(str);
str1 = kb.readLine();
// send to client
ps.println("Massge from=>Server"+str1+" "+date+"\n");
}
// close connection
ps.close();
br.close();
kb.close();
server.close();
socket.close();
// terminate application
System.exit(0);
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
10Progaming Detalis
} // end of while
}
public static void main(String args[]) throws IOException
{ int port;
Scanner sc=new Scanner(System.in);
System.out.println("Give Port Name");
port=sc.nextInt();
Server server = new Server(port);
}
}
package _package;
import java.util.Date;
Massage Class
public class Massage {
public String recipientHash;
public Date timestamp;
public byte[] key;
public byte[] iv;
public byte[] encryptedMsg;
public byte[] signature; //
public Massage(String recipientHash, Date timestamp, byte[] key, byte[] iv, byte[] encryptedMsg) {
this.recipientHash = recipientHash;
Document Page
11Progaming Detalis
this.timestamp = timestamp;
this.key = key;
this.iv = iv;
this.encryptedMsg = encryptedMsg;
}
Execution of Output
Server Class
Fig-1
The server class stated and now give the port no for start the server
Document Page
12Progaming Detalis
Fig-2
After giving the port no server wait for the client
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
13Progaming Detalis
Client Class
Fig-3
Now run the client class. After running the class it wants hostname, Port no and user id. After giving the
information the client has been starting and the Client name and current date and time. Now Client send to a
message in server “Hello I am Alice”.
Document Page
14Progaming Detalis
Fig-4
The server received the message through the encryption. The encrypt message show. Massage decrypt
with the algorithm and show it in the server with the current date and time.
Fig-5
Now server replies the massage to the server
Document Page
15Progaming Detalis
Fig-6
The massage which receives from the server into the client and shows the massage with current date
and time
chevron_up_icon
1 out of 16
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]