0% found this document useful (0 votes)
11 views6 pages

Wireless Assignment 01

The document outlines an assignment for implementing a client-server communication system focused on wireless and mobile security. It details the objectives, architecture, and components involved, including Java code for both server and client sides that handle data generation, checksum calculation, encryption using the RC4 algorithm, and data integrity verification. The code includes methods for generating random strings, encrypting checksums, and processing received data to ensure integrity during communication.

Uploaded by

baneennaqvi14125
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)
11 views6 pages

Wireless Assignment 01

The document outlines an assignment for implementing a client-server communication system focused on wireless and mobile security. It details the objectives, architecture, and components involved, including Java code for both server and client sides that handle data generation, checksum calculation, encryption using the RC4 algorithm, and data integrity verification. The code includes methods for generating random strings, encrypting checksums, and processing received data to ensure integrity during communication.

Uploaded by

baneennaqvi14125
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/ 6

COMSATS UNIVERSITY ISLAMABAD

WIRELESS AND MOBILE SECURITY


ASSIGNMENT 01
NAME: SYEDA BANEEN BATOOL
REGISTRATION NO: SP22-BCT-045
CLASS: BCT-6A
SUBMITTED TO: SIR ZULFIQAR ALI
WEP IMPLEMENTATION

 WHAT WE HAVE TO IMPLEMENT:


OBJECTIVES:
 Develop a client-server communication system.
 Generate 32-bit random strings.
 Calculate checksums for the generated strings.
 Encrypt the checksum using the RC4 algorithm.
 Transmit data between clients and validate data integrity.

ARCHITECTURE:

The system consists of a server and two clients:

 Server: Listens for client connections and facilitates communication between them.

 Client 1: Generates random strings, calculates checksum, encrypts checksum, and


sends data to the server.

 Client 2: Receives data from the server, decrypts checksum, recalculates checksum,
and verifies data integrity.

COMPONENTS:

 Server: Java code to handle client connections and data forwarding.


 Client: Java code for generating data, encrypting/decrypting checksums, and
communication with the server.

 CODE:
SERVER SIDE:

import java.io.*;
import java.net.*;

public class Server {

// Handles communication between two clients


public static void handleClient(Socket clientSocket, Socket
otherSocket) {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(otherSocket.getOutputStream(), true);

String message;
while ((message = in.readLine()) != null) {
System.out.println("Received from client: " + message);
out.println(message); // Send the message to the other
client
}
} catch (IOException e) {
System.out.println("Connection closed");
}
}

public static void main(String[] args) {


try (ServerSocket serverSocket = new ServerSocket(9999)) {
System.out.println("Server is waiting for clients...");

// Accept two clients


Socket client1 = serverSocket.accept();
System.out.println("Client 1 connected!");

Socket client2 = serverSocket.accept();


System.out.println("Client 2 connected!");

// Create two threads to handle the clients


new Thread(() -> handleClient(client1, client2)).start();
new Thread(() -> handleClient(client2, client1)).start();

} catch (IOException e) {
e.printStackTrace();
}
}
}

CLIENT SIDE:
import java.io.*;
import java.net.*;
import java.util.Random;

public class Client {

// Function to generate a 32-bit random string


public static String generate32BitString() {
Random random = new Random();
int randomInt = random.nextInt();
return String.format("%32s",
Integer.toBinaryString(randomInt)).replace(' ', '0');
}

// Function to calculate checksum


public static String calculateChecksum(String string1, String string2)
{
int int1 = (int) Long.parseLong(string1, 2);
int int2 = (int) Long.parseLong(string2, 2);
int result = int1 + int2;
if (result >= (1 << 32)) {
result = (result & 0xFFFFFFFF) + 1; // Simulate carry bit
behavior
}
return String.format("%32s",
Integer.toBinaryString(result)).replace(' ', '0');
}

// RC4 encryption and decryption function


public static byte[] rc4(byte[] key, byte[] text) {
byte[] S = new byte[256];
byte[] keystream = new byte[text.length];
int j = 0;

// Key-scheduling algorithm (KSA)


for (int i = 0; i < 256; i++) {
S[i] = (byte) i;
}

for (int i = 0; i < 256; i++) {


j = (j + S[i] + key[i % key.length]) & 0xFF;
byte temp = S[i];
S[i] = S[j];
S[j] = temp;
}

// Pseudo-random generation algorithm (PRGA)


int i = 0;
j = 0;
for (int c = 0; c < text.length; c++) {
i = (i + 1) & 0xFF;
j = (j + S[i]) & 0xFF;
byte temp = S[i];
S[i] = S[j];
S[j] = temp;
keystream[c] = (byte) (text[c] ^ S[(S[i] + S[j]) & 0xFF]);
}

return keystream;
}

// Function to convert byte array to hex string


public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
}

// Function to convert hex string to byte array


public static byte[] hexToBytes(String hex) {
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i+1), 16));
}
return data;
}

// Client to communicate with the server


public static void client() {
final String serverAddress = "localhost";
final int portNumber = 9999;

try (Socket clientSocket = new Socket(serverAddress, portNumber)) {


System.out.println("Connected to server");

BufferedReader serverInput = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter output = new
PrintWriter(clientSocket.getOutputStream(), true);

// Generate two 32-bit strings


String string1 = generate32BitString();
String string2 = generate32BitString();
System.out.println("Generated strings: " + string1 + ", " +
string2);

// Calculate checksum
String checksum = calculateChecksum(string1, string2);
System.out.println("Calculated checksum: " + checksum);

// Encrypt checksum using RC4


byte[] key = "secretkey".getBytes();
byte[] encryptedChecksum = rc4(key, checksum.getBytes("UTF-
8"));
System.out.println("Encrypted checksum: " +
bytesToHex(encryptedChecksum));

// Send strings and encrypted checksum to server


output.println(string1 + "," + string2 + "," +
bytesToHex(encryptedChecksum));

// Read response from server


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

// Process the received data if the response is not null


if (response != null) {
processReceivedData(response);
}

} catch (IOException e) {
e.printStackTrace();
}
}

// Function to process the received data


public static void processReceivedData(String receivedData) {
String[] parts = receivedData.split(",");
if (parts.length == 3) {
String receivedString1 = parts[0];
String receivedString2 = parts[1];
String encryptedReceivedChecksumHex = parts[2];

// Decrypt the received checksum


byte[] encryptedReceivedChecksum =
hexToBytes(encryptedReceivedChecksumHex);
byte[] decryptedReceivedChecksum = rc4("secretkey".getBytes(),
encryptedReceivedChecksum);

// Convert decrypted checksum to string with exception handling


String decryptedChecksum;
try {
decryptedChecksum = new String(decryptedReceivedChecksum,
"UTF-8");
} catch (UnsupportedEncodingException e) {
decryptedChecksum = "";
System.out.println("Error decoding checksum: " +
e.getMessage());
}

// Recalculate checksum
String calculatedChecksum = calculateChecksum(receivedString1,
receivedString2);

// Verify checksum
if (calculatedChecksum.equals(decryptedChecksum)) {
System.out.println("Checksum verified: Integrity
maintained.");
} else {
System.out.println("Checksum verification failed: Integrity
not maintained.");
}
} else {
System.out.println("Received data format is incorrect.");
}
}

public static void main(String[] args) {


client();
}
}

OUTPUTS:

You might also like