Advertisement
WarPro

rsa

Nov 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. void main()
  4. {
  5. int gcd(int,int);
  6. int p,q,d=0,i,k=0,n=0,val,e=0;
  7. long int val2;
  8. int t;
  9. char pt[50],ct[50],dt[50];
  10. printf("\n enter p,q values");
  11. scanf("%d %d",&p,&q);
  12. n=p*q;
  13. k=(p-1)*(q-1);
  14. for(i=2;i<k;i++)
  15. {
  16. if(k%i!=0)
  17. {
  18. t=gcd(k,i);
  19. e=t;
  20. break;
  21. }
  22. }
  23. for(i=1;i<k;i++)
  24. {
  25. if(((i*e)%k)==1)
  26. {
  27. d=i;
  28. break;
  29. }
  30. }
  31. printf("\n e=%d d=%d",e,d);
  32. printf("enter the plain text");
  33. scanf("%s",pt);
  34. i=0;
  35. while(pt[i]!='\0')
  36. {
  37. val=pt[i]-96;
  38. val2=pow(val,e);
  39. ct[i]=val2%n;
  40. i++;
  41. }
  42. ct[i]='\0';
  43. printf("the cipher text is %s",ct);
  44. i=0;
  45. while(ct[i]!='\0')
  46. {
  47. val2=pow(ct[i],d);
  48. dt[i]=val2%n;
  49. dt[i]=dt[i]+96;
  50. i++;
  51. }
  52. dt[i]='\0';
  53. printf("\n the plain text data is %s", dt);
  54.  
  55. }
  56. int gcd(int k,int i)
  57. {
  58. int r;
  59. r=k%i;
  60. while(r!='\0')
  61. {
  62. k=i;
  63. i=r;
  64. r=k%i;
  65. if(r==1)
  66. {
  67. return k;
  68. }
  69. }
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement