0% found this document useful (0 votes)
10 views

CSS 02

The document outlines an experiment to implement the RSA asymmetric key algorithm using Turbo C. It includes code for calculating the greatest common divisor, modular multiplicative inverse, and functions for encryption and decryption. The output demonstrates the generation of public and private keys, as well as the encryption and decryption of a plaintext value.

Uploaded by

rp505714
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)
10 views

CSS 02

The document outlines an experiment to implement the RSA asymmetric key algorithm using Turbo C. It includes code for calculating the greatest common divisor, modular multiplicative inverse, and functions for encryption and decryption. The output demonstrates the generation of public and private keys, as well as the encryption and decryption of a plaintext value.

Uploaded by

rp505714
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/ 3

NAME : DUYESH HEMANT PAWAR

Name
T.E/A3. : Jayraj Patil
ROLL NO. 154
T.E/A3 Roll No. 161
UID NO. 122CP1184A

Experiment No. 02

Aim : Write a program to study & implement


Rivest Shamir Adelman (RSA) asymmetric key

Software Used : Turbo C

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

// Function to calculate the greatest common


divisor

int gcd(int a, int b)

while (b != 0)

int temp = b;

b = a % b;

a = temp;

return a;

// Function to calculate the modular multiplicative


inverse; int modInverse(int e, int phi)
}

int main()

{ {

for (int i = 1; i < phi; i++) int p,q,plaintext;

{ printf("\nEnter two prime number\n");

if ((e * i) % phi == 1) scanf("%d%d",&p,&q);

return i; // Step 2: Compute n and phi(n)

} int n = p * q;

return -1; // If no inverse exists int phi = (p - 1) * (q - 1);

} // Step 3: Choose e such that 1 < e < phi and gcd(e,


phi) = 1
// Function to calculate power modulo (base^exp) %
mod int e = 17; // Example value for e

long long powerMod(long long base, long long exp, while (gcd(e, phi) != 1)
long long mod)
{
{
e++;
long long result = 1;
}
base = base % mod;
// Step 4: Calculate d (modular multiplicative
while (exp > 0) { inverse of e modulo phi)

if (exp % 2 == 1) int d = modInverse(e, phi);

{ if (d == -1)

result = (result * base) % mod; {

} printf("No modular inverse found!\n");

exp = exp >> 1; return 1;

base = (base * base) % mod; }

} printf("Public Key: (e = %d, n = %d)\n", e, n);

return result; printf("Private Key: (d = %d, n = %d)\n", d, n);


// Step 5: Encryption

printf("\nEnter the plain text\n");

scanf("%d",&plaintext);// Example plaintext

long long ciphertext = powerMod(plaintext, e, n);

printf("Plaintext: %d\n", plaintext);

printf("Ciphertext: %lld\n", ciphertext);

// Step 6: Decryption

long long decrypted = powerMod(ciphertext, d, n);

printf("Decrypted Text: %lld\n", decrypted);

return 0;

OUTPUT

Enter two prime number

11

23

Public Key: (e = 17, n = 253)

Private Key: (d = 13, n = 253)

Enter the plain text

143

Plaintext: 143

Ciphertext: 176

Decrypted Text: 143

You might also like