Headlines
Loading...
Data Communication network(DCN) program to implement RSA algorithm using C.

Data Communication network(DCN) program to implement RSA algorithm using C.

Write a program to implement RSA algorithm using C.

Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main()
{
int p,q,n,e=1,j;
int d=1,i1;
int t1,t2,pt[10],ct[10],rt[10];
char i[10];
clrscr();
printf("\n\nEnter the two prime no. : ");
scanf("%d %d",&p,&q);
printf("\n\nEnter the message to be sent : ");
scanf("%s",i);
i1=strlen(i);
for(j=0;j<i1;j++)
pt[j]=i[j]-96;

n=p*q;
t1=p-1;
t2=q-1;
while((t1*t2)%e==0)
e++;
for(j=0;j<i1;j++)
ct[j]=((int)pow(pt[j],e))%n;
printf("\nSender Side:");
printf("\n----------------");
printf("\nPublic Key(e)=%d",e);
for(j=0;j<i1;j++)
printf("\nCipher Text = %d",ct[j]);

//Receiver side
printf("\n\nReceiver Side:");
printf("\n-----------------");
while((d*e)%(t1*t2)!=1)
d++;
printf("\n\nPrivate key(d)=%d",d);
for(j=0;j<i1;j++)
{
rt[j]=((int)pow(ct[j],d))%n;
printf("\n\nPlain Text=%d",rt[j]);
}
printf("\n\nDecrypted Message:");
for(j=0;j<i1;j++)
{
rt[j]=rt[j]+96;
printf("%c",rt[j]);
}
getch();
}

Output:

Enter the two prime no. : 3
5

Enter the message to be sent : abcdef

Sender Side:
----------------
Public Key(e)=3
Cipher Text = 1
Cipher Text = 8
Cipher Text = 12
Cipher Text = 4
Cipher Text = 5
Cipher Text = 6

Receiver Side:
-----------------

Private key(d)=3

Plain Text=1

Plain Text=2

Plain Text=3

Plain Text=4

Plain Text=5

Plain Text=6

Decrypted Message: abcdef

*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***