RSA.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. long double find_pow(long double a,long double b, long double c)
  5. {
  6. long double i,final=1;
  7. for(i=0;i<b;i++)
  8. final=fmodl(final*fmodl(a,c),c);
  9. return fmodl(final,c);
  10. }
  11. long double find_d(long double e,long double phi_of_n)
  12. {
  13. int i;
  14. e=fmodl(e,phi_of_n);
  15. for(i=0;i<phi_of_n;i++)
  16. if(fmodl((e*i),phi_of_n)==1)
  17. break;
  18. return i;
  19. }
  20. int main()
  21. {
  22. long double M,p,q,e,n,phi_of_n,C,d;
  23. cout<<"\nEnter the plain text\n";
  24. cin>>M;
  25. cout<<"\nEnter the two prime numbers\n";
  26. cin>>p>>q;
  27. cout<<"\nEnter e\n";
  28. cin>>e;
  29. n=p*q;
  30. phi_of_n = (p-1)*(q-1);
  31. C=find_pow(M,e,n);
  32. cout<<"\nEncrypted Text is "<<C;
  33. d=find_d(e,phi_of_n);
  34. M=find_pow(C,d,n);
  35. cout<<"\nDecrypted Text is "<<M<<endl;
  36. return 0;
  37. }
  38. /*
  39. OUTPUT:-
  40. Enter the plain text
  41. 88
  42. Enter the two prime numbers
  43. 17 11
  44. Enter e
  45. 7
  46. Encrypted Text is 11
  47. Decrypted Text is 88
  48. */