Implement DH Key exchange algorithm for the simple client-server communication a
ID: 3749790 • Letter: I
Question
Implement DH Key exchange algorithm for the simple client-server communication as implemented in the provided files server.py & client.py. The current code demonstrates the sending of a file from the server to the client and sets up the appropriate socket connections. The students will NOT need to create the connections, they cam piggy back on the connections created and achieve the public key exchange for DH algorithm. Once the shared key has been established the students should encrypt the file being transferred from the server to the client and transfer the encrypted file. On the client side the file should be decrypted using the same shared key. Finally the server should also send the hash of the encrypted file and the client should verify that it received the same file by hash comparison. Note that Python provides functions to compute the hash of inputs. The students should use a variation of the SHA or MD5 hash as implemented in Python.
Client PY:
Server PY:
Explanation / Answer
I have modified your code to fullfill all the above mentioned requrements.
1. Securly send encryption and decryption key using DH_Algorithim
2. Encryption and Decrytion of file.
3. Hashing of file data to match the content
I have commented all the modified code with Modified comment.
Server.py
'''
File transfer server code. Not multi-threaded.
'''
import socket # Import socket module
import struct
from Crypto.Cipher import DES
import hashlib
# Reserve a port for your service (*client must connect with this port number)
port = 63000
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection and listen for requests
def serve():
"""
The server code. Accepts connections from clients and transfers a file.
The file should be in the same directory. Ensure that there is only
one client connecting. This is not multi-threaded at this point.
"""
# ----------Modified-------------------------------------
# Creating public modulous known to client and server
p = 23
# Creating secret key known to server only
secret_key = 3
# Creating public base known to client and server
g = 5
# Getting the key which server will share with client
sharing_key = (g ** secret_key) % p
# ----------------------------------------------------------
print('Server listening....')
while True:
conn, addr = s.accept() # Establish connection with client, returns a tuple
print('Got connection from', addr)
data = conn.recv(1024)
# ----------Modified------------------------------------
# Server will send its sharing key to server
conn.send(bytes(str(sharing_key) + '', encoding='UTF8'))
# Server will recieve key from server
recieved_key = int(conn.recv(1024))
# Server will generate a common key using its secret key and public modulous
common_key = (recieved_key ** secret_key) % p
print('CommonKey:', common_key)
# Creating object for encrypting file
# Common key with a random string is used to generate a
# strong password
obj = DES.new(str(common_key) + "123456",
DES.MODE_CFB, "12345678")
# ----------------------------------------------------
print('Server received', repr(data))
# file to transfer
filename = './sample.txt'
f = open(filename, 'rb')
l = f.read(1024)
while (l):
# ----------Modified------------------------------------
# Genrating hash of file
hashobj = hashlib.md5(l)
# Server will send encrpted file to client
conn.send(obj.encrypt(l))
# Server will send hash of file to client
conn.send(bytes(str(hashobj.hexdigest()), encoding='UTF8'))
# ----------------------------------------------------
print('Sent ', repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send(bytes('File transfer complete!', encoding='UTF8'))
conn.close()
if __name__ == '__main__':
serve()
Client.py
import socket
import struct
from Crypto.Cipher import DES
import hashlib
def client():
"""
Client code for connecting to server and receiving file from server.
Right now we assume both work on localhost.
"""
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 63000 # Make sure that client pings server on correct port
# ----------Modified------------------------------------
# Creating public modulous known to client and server
p = 23
# Creating secret key known to client only
secret_key = 4
# Creating public base known to client and server
g = 5
# Getting the key which client will share with server
sharing_key = (g ** secret_key) % p
# ----------------------------------------------------
s.connect((host, port)) # connect with the server
# communicate with the server
s.send(bytes("Hello server!", encoding='UTF8'))
# ----------Modified------------------------------------
# Client will recieve key from server
recieved_key = int(s.recv(1024))
# Client will generate a common key using its secret key and public modulous
common_key = (recieved_key ** secret_key) % p
print('CommonKey:', common_key)
# Client will send its sharing key to server
s.send(bytes(str(sharing_key) + '', encoding='UTF8'))
# Creating object for decrypting file using
# Common key with a random string is used to generate a
# strong password
obj = DES.new(str(common_key) + "123456",
DES.MODE_CFB, "12345678")
# ----------------------------------------------------
with open('./client.txt', 'wb') as f:
while True:
print('Receiving data...')
# ----------Modified------------------------------------
# Client will recieve the encrypted file data
data = s.recv(1024)
# Client will recieve the hash of file
recieved_hash = s.recv(4096)
# Client will decrypt the data and hash it
data_hash = hashlib.md5(obj.decrypt(data)).hexdigest()
# ----------------------------------------------------
if not data:
break
# ----------Modified------------------------------------
# Client will match recieved hash and generated hash
# If both hashes match, data will be saved to file.
if (data_hash == str(recieved_hash, encoding='UTF8')):
# Client will
print('Mathing hash successfully.')
# write data to a file
f.write(obj.decrypt(data))
else:
print('Error: Mathing hashes')
# ----------------------------------------------------
f.close()
print('Successfully obtained file from server')
s.close()
print('Connection closed')
if __name__ == '__main__':
client()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.