0% found this document useful (0 votes)
14 views3 pages

CSS Experiment No 2 Printout

The document outlines an experiment aimed at implementing the RSA algorithm using C programming. It includes code for checking prime numbers, calculating GCD, and performing modular exponentiation, along with user input for prime numbers and data encryption and decryption. The conclusion states that the experiment successfully studied the RSA algorithm and solved the problem.

Uploaded by

Adiba Khan
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)
14 views3 pages

CSS Experiment No 2 Printout

The document outlines an experiment aimed at implementing the RSA algorithm using C programming. It includes code for checking prime numbers, calculating GCD, and performing modular exponentiation, along with user input for prime numbers and data encryption and decryption. The conclusion states that the experiment successfully studied the RSA algorithm and solved the problem.

Uploaded by

Adiba Khan
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/ 3

Experiment No :- 02

Aim:- Implementation of RSA algorithm using c.


Roll No:- 25
Code:-
#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int checkPrime(int n) {

int i;

int m = n / 2;

for (i = 2; i <= m; i++) {

if (n % i == 0) {

return 0; // Not Prime

return 1; // Prime

int findGCD(int n1, int n2) {

int i, gcd;

for (i = 1; i <= n1 && i <= n2; ++i) {

if (n1 % i == 0 && n2 % i == 0)

gcd = i;

return gcd;

int powMod(int a, int b, int n) {

long long x = 1, y = a;

while (b > 0) {

if (b % 2 == 1)

x = (x * y) % n;
y = (y * y) % n; // Squaring the base

b /= 2;

return x % n;

int main(int argc, char* argv[]) {

int p, q;

int n, phin;

int data, cipher, decrypt;

while (1) {

printf("Enter any two prime numbers: ");

scanf("%d %d", &p, &q);

if (!(checkPrime(p) && checkPrime(q)))

printf("Both numbers are not prime. Please enter prime numbers only...\n");

else if (!checkPrime(p))

printf("The first prime number you entered is not prime, please try again...\n");

else if (!checkPrime(q))

printf("The second prime number you entered is not prime, please try again...\n");

else

break;

n = p * q;

phin = (p - 1) * (q - 1);

int e = 13;

int k = 4;

int d = 0;

d = ((k * phin) + 1) / e;

printf("Value of e: %d\nValue of d: %d\nValue of phin: %d\n", e, d, phin);

printf("Enter some numerical data: ");

scanf("%d", &data);
cipher = powMod(data, e, n);

printf("The cipher text is: %d\n", cipher);

decrypt = powMod(cipher, d, n);

printf("The decrypted text is: %d\n", decrypt);

return 0;

Output:-

Conclusion:- In this experiment we have studied RSA algoritm and


Solve Problem.

You might also like