-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffie_Hellman.cpp
More file actions
74 lines (60 loc) · 2.32 KB
/
Diffie_Hellman.cpp
File metadata and controls
74 lines (60 loc) · 2.32 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
//============================================================================
// Name : Diffie_Hellman.cpp
// Author : SidPro
// Version : 1.0
// Description :
/*
this is public-key cryptography and is generally
referred to as Diffie-Hellman key exchange
The purpose of the algorithm is to enable two users to securely exchange a
key that can then be used for subsequent symmetric encryption of messages. The
algorithm itself is limited to the exchange of secret values
For any integer b and a primitive root a of prime number p, we can find a
unique exponent i such that
b = a^i (mod p) where 0 <=i<=(p - 1)
The exponent i is referred to as the discrete logarithm of b for the base a, mod p.
We express this value as dloga,p(b).
*/
//============================================================================
#include <iostream>
#include <cmath>
using namespace std;
// Power function to return value of a^b(mod q)
long long int power(long long int a, long long int b,long long int q){
if (b == 1)return a;
else return (((long long int)pow(a, b)) % q);
}
int main() {
//there are two publicly known numbers: a prime number q and an integer a that is a primitive root of q.
//Suppose the users A and B wish to create a shared key.
long long int a=2,q=11;
// Alice generates a private key XA such that XA<q
long long int XA=8,YA,KA;
//Alice calculates a public key YA = a^XA mod q
YA = power(a,XA,q);
// Bob generates a private key XB such that XB<q
long long int XB=4,YB,KB;
// Bob calculates a public key YB = a^XB mod q
YB = power(a,XB,q);
// Alice receives Bob’s public key YB in plaintext
// Alice calculates shared secret key K = YB^XA mod q
KA = power(YB,XA,q);
// Bob receives Alice’spublic key YA in plaintext
// Bob calculates shared secret key K = YA^XB mod q
KB = power(YA,XB,q);
/*
above two calculations produce identical results:
K = (YB)^XA mod q
= (a^XB mod q)^XA mod q
= (a^XB)^XA mod q by the rules of modular arithmetic
= a^(XB*XA) mod q
= (a^XA)^XB mod q
= (a^XA mod q)^XB mod q
= (YA)^XB mod q
The result is that the two sides have exchanged a secret value
*/
cout<<"Secret key for the Alice is : "<<KA<<"\n";
cout<<"Secret Key for the Bob is : "<<KB<<"\n";
cout<<"Two sides have exchanged a secret key";
return 0;
}