0% found this document useful (0 votes)
29 views11 pages

CCS354 Network Security Lab Manual (1-4)

The document outlines the implementation of symmetric and asymmetric key algorithms, including AES and RSA, for encryption, decryption, and digital signature schemes. It provides detailed algorithms and Java code examples for each method, along with steps for installing and using Wireshark and tcpdump to analyze network traffic. The results indicate successful execution of the encryption methods and network traffic analysis.

Uploaded by

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

CCS354 Network Security Lab Manual (1-4)

The document outlines the implementation of symmetric and asymmetric key algorithms, including AES and RSA, for encryption, decryption, and digital signature schemes. It provides detailed algorithms and Java code examples for each method, along with steps for installing and using Wireshark and tcpdump to analyze network traffic. The results indicate successful execution of the encryption methods and network traffic analysis.

Uploaded by

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

Ex.No.

1 : Implement symmetric key algorithms

Aim:

To implement a Symmetric Key Encryption and Decryption algorithm using AES


(Advanced Encryption Standard) in Java.

Algorithm:

1.​ Input the plaintext from the user.


2.​ Generate a secret key using the AES algorithm.
3.​ Initialize the cipher for encryption mode.
4.​ Encrypt the plaintext using the secret key.
5.​ Print the encrypted text in Base64 format for readability.
6.​ Initialize the cipher for decryption mode.
7.​ Decrypt the encrypted text back to the original plaintext.
8.​ Print the decrypted text to verify correctness.

Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Scanner;

public class SymmetricKeyExample


{
public static void main(String[] args) throws Exception
{
// Get plaintext input from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the text to encrypt: ");
String plainText = scanner.nextLine();

// Generate AES Key


KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();

// Initialize Cipher
Cipher cipher = Cipher.getInstance("AES");
// Encrypt
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
System.out.println("Encrypted Text (Base64 Encoded): " +
java.util.Base64.getEncoder().encodeToString(encrypted));

// Decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Decrypted Text: " + new String(decrypted));
}
}

Output:

Result:
The implementation of AES Symmetric Key Encryption and Decryption was
successfully executed.
Ex.No.2: Implement asymmetric key algorithms and key exchange algorithms

Aim:

To implement Asymmetric Key Cryptography using the RSA algorithm and perform key
exchange for secure communication.

Algorithm:

1.​ Generate an RSA key pair (Public Key & Private Key).
2.​ Accept plaintext input from the user.
3.​ Encrypt the plaintext using the Public Key.
4.​ Display the encrypted text in Base64 encoding for readability.
5.​ Accept encrypted input for decryption (Base64 format).
6.​ Decrypt the encrypted text using the Private Key.
7.​ Display the original decrypted message to verify correctness.

Program:
import java.security.*;
import javax.crypto.Cipher;
import java.util.Scanner;

public class RSAExample {

// Generate RSA Key Pair


public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048); // 2048-bit RSA key pair
return keyGen.generateKeyPair();
}

// Encrypt using Public Key


public static byte[] encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plainText.getBytes());
}

// Decrypt using Private Key


public static String decrypt(byte[] cipherText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decrypted = cipher.doFinal(cipherText);
return new String(decrypted);
}

public static void main(String[] args) throws Exception {


Scanner scanner = new Scanner(System.in);

// Generate RSA Key Pair


KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

// Get plaintext input from the user


System.out.print("Enter the text to encrypt: ");
String message = scanner.nextLine();
System.out.println("Original Message: " + message);

// Encrypt the message using the Public Key


byte[] encryptedMessage = encrypt(message, publicKey);
System.out.println("Encrypted Message (Base64): " +
java.util.Base64.getEncoder().encodeToString(encryptedMessage));

// Get encrypted input from the user for decryption


System.out.print("Enter the encrypted text (Base64) to decrypt: ");
String encryptedInput = scanner.nextLine();
byte[] encryptedInputBytes = java.util.Base64.getDecoder().decode(encryptedInput);

// Decrypt the message using the Private Key


String decryptedMessage = decrypt(encryptedInputBytes, privateKey);
System.out.println("Decrypted Message: " + decryptedMessage);
}
}
Output:

Result:

The implementation of Asymmetric Key Encryption and Decryption using the RSA
Algorithm was successfully executed.
Ex.No.3 Implement digital signature schemes

Aim:

To implement a Digital Signature Scheme using the RSA algorithm and SHA-256
hashing for message authentication and integrity verification.

Algorithm:

1.​ Generate an RSA Key Pair (Public Key & Private Key).
2.​ Accept a message from the user.
3.​ Generate a digital signature using the Private Key and SHA-256 hashing.
4.​ Display the generated signature in Base64 format for readability.
5.​ Verify the digital signature using the Public Key.
6.​ Test signature verification by altering the message and verifying again.
7.​ Display verification results, proving message integrity and authentication.

Program:
import java.security.*;
import java.util.Base64;
import java.util.Scanner;

public class DigitalSignatureExample {

// Generate RSA Key Pair


public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048); // 2048-bit RSA key pair
return keyGen.generateKeyPair();
}

// Sign the message using the private key


public static byte[] signMessage(String message, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA"); // Use SHA-256 with
RSA
signature.initSign(privateKey);
signature.update(message.getBytes());
return signature.sign();
}

// Verify the signature using the public key


public static boolean verifySignature(String message, byte[] signatureBytes, PublicKey
publicKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA"); // Use SHA-256 with
RSA
signature.initVerify(publicKey);
signature.update(message.getBytes());
return signature.verify(signatureBytes);
}

public static void main(String[] args) throws Exception {


Scanner scanner = new Scanner(System.in);

// Generate RSA Key Pair


KeyPair keyPair = generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();

// Get the message from the user


System.out.print("Enter the message to sign: ");
String message = scanner.nextLine();
System.out.println("Original Message: " + message);

// Sign the message


byte[] signatureBytes = signMessage(message, privateKey);
String signatureBase64 = Base64.getEncoder().encodeToString(signatureBytes);
System.out.println("Generated Signature (Base64): " + signatureBase64);

// Verify the signature using the public key


boolean isVerified = verifySignature(message, signatureBytes, publicKey);
System.out.println("Signature Verified: " + isVerified);

// Example of a manipulated message for testing signature verification


System.out.print("Enter a manipulated message to test verification: ");
String alteredMessage = scanner.nextLine();
boolean isAlteredMessageVerified = verifySignature(alteredMessage, signatureBytes,
publicKey);
System.out.println("Altered Message Signature Verified: " + isAlteredMessageVerified);
}
}
Output:

Result:

The implementation of Digital Signature Scheme using RSA and SHA-256 was successfully
executed
Ex.No.4 Installation of Wire shark, tcpdump and observe data transferred in client-server
communication using UDP/TCP and identify the UDP/TCP datagram.

Aim:

To install and use Wireshark and tcpdump to observe network traffic and analyze
TCP/UDP datagrams in client-server communication.

Procedure:

Step 1: Install Wireshark and tcpdumpOpen a terminal.

●​ Update your package list:​


sudo apt update
●​ Install Wireshark and tcpdump:​
sudo apt install wireshark tcpdump
●​ During the Wireshark installation, you may be prompted to allow non-superusers to
capture packets. Select Yes if you want to run Wireshark without sudo.

Step 2: Capture Network Traffic

Using Wireshark:

●​ Launch Wireshark:​
sudo wireshark
●​ Select the network interface (e.g., eno1 for Ethernet).
●​ Start capturing packets by clicking the Start button.
●​ Use display filters to focus on specific traffic:
○​ For TCP: tcp
○​ For UDP: udp
●​ Observe the packets in the capture window. You can see details like source IP, destination
IP, ports, and payload.

Using tcpdump:

●​ Open a terminal.
●​ Capture TCP traffic:​
sudo tcpdump -i <interface> tcp​
Replace <interface> with your network interface (e.g., eno1).
●​ Capture UDP traffic:​
sudo tcpdump -i <interface> udp
●​ Save the captured packets to a file for later analysis:​
sudo tcpdump -i <interface> -w capture.pcap​
You can open the capture.pcap file in Wireshark for detailed analysis.

Step 3: Set Up Client-Server Communication

Set up a Simple TCP Server and Client

●​ Start a TCP Server:​


nc -l 12345
●​ Connect with a TCP Client:​
nc localhost 12345
●​ Type messages in the client and observe transmission.

Set up a Simple UDP Server and Client

●​ Start a UDP Server:​


nc -u -l 12345
●​ Connect with a UDP Client:​
nc -u localhost 12345
●​ Type messages and observe the interaction.

Step 5: Analyze the Captured Packets

1.​ Use Wireshark or tcpdump to capture the traffic while running the client-server
communication.
2.​ In Wireshark:
○​ Look for packets with the protocol UDP or TCP.
○​ Expand the packet details to see the headers (source port, destination port,
checksum, etc.).
○​ For TCP, observe the sequence numbers, acknowledgment numbers, and flags
(SYN, ACK, FIN, etc.).
○​ For UDP, the header is simpler, containing only source port, destination port,
length, and checksum.
3.​ In tcpdump:
○​ The output will show the source and destination IPs and ports, along with the
protocol (UDP/TCP).
Step 6: Identify UDP/TCP Datagrams

●​ UDP Datagram:
○​ Connectionless protocol.
○​ No handshake or acknowledgment.
○​ Smaller header (8 bytes).
●​ TCP Datagram:
○​ Connection-oriented protocol.
○​ Three-way handshake (SYN, SYN-ACK, ACK).
○​ Larger header (20 bytes) with sequence numbers, acknowledgment numbers, and
flags.

Result:

The installation of Wireshark and tcpdump was successful. Network traffic was
captured, and TCP/UDP datagrams were identified and analyzed.

You might also like