def DH(self, client, cipher): print '\n############### STARTING D-H ##################' myDH = DiffieHellman() # Receive value from client recvDH = client.recv(1024).strip() # Decrypt received value from client publicVal = cipher.decrypt(recvDH) print 'Received encrypted value: ', recvDH.encode('hex') print '\n' print 'g^a mod p value is: ', publicVal print '\n' # Encrypt DH's public key AES using shared cipher sendDH = cipher.encrypt(str(myDH.public_key)) print 'g^b mod p value is: ', myDH.public_key print '\n' print 'Sending encrypted value: ', sendDH.encode('hex') print '\n' # Send computed DH to client client.send(str(sendDH)) # Compute shared key myDH.calc_shared_key(long(publicVal)) print 'Calculated session key:', myDH.key self.sessionKey = myDH.key print '################## D-H OVER ###################\n'
def DH(self, sharedKey): print '\n############### STARTING D-H ##################' # Create AES object with shared key cipher = AESCipher(sharedKey) #generate key to send to server myDiffieHellman = DiffieHellman() print 'g^a mod p value is: ', myDiffieHellman.public_key print '\n' #send key to server sendDH = cipher.encrypt(str(myDiffieHellman.public_key)) print 'Sending encrypted value: ', sendDH.encode('hex') self.send(str(sendDH)) print '\n' recvDH = self.waitToRec() #decrypt received DH value reply = cipher.decrypt(recvDH) print 'Received encrypted value: ', recvDH.encode('hex') print '\n' print 'g^b mod p value is: ', reply print '\n' #calculate session key myDiffieHellman.calc_shared_key(long(reply)) print "Calculated session key:", myDiffieHellman.key self.sessionKey = myDiffieHellman.key print '################## D-H OVER ###################\n'