-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeffie_helmin.cpp
95 lines (68 loc) · 2.52 KB
/
deffie_helmin.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
89
90
91
92
93
94
95
#include <iostream>
#include <cmath>
using namespace std;
int set_key;
//* FUNCTION TO SEE IF THE PASSED NUMBER IS PRIME OR NOT.* //
bool is_prime(int n){
if(n < 2){
return false;
}
else{
for(int i=2 ; i<=sqrt(n) ; i++){
if(n % i == 0){
return false;
}
}
return true;
}
}
bool key_found = false;
//** FIND THE KEY COMBINATIONS [BIG_P, ALHPA, a, b, PKA, PKB] */
void find_shared_key(int P) {
// Try different prime numbers P
for (; P < 1000; ++P) {
if (!is_prime(P)) {
continue;
}
// Try different values for the base alpha
for (int alpha = 2; alpha < P; alpha++){
// Check if alpha is a primitive root modulo P
if(!is_prime(alpha)){
continue;
}
// Try different values for Alice's private key a
for(int a=1 ; a<P ; a++){
int A = static_cast<int>(pow(alpha, a)) % P; // Alice's PUBLIC KEY.
// Try different values for Bob's private key b
for(int b = 1; b < P; b++){
int B = static_cast<int>(pow(alpha, b)) % P; // Bob's PUBLIC KEY.
// CALCULATE THE SHARED KEYS.
int shared_key_alice = static_cast<int>(pow(B, a)) % P;
int shared_key_bob = static_cast<int>(pow(A, b)) % P;
if (shared_key_alice == set_key && shared_key_alice == shared_key_bob) {
cout<<"\nLARGE PRIME P = " << P << endl;
cout<<"ALPHA = " << alpha << endl;
cout<<"ALICE KI PRIVATE KEY= " << a << endl;
cout<<"BOB KI PRIVATE KEY= " << b << endl;
cout<<"Shared key = " << shared_key_alice << endl;
key_found = true;
return;
}
} // Bob's private key loop
} // Alice's private key loop
} // ALPHA LOOP
} // OUTER LOOP TILL WE GET THE POSSIBLE COMBINATIONS FOR THE SHARED KEY.
//cout << "\nNO MATCH FOUND FOR 13\n" << endl;
}
//** MAIN FUNCTION. */
int main(){
cout<<"\n ENTER THE SHARED KEY. [FOR WHICH YOU WANT TO PERFORM BRUTE FORCE ATTACK] \n";
cin>>set_key;
for(int i=0 ; i<1000 ; i++){
find_shared_key(i);
if(key_found){
break;
}
}
return 0;
}