Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

import socket 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 s.connect((host, port)) # connect with the server s.send("Hello server!") # communicate with the server with open('received_file', 'wb') as f: while True: print('Receiving data...') data = s.recv(1024) print('data=%s', (data)) if not data: break # write data to a file f.write(data) f.close() print('Successfully obtained file from server') s.close() print('Connection closed') if __name__=='__main__': client()

Server PY:

''' File transfer server code. Not multi-threaded. ''' import socket # Import socket module port = 63000 # Reserve a port for your service (*client must connect with this port number) 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. """ 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) print('Server received', repr(data)) # file to transfer filename='sample.txt' f = open(filename,'rb') l = f.read(1024) while (l): conn.send(l) print('Sent ',repr(l)) l = f.read(1024) f.close() print('Done sending') conn.send('File transfer complete!') conn.close() if __name__=='__main__': serve()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions