CO3099 Programming Assignment: Java Client-Server Application

Verified

Added on  2022/08/11

|15
|1603
|24
Homework Assignment
AI Summary
This document presents a Java-based client-server program designed for secure communication, addressing a programming assignment that simulates a secure system for a rebellion. The solution includes a client and a server component, demonstrating the use of sockets, input/output streams, and data encryption to protect transmitted messages. The client establishes a connection with the server, encrypts messages using AES encryption with a user-specific key, and sends the encrypted data. The server receives and displays the messages. The code incorporates necessary Java packages for networking, input/output, and security, including classes for message digests, encryption, and handling exceptions. The program structure includes classes for both client and server, with methods for establishing connections, sending and receiving data, and encrypting/decrypting messages. The assignment brief outlines the requirements for the system, emphasizing secure communication. The solution provides a practical implementation of these requirements, making it a valuable resource for students studying networking and cybersecurity. The server waits for client connections, receives data, and displays them. The program also includes user authentication using a user ID to create a unique key for each user. The program demonstrates the complete cycle of establishing a connection, sending data, encrypting messages, and receiving messages.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Running Head: Program Description
Program Description
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
Client Class
Creating a package named soketprogaming1. In java, the package is a special type of mechanism that
helps to encapsulate a group of classes. The package will help us to prevent naming conflicts and help us
to search for a specific class among other classes. After creating a package, importing several classes by
using the import statement. These classes or packages are essentials to run the program. Importing
java.net.* to using networking application on the java code. java.io.* is basically used for input and
output. Importing MessageDigest of the security class provided every function the message-digest
algorithm. No Such Algorithm Exception is handle by the exception class. This exception can be thrown
when cryptographic algorithm expected but that program is not present in the program. These are the
most important packages that are used in the client class. In the client class, initializing socket and input-
output streams to execute the program. Creating a constructor to put the IP address and port number.
Then providing codes for establishing a connection for connecting a client to the server. Then creating
codes to send data to the server. By using buffer reader class reading data is coming from the server. To
read data from the keyboard by using Buffered Reader kb then closing the connection by using the close
connection for class. The main method is implemented in this class.
package soketprogaming1;
import java.net.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
Document Page
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 userid1;
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
{
this.address=address;
this.port=port;
this.userid1=userid1;
MessageDigest sha = null;
// establish a connection
Document Page
socket = new Socket(address, port);
System.out.println("Connected");
System.out.println(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());
// to read data coming from the server
BufferedReader br
= new BufferedReader(
new InputStreamReader(
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
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 = soketprogaming1.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);
}
// close connection.
dos.close();
br.close();
kb.close();
socket.close();
Document Page
}
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)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
Document Page
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 userid2=null;
Scanner sc=new Scanner(System.in);
System.out.println("Give User Id");
userid2=sc.nextLine();
Client client = new Client("localhost",8080,userid2);
}
Creating a class named massage. Creating arrays name key, iv, encryptedMsg, signature. Creating
construction to get the information
package soketprogaming1;
import java.util.Date;
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
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;
this.timestamp = timestamp;
this.key = key;
this.iv = iv;
this.encryptedMsg = encryptedMsg;
}
}
Server Class
Creating a class named server. After importing necessary classes and packages by using importing
statements, initializing the socket and input stream. Creating a constructor for this class with the port.
Then start server and waiting for client connection. Send data to client creating an object of PrintStream
class. To read data that is coming from the client creating an object of buffered reader class. To read
every data form the keyboard creating a kb object of buffer reader class. After getting data from the
keyboard to start the server and waits for the connection and creating an instance of socket class. To
send the data to the client creating the ps object of the print-stream class. To read the every data from
client using readline method.
read from client
Document Page
package soketprogaming1;
import java.net.*;
import java.io.*;
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
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
Document Page
// 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) {
String str, str1;
// repeat as long as the client
// does not send a null string
// read from client
while ((str = br.readLine()) != null) {
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
System.out.println(str);
str1 = kb.readLine();
// send to client
ps.println("Massge from=>Bob"+str1+" "+date+"\n");
}
// close connection
ps.close();
br.close();
kb.close();
server.close();
socket.close();
// terminate application
System.exit(0);
} // end of while
}
public static void main(String args[]) throws IOException
{
Server server = new Server(8080);
Document Page
}
}
The above picture shows the server status and the server is waiting for a client.
Document Page
The above picture is showing the program is asking for user id. After getting the user id, the
program will find user id is correct or not. If the user id is correct, the server will be connected and
display connected status with day and date.
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
In the above picture user sending a message. In this scenario user sending his/her name.
Document Page
The above picture shows the user's message after encrypting and displays decrypting the message with
date and time.
Again user sending a message to the server.
chevron_up_icon
1 out of 15
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]