Advertisement
JanisPlayer

Untitled

Nov 12th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. // find gcd
  5. int gcd(int a, int b) {
  6. int t;
  7. while(1) {
  8. t= a%b;
  9. if(t==0)
  10. return b;
  11. a = b;
  12. b= t;
  13. }
  14. }
  15. int main() {
  16. //2 random prime numbers
  17. double p = 99991;
  18. double q = 897667;
  19. double n=p*q;//calculate n
  20. double track;
  21. double phi= (p-1)*(q-1);//calculate phi
  22. //public key
  23. //e stands for encrypt
  24. double e=7;
  25. //for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
  26. while(e<phi) {
  27. track = gcd(e,phi);
  28. if(track==1)
  29. break;
  30. else
  31. e++;
  32. }
  33. //private key
  34. //d stands for decrypt
  35. //choosing d such that it satisfies d*e = 1 mod phi
  36. double d1=1/e;
  37. double d=fmod(d1,phi);
  38. double message = 567799;
  39. double c = pow(message,e); //encrypt the message
  40. double m = pow(c,d);
  41. c=fmod(c,n);
  42. m=fmod(m,n);
  43. cout<<"Original Message = "<<message;
  44. cout<<"\n"<<"p = "<<p;
  45. cout<<"\n"<<"q = "<<q;
  46. cout<<"\n"<<"n = pq = "<<n;
  47. cout<<"\n"<<"phi = "<<phi;
  48. cout<<"\n"<<"e = "<<e;
  49. cout<<"\n"<<"d = "<<d;
  50. cout<<"\n"<<"Encrypted message = "<<c;
  51. cout<<"\n"<<"Decrypted message = "<<m;
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement