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

RSA Program

The document provides a C program that implements the RSA algorithm for data encryption and decryption. It includes functions for modular exponentiation and calculating the greatest common divisor (GCD), and it prompts the user to input a string to encrypt and then decrypt. The program successfully encrypts the input string 'iselab' and displays both the encrypted values and the decrypted output, confirming the integrity of the data.

Uploaded by

20p0801mccpuc
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)
2 views3 pages

RSA Program

The document provides a C program that implements the RSA algorithm for data encryption and decryption. It includes functions for modular exponentiation and calculating the greatest common divisor (GCD), and it prompts the user to input a string to encrypt and then decrypt. The program successfully encrypts the input string 'iselab' and displays both the encrypted values and the decrypted output, confirming the integrity of the data.

Uploaded by

20p0801mccpuc
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

Program10:

Write a C program to implement RSA algorithm, to encrypt the data while sending it and
decrypt while receiving.

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

unsigned int mod_exp(int base,int exp,int n)

unsigned int i,pow=1;

for(i=0;i<exp;i++)

pow=(pow*base)%n;

return pow;

int gcd(int m,int n)

while(m!=n)

if(m==n || n==1)

m=1,n=1;

break;

else

if(m>n)

m=m-n;

else

n=n-m;
}

return m;

int main()

int p=47,q=71;

int n=p*q;

int d=1,e=n-1,i,j;

char in[20]=" ";

unsigned int out[20]={0},decr[20];

int z;

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

for(;;)

if(gcd(e,z)==1)

break;

e--;

for(;;)

if((e*d)%z==1)

break;

d++;

printf("\nenter the input string:\n");

scanf("%s",in);

printf("\nThe entered string is: %s",in);


printf("\n");

printf("The encrypted string is \n");

for(i=0;i<strlen(in);i++)

out[i]=mod_exp(in[i],e,n);

printf("%c=%3d\n",in[i],out[i]);

printf("\nDecrypted string is: ");

for(i=0;i<strlen(in);i++)

decr[i]=mod_exp(out[i],d,n);

printf("%c",(char)decr[i]);

printf("\n");

return 0;

Output:

enter the input string:

iselab

The entered string is: iselab

The encrypted string is

i=886

s=3230

e=2515

l=2309

a=2653

b=2024

Decrypted string is: iselab

You might also like