Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- // find gcd
- int gcd(int a, int b) {
- int t;
- while(1) {
- t= a%b;
- if(t==0)
- return b;
- a = b;
- b= t;
- }
- }
- int main() {
- //2 random prime numbers
- double p = 99991;
- double q = 897667;
- double n=p*q;//calculate n
- double track;
- double phi= (p-1)*(q-1);//calculate phi
- //public key
- //e stands for encrypt
- double e=7;
- //for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
- while(e<phi) {
- track = gcd(e,phi);
- if(track==1)
- break;
- else
- e++;
- }
- //private key
- //d stands for decrypt
- //choosing d such that it satisfies d*e = 1 mod phi
- double d1=1/e;
- double d=fmod(d1,phi);
- double message = 567799;
- double c = pow(message,e); //encrypt the message
- double m = pow(c,d);
- c=fmod(c,n);
- m=fmod(m,n);
- cout<<"Original Message = "<<message;
- cout<<"\n"<<"p = "<<p;
- cout<<"\n"<<"q = "<<q;
- cout<<"\n"<<"n = pq = "<<n;
- cout<<"\n"<<"phi = "<<phi;
- cout<<"\n"<<"e = "<<e;
- cout<<"\n"<<"d = "<<d;
- cout<<"\n"<<"Encrypted message = "<<c;
- cout<<"\n"<<"Decrypted message = "<<m;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement