๐ŸŒ
B.TECH III YEAR โ€ข I SEMESTER โ€ข R22

Computer Networks - Lab Manual

Computer Networks Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š I Semester ๐ŸŒ Computer Networks ๐Ÿงช Lab Program
Selected Program

Implement data encryption and data decryption.

โ† All Computer Networks Programs

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


๐Ÿ’ป Run This C Program

Implement data encryption and data decryption.
The selected C program is loaded automatically into the editor. Edit the program, provide input through STDIN if required and click Run.

The compiler is embedded using OneCompiler. Your C program runs inside the embedded C environment; Study Glance does not execute C code directly on your web server. Enter the input values of the program in the STDIN box before clicking Run.
โ† Back to Computer Networks Lab Programs ๐Ÿ“š All Lab Manuals