-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerkle-Hellman1.cpp
88 lines (87 loc) · 1.4 KB
/
Merkle-Hellman1.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<fstream>
#define LL long long
#define N 8
using namespace std;
bool is_prime(int n)
{
int i=2;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
LL pow_mod(LL a,LL b,LL p)
{
LL res=1;
while(b)
{
if(b&1)
res=(res*a)%p;
a=(a*a)%p;
b=b>>1;
}
return res;
}
LL Fermat(LL a,LL p)
{
return pow_mod(a,p-2,p);
}
int main()
{
vector<int> privatekey;
vector<int> publickey;
int sum=0,a=1,modnumber,mulnumber,inversemul;
for(int i=1;i<=N;i++)
{
privatekey.push_back(a);
sum+=a;
a=sum+1;
}
sum=a;
srand(time(0));
do
{
modnumber=rand()%500;
}while(modnumber<=sum||!is_prime(modnumber));
do
{
mulnumber=rand()%100;
}while(gcd(modnumber,mulnumber)!=1);
inversemul=Fermat(mulnumber,modnumber);
ofstream fout("privatekey.txt");
streambuf *coutbackup;
coutbackup=cout.rdbuf(fout.rdbuf());
for(int i=1;i<=N;i++)
{
cout<<privatekey[i-1]<<" ";
}
cout<<endl;
//cout<<mulnumber<<endl;
cout<<inversemul<<endl;
cout<<modnumber<<endl;
fout.close();
fout.open("publickey.txt");
//ofstream fout("publickey.txt");
//streambuf *coutbackup;
coutbackup=cout.rdbuf(fout.rdbuf());
for(int i=1;i<=N;i++)
{
a=(mulnumber*privatekey[i-1])%modnumber;
publickey.push_back(a);
cout<<a<<" ";
}
cout<<endl;
cout<<mulnumber;
fout.close();
}