#client side code import sha1code #import the code for sha1 algorithm import socket clientsocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) #create a unix domain, stream type client side socket #get hostname of local machine #host = socket.gethostname() host = '127.0.0.1' #localhost #assign port no. port = 12345 clientsocket.connect((host, port)) #connect to localhost,port no. clientsocket.send("thisisaplaintext") # send message to be encrypted to server message_from_server = clientsocket.recv(1024) #get message digest from server message_digest = sha1code.sha1( "thisisaplaintext") #compute message digest locally if (message_digest == message_from_server): print("success") else: print("failure")
#server side code import socket import sha1code #create a server side socket object serversocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM) #create a unix domain stream socket #get the hostname of local machine hostname= '127.0.0.1' print hostname port = 12345 #port to which server will be listening #bind the socket to port serversocket.bind((hostname, port)) #bind serverside socket to host and port #set max no. of connections to listen serversocket.listen(10) while True: #establish a connection by accepting connection from client clientsocket,addr =serversocket.accept() #clientsocket is socket connected with client , addr corresponds to address of client print("successfully accepted connection from %s" % str(addr)) #print address of client from whom connection is recieved #receive 1024 bytes of message from client plain_text =clientsocket.recv(1024) message_digest=sha1code.sha1(plain_text) #get message digest from sha1 algorithm clientsocket.send(message_digest) #send back message digest clientsocket.close() #close client side socket
#client side code import sha1code #import the code for sha1 algorithm import socket clientsocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) #create a unix domain, stream type client side socket #get hostname of local machine #host = socket.gethostname() host = 'localhost' #localhost #assign port no. port = 12345 clientsocket.connect((host, port)) #connect to localhost,port no. msg = raw_input("Enter message: ") message_digest = sha1code.sha1(msg) print "Message Digest Initial: ", message_digest clientsocket.send(msg) # send message to be encrypted to server message_from_server = clientsocket.recv(1024) #get message digest from server #compute message digest locally newmd = sha1code.sha1(message_from_server) print "Message Digest Final: ", newmd if (message_digest == newmd): print("success") else: print("failure")