-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDiffie Hellman.cpp
More file actions
109 lines (93 loc) · 2.46 KB
/
Diffie Hellman.cpp
File metadata and controls
109 lines (93 loc) · 2.46 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n,p;
cout<<"Elliptical Curve General Form y^2 mod p = (x^3 + Ax + B) mod p / n ";
cout<<"Enter the value of P: \n";
cin>>p;
n=p;
int LHS[2][n], RHS[2][n], a,b,i,j;
cout<<"\nEnter the Value of a: \n";
cin>>a;
cout<<"\nEnter the Value of b: \n";
cin>>b;
cout<<"\nCurrent Elliptic Curve \t\t ----> y^2 mod "<<p<<"= (x^3 + "<<a<<"x + "<<a<<") mod "<<p<<"\n\n";
for(int i=0;i<n;i++)
{
LHS[0][i] = i;
RHS[0][i] = i;
LHS[1][i] = ((i*i*i)+a*i+b)%p;
RHS[1][i] = (i*i) % p;
}
int c = 0;
vector<int> arr_x,arr_y;
for(i =0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(LHS[1][i] == RHS[i][j])
{
c++;
arr_x.push_back(LHS[0][i]);
arr_y.push_back(RHS[0][j]);
}
}
}
cout<<endl<<"Generated Points are:"<<endl;
for(int i=0; i<c; i++)
{
cout<<i+1<<"\t("<<arr_x[i]<<","<<arr_y[i]<<")"<<endl;
}
cout<<"Base Point: ("<<arr_x[0]<<","<<arr_y[0]<<")"<<endl;
int k,d,m;
cout<<"Enter the random number 'd' i.e. private key of sender (d/n) \n";
cin>>d;
int qx = d*arr_x[0];
int qy = d*arr_y[0];
//q is the public key of sender
//Encryption before sending otp to user
cout<<" \n Encryption Before sending otp to user";
cout<<"\nEnter the random number 'k' (k<n) \n";
cin>>k;
cout<<"Enter the Amount to be withdrawn:\n";
cin>>m;
int c1x = k*arr_x[0];
int c1y = k*arr_y[0];
cout<<"Value of c1: ("<<c1x<<","<<c1y<<")"<<"\n";
int c2x = k*qx+m;
int c2y = k*qy+m;
cout<<"Value of c2: ("<<c2x<<","<<c2y<<")"<<"\n";
cout<<"OTP for the transaction is c1x c1y c2x c2y: ";
if(c1x<10)
cout<<"0"<<c1x;
else
cout<<c1x;
if(c1y<10)
cout<<"0"<<c1y;
else
cout<<c1y;
if(c2x<10)
cout<<"0"<<c2x;
else
cout<<c2x;
if(c2y<10)
cout<<"0"<<c2y;
else
cout<<c2y;
cout<<"\n\n Decryption on Bank Server Side";
//DECRYPTION (Bank Server)
cout<<"\n\n Enter the OTP: ";
int otp;
cin>>otp;
int cipher[4];
for(int i=0;i<4;i++)
{cipher[i] = otp%100;
otp=otp/100;}
cout<<"\n The Amount received is:\n";
int Mx = cipher[1]-d*cipher[3];
int My = cipher[0]-d*cipher[2];
cout<<Mx<<"\n";
return 0;
}