def messageReceivingService(self): while True: received = self.receiveData() print "[Incoming] received raw message: " + received # special message for exchanging Diffie Hellman keys if(received[0] == MSG_TYPE_DH): partial_session_key_received = int(received[1:]) if(self.mode == 'client'): partial_session_key_generated = self.dh.partialSessionKeyGen()[0] self.socket.sendall(MSG_TYPE_DH + str(partial_session_key_generated) + TERMINATORS) self.total_session_key = self.dh.computeTotalSessionKey(partial_session_key_received) self.key_estabilshment_inprogress = False # regular messages sent by user else: received = received[1:] # HMAC is first 16 bytes (32 hex digits) receivedHmac = received[:32] ciphertext = received[32:] print "[Incoming] hmac received: "+ receivedHmac print "[Incoming] ciphertext received: " + ciphertext plaintext = CBC.decrypt(self.cipher, ciphertext) print "[Incoming] plaintext decrypted: " + plaintext self.console.text = self.console.text + "\nReceived: " + plaintext generatedHmac = hmac_gen.genHmac(self.shared_secret_hash, plaintext) print "[Incoming] hmac to compare with: " + generatedHmac if(receivedHmac != generatedHmac): print "ERROR: Message integrity compromised!"
def sendData(self, obj): plaintext = self.data_to_send.text ciphertext = CBC.encrypt(self.cipher, plaintext) self.console.text = self.console.text + '\n' + 'Text to be sent: ' + self.data_to_send.text hmacVal = hmac_gen.genHmac(self.shared_secret_hash, plaintext) print "[Outgoing] encrypted ciphertext to send: " + ciphertext print "[Outgoing] hmac value " + hmacVal self.socket.sendall(MSG_TYPE_REGULAR + hmacVal + ciphertext + TERMINATORS) self.data_to_send.text = ''
def messageReceivingService(self): while True: received = self.receiveData() print "[Incoming] received raw message: " + received # special message for exchanging Diffie Hellman keys if (received[0] == MSG_TYPE_DH): partial_session_key_received = int(received[1:]) if (self.mode == 'client'): partial_session_key_generated = self.dh.partialSessionKeyGen( )[0] self.socket.sendall(MSG_TYPE_DH + str(partial_session_key_generated) + TERMINATORS) self.total_session_key = self.dh.computeTotalSessionKey( partial_session_key_received) self.key_estabilshment_inprogress = False # regular messages sent by user else: received = received[1:] # HMAC is first 16 bytes (32 hex digits) receivedHmac = received[:32] ciphertext = received[32:] print "[Incoming] hmac received: " + receivedHmac print "[Incoming] ciphertext received: " + ciphertext plaintext = CBC.decrypt(self.cipher, ciphertext) print "[Incoming] plaintext decrypted: " + plaintext self.console.text = self.console.text + "\nReceived: " + plaintext generatedHmac = hmac_gen.genHmac(self.shared_secret_hash, plaintext) print "[Incoming] hmac to compare with: " + generatedHmac if (receivedHmac != generatedHmac): print "ERROR: Message integrity compromised!"