Skip to content

Commit b8c2cce

Browse files
authored
Merge pull request #1100 from NikitaEmberi/main
Fix #1098 Diffie Hellman Key Exchange Algorithm
2 parents 29ae766 + 4acb26e commit b8c2cce

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import socket
2+
3+
def power(a,b,c):
4+
return ((a**b)%c)
5+
6+
s = socket.socket()
7+
print("socket created")
8+
s.bind(('192.168.1.101',9999))
9+
print('Bind completed')
10+
s.listen(3)
11+
print('Server listening')
12+
13+
while True:
14+
c,addr = s.accept()
15+
print('client connected', addr)
16+
p = int(input("Alice, Enter a prime number P: "))
17+
q = int(input("Alice, Enter a prime number Q: "))
18+
c.send(bytes(str(p),'utf-8'))
19+
c.send(bytes(str(q),'utf-8'))
20+
r = int(input("Alice, Enter your private number: "))
21+
x = power(p,r,q)
22+
print(f"X is: {x}")
23+
c.send(bytes(str(x),'utf-8'))
24+
y = c.recv(1024).decode()
25+
ka = power(int(y),r,q)
26+
print(f"The Key with Alice is: {ka}")
27+
c.close()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import socket
2+
def power(a,b,c):
3+
return ((a**b)%c)
4+
5+
c = socket.socket()
6+
c.connect(('192.168.1.101',9999))
7+
p = c.recv(1024).decode()
8+
q = c.recv(1024).decode()
9+
x = c.recv(1024).decode()
10+
s=int(input("Bob, enter your private key: "))
11+
y = power(int(p),s,int(q))
12+
c.send(bytes(str(y) ,'utf-8'))
13+
kb = power(int(x),s,int(q))
14+
print(f"The Key with Bob is: {kb}")
15+
16+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Diffie Hellman Key Exchange Algorithm
2+
The Diffie–Hellman (DH) Algorithm is a key-exchange protocol that enables two parties communicating over public channel to establish a mutual secret without it being
3+
transmitted over the Internet. DH enables the two to use a public key to encrypt and decrypt their conversation or data using symmetric cryptography. </br>
4+
***- https://www.hypr.com*** </br>
5+
6+
Symmetric encryption is a type of encryption where only one key (a secret key) is used to both encrypt and decrypt electronic information. The entities communicating via
7+
symmetric encryption must exchange the key so that it can be used in the decryption process. This encryption method differs from asymmetric encryption where a pair of keys,
8+
one public and one private, is used to encrypt and decrypt messages.
9+
10+
By using symmetric encryption algorithms, data is converted to a form that cannot be understood by anyone who does not possess the secret key to decrypt it.
11+
Once the intended recipient who possesses the key has the message, the algorithm reverses its action so that the message is returned to its original and understandable form.
12+
13+
To understand the Math behind DH Algorithm, Kindly visit https://mathworld.wolfram.com/Diffie-HellmanProtocol.html
14+
15+
## Libraries required for executing the algorithm :
16+
- Socket
17+
- Before working with the project, you need to keep ready ip address of your system. You can retrieve ip address by :
18+
In Command Prompt, run following command
19+
- On Windows
20+
```ifconfig```
21+
- On MAC / LINUX based systems
22+
```ipconfig```
23+
24+
## Steps to run the program
25+
**1.** Fork [this](https://github.com/Tejas1510/Hacking-Scripts/) repository.
26+
Click on the <a href="https://github.com/Tejas1510/Hacking-Scripts/"><img src="https://img.icons8.com/ios/24/000000/code-fork.png"></a> symbol at the top right corner.
27+
28+
**2.** Clone the forked repository.
29+
30+
```bash
31+
git clone https://github.com/<your-github-username>/Hacking-Scripts
32+
```
33+
**3.** Navigate to the project directory.
34+
35+
```bash
36+
cd Hacking-Scripts/python/RSA Algorithm
37+
```
38+
**4.** Run the python file.
39+
- First run alice.py
40+
```bash
41+
python alice.py
42+
```
43+
- Then run bob.py
44+
```bash
45+
python bob.py
46+
```
47+
# Demonstration of Exchange :
48+
## Output of Alice(Sender) file :
49+
-You need to enter ip address of your system at underlined part :
50+
![image](https://user-images.githubusercontent.com/59737567/139343247-96b5a47b-5e37-486b-8b98-d61f1fa1f75d.png)
51+
52+
## Output of Bob(Receiver) file :
53+
![image](https://user-images.githubusercontent.com/59737567/139343330-22472403-62f7-4221-9752-2e480eda8a52.png)
54+
55+
## Conclusion:
56+
As you can see how the secret key is established between sender and receiver without it being transmitted over the Internet to communicate with each other.
57+
58+
### References
59+
- https://www.cryptomathic.com/news-events/blog/symmetric-key-encryption-why-where-and-how-its-used-in-banking
60+
61+
62+
### Contributed by : [@NikitaEmberi](https://github.com/NikitaEmberi)

0 commit comments

Comments
 (0)