forked from durgesh2001/hacktoberfest_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionAlgo.cpp
More file actions
55 lines (46 loc) · 950 Bytes
/
EncryptionAlgo.cpp
File metadata and controls
55 lines (46 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<math.h>
using namespace std;
int gcd(int a, int b) {
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b;
b= t;
}
}
int main() {
double p,q,message ;
cout<<"enter 2 prime numbers p and q respectively :";
cin>>p>>q;
cout<<endl;
cout<<"enter the message which needs to be Encrypted:";
cin>>message;
double n=p*q;
double track;
double phi= (p-1)*(q-1);
double e=7;
while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
}
double d1=1/e;
double d=fmod(d1,phi);
double c = pow(message,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<endl;
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;
}