Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- void main()
- {
- int gcd(int,int);
- int p,q,d=0,i,k=0,n=0,val,e=0;
- long int val2;
- int t;
- char pt[50],ct[50],dt[50];
- printf("\n enter p,q values");
- scanf("%d %d",&p,&q);
- n=p*q;
- k=(p-1)*(q-1);
- for(i=2;i<k;i++)
- {
- if(k%i!=0)
- {
- t=gcd(k,i);
- e=t;
- break;
- }
- }
- for(i=1;i<k;i++)
- {
- if(((i*e)%k)==1)
- {
- d=i;
- break;
- }
- }
- printf("\n e=%d d=%d",e,d);
- printf("enter the plain text");
- scanf("%s",pt);
- i=0;
- while(pt[i]!='\0')
- {
- val=pt[i]-96;
- val2=pow(val,e);
- ct[i]=val2%n;
- i++;
- }
- ct[i]='\0';
- printf("the cipher text is %s",ct);
- i=0;
- while(ct[i]!='\0')
- {
- val2=pow(ct[i],d);
- dt[i]=val2%n;
- dt[i]=dt[i]+96;
- i++;
- }
- dt[i]='\0';
- printf("\n the plain text data is %s", dt);
- }
- int gcd(int k,int i)
- {
- int r;
- r=k%i;
- while(r!='\0')
- {
- k=i;
- i=r;
- r=k%i;
- if(r==1)
- {
- return k;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement