Menu

Computer Networks - (LAB PROGRAMS)


Aim:

 Implement data encryption and data decryption

Solution :

Week 7

Using RSA algorithm encrypts a text data and Decrypt the same.


Source Code:

File Name: encrypt_decrypt.c

#include<stdio.h>
#include <string.h>
#include <math.h>

// Function to perform modular exponentiation: (base^exp) % mod
int modular_exponentiation(unsigned int base, unsigned int exp, unsigned int mod) {
    unsigned long int result = 1;
    for (int i = 1; i <= exp; i++) {
        result = (result * base) % mod;
    }
    return (unsigned int) result;
}

void main() {
    char message[100];
    unsigned int plaintext[100], ciphertext[100];
    unsigned int n = 253;      // Modulus (product of two primes: p * q)
    unsigned int e = 13;       // Public exponent
    unsigned int d = 17;       // Private exponent
    int i;


    // Input plaintext
    printf("Enter the plain text: ");
    fgets(message, sizeof(message), stdin);

    // Strip newline
    size_t len = strlen(message);
    if (len > 0 && message[len - 1] == '\n') {
        message[len - 1] = '\0';
    }


    // Convert characters to ASCII integers
    for (i = 0; i < strlen(message); i++) {
        plaintext[i] = (unsigned int) message[i];
    }

    // Encrypt each character using RSA encryption: ct = pt^e mod n
    printf("\nEncrypted Cipher Text (Numeric): ");
	 for (i = 0; i < strlen(message); i++) {
        ciphertext[i] = modular_exponentiation(plaintext[i], e, n);
        printf("%d ", ciphertext[i]);
    }

    // Print original plain text
    printf("\nOriginal Plain Text: ");
    for (i = 0; i < strlen(message); i++) {
        printf("%c", plaintext[i]);
    }

    // Decrypt each character using RSA decryption: pt = ct^d mod n
    printf("\nDecrypted Plain Text: ");
    for (i = 0; i < strlen(message); i++) {
        plaintext[i] = modular_exponentiation(ciphertext[i], d, n);
        printf("%c", plaintext[i]);
    }


}


Output:


$ gcc encrypt_decrypt.c
$ ./a.out
Enter the plain text: StudyGlance

Encrypted Cipher Text (Numeric): 172 139 211 133 220 4 3 113 209 66 173
Original Plain Text: StudyGlance
Decrypted Plain Text: StudyGlance



Related Content :

Computer Networks Lab Programs

1) Implement the data link layer framing methods such as character, character-stuffing and bit stuffing.   View Solution

2) Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP   View Solution

3) Develop a simple data link layer that performs the flow control using the sliding window protocol, and loss recovery using the Go-Back-N mechanism.   View Solution

4) Implement Dijsktra’s algorithm to compute the shortest path through a network   View Solution

5) Take an example subnet of hosts and obtain a broadcast tree for the subnet.   View Solution

6) Implement distance vector routing algorithm for obtaining routing tables at each node.   View Solution

7) Implement data encryption and data decryption   View Solution

8) Write a program for congestion control using Leaky bucket algorithm.   View Solution

9) Write a program for frame sorting techniques used in buffers.   View Solution

10) Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
iv.Analysis and Statistics & Filters.
  View Solution

11) How to run Nmap scan   View Solution

12) Operating System Detection using Nmap   View Solution

13) Do the following using NS2 Simulator
i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate & Throughput.
vi. Simulate to Plot Congestion for Different Source/Destination
vii. Simulate to Determine the Performance with respect to Transmission of Packets
  View Solution