Your Ad Here

Wednesday, January 23, 2008

RSA Algorithm

/* C Program For The Implementation Of RSA*/

#include<>
#include<>

void main()
{
long int p,q,n,fn,e,s=0,d=1,C[100],M;
char m[100];
unsigned long int b=0,c=0;
int i,j,t;
clrscr();
printf("Enter the value of p and q: ");
scanf("%ld%ld",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
printf("\nEnter the Encryption key e: ");
scanf("%ld",&e);
do
{ s=(d*e)%fn;
d++;
}while(s!=1);
d=d-1;
printf("\n\tPublic key (%ld,%ld) ",e,n);
printf("\n\tPrivate key (%ld,%ld) ",d,n);
printf("\n\nEnter message :\n");
scanf("%s",m);
for(j=0;j< strlen(m);j++)
{
t=(int)m[j];
c=1;
for(i=0;i< e;i++)
c=c*t%n;
c=c%n;
printf("%d ",c);
}
printf("\n\nEnter cipher text :\n");
for(i=0;i< strlen(m);i++)
scanf("%ld",&C[i]);
printf("\n\n\tPlaintext :");

for(j=0;j< strlen(m);j++)
{
M=1;
for(i=0;i< d;i++)
M=M*C[j]%n;
M=M%n;
printf("%c",M);
}
getch();
}
/********************* OUTPUT ***********************
Enter the value of p and q: 7 17

Enter the Encryption key e: 5

Public key (5,119)
Private key (77,119)

Enter message :
Lionel_Noronha
111 56 76 94 33 75 23 57 76 88 76 94 83 20

Enter cipher text :
111 56 76 94 33 75 23 57 76 88 76 94 83 20


Plaintext :Lionel_Noronha
****************************************************/

6 comments:

Anonymous said...

Can any1 give a complete source code for data encryption standard(DES)in C. ....
thanks...

John said...

This is not DES. It's RSA.

Anonymous said...

take your p,q values 101 , 123 ( 3digit prime ) and your code will be failed...
your code is limited only for selected prime no....

Anonymous said...

123 is not a prime, thats y so,,
first learn to calculate a prime number,

Anonymous said...

first calculate prime no correctly,,123 is nt prime

Anonymous said...

i try to run it but error..

Your Ad Here