def receive_aes_crypt(clientsocket, address, decryption_key, show_progress=True): crypt = AESCipher(decryption_key) chunk_count = int(receive_dyn(clientsocket, address)) received_digest = receive_dyn(clientsocket, address) org = chunk_count chunk_size = 1024 decrypted_results = "" byte_rate = "" start_time = datetime.now() if show_progress and chunk_count: print bcolors.OKBLUE + "Progress: " while chunk_count > 0: encrypted_chunk = receive_dyn(clientsocket, address) #print "\"" + encrytpted_chunk + "\"" decrypted_results = decrypted_results + crypt.decrypt(encrypted_chunk) chunk_count = chunk_count - 1 if show_progress: time_elapsed = (datetime.now() - start_time).seconds if time_elapsed > 0: byte_rate = str((float(len(decrypted_results)) / float(time_elapsed)) / 1024.0) update_progress( int(100 * float(org - chunk_count) / float(org)), byte_rate + " Kb/s " if byte_rate else "" ) if show_progress: print bcolors.ENDC calculated_digest = MD5.new(decrypted_results).digest() if calculated_digest == received_digest: return decrypted_results else: raise Exception("AES: data integrity check failed.") return False
def send_aes_crypt(clientsocket, address, data, encryption_key): crypt = AESCipher(encryption_key) chunk_size = 1024 chunk_count = int(math.ceil(len(data) / float(chunk_size))) digest = MD5.new(data).digest() send_dyn(clientsocket, address, str(chunk_count)) send_dyn(clientsocket, address, digest) bytes_encrypted = 0 encrypted_data = "" while bytes_encrypted < len(data): encrypted_chunk = crypt.encrypt(data[bytes_encrypted:min(bytes_encrypted+chunk_size, len(data))]) bytes_encrypted = bytes_encrypted + chunk_size send_dyn(clientsocket, address, encrypted_chunk)