def send_hello(self): """ Send initial hello packet to server """ packet = {'type': 'hello'} packet = encode_dict(packet) self.srv_sock.sendall(packet)
def send_ack(self, connection): """ Send an ACK packet to client """ packet = {'type': "ACK"} packet = encode_dict(packet) bytes_sent = connection.send(packet)
def send_pg(self): """ Send over p and g to KDC for DH algorithm """ packet = {'type': 'DH_1', 'from': self.user, 'p': self.p, 'g': self.g} packet = encode_dict(packet) self.kdc_sock.sendall(packet)
def send_ack(self): """ Send an ACK packet to KDC """ packet = {'type': "ACK", 'from': self.user} packet = encode_dict(packet) packet = self.kdc_sock.sendall(packet)
def send_hello(self, conn): """ Send response hello packet to client """ packet = {'type': 'hello'} packet = encode_dict(packet) conn.sendall(packet)
def send_A(self): """ Send over A to KDC for KDC to compute secret_s """ A = (self.g ** self.secret_a) % self.p packet = {'type': "DH_2", 'from': self.user, 'A': A} packet = encode_dict(packet) self.kdc_sock.sendall(packet)
def export_file(self) -> None: save_file_name = self.save_file_dialog( 'Save Files (*.autosave);;All Files (*)') if save_file_name: with open(save_file_name, 'wb') as file: file.write(encode_dict(self.game_save, self.key)) else: self.alert('No path specified!', QMessageBox.Warning)
def send_sym_key(self): """ Generate symmetric key for session and send to server """ self.sym_key = gen_sym_key(168) e_sym_key = self.encrypt_sym_key(self.sym_key) packet = {'type': 'sym_key'} packet['key'] = e_sym_key packet = encode_dict(packet) self.srv_sock.sendall(packet)
def send_suite(self, conn): """ Send selected cipher suite to client """ packet = {'type': 'suite'} packet['sym_key'] = self.alg_handler.sym_key_alg packet['key_exc'] = self.alg_handler.key_exc_alg packet['hash'] = self.alg_handler.hash_alg packet = encode_dict(packet) conn.sendall(packet)
def send_suite(self): """ Send cipher suite to server """ packet = {'type': 'suite'} packet['sym_key'] = ['des'] packet['key_exc'] = ['rsa'] packet['hash'] = ['sha1'] packet = encode_dict(packet) self.srv_sock.sendall(packet)
def send_key_exc(self): """ Generate public keys and send to server """ self.alg_handler.gen_asym_keys() packet = {'type': 'key_exc'} (n, e) = self.alg_handler.get_my_pub_key() packet['public_key_1'] = n packet['public_key_2'] = e packet = encode_dict(packet) self.srv_sock.sendall(packet)
def send_key_exc(self, conn): """ Generate public keys and send to client """ self.alg_handler.gen_asym_keys() packet = {'type': 'key_exc'} (n, e) = self.alg_handler.get_my_pub_key() packet['public_key_1'] = n packet['public_key_2'] = e packet = encode_dict(packet) conn.sendall(packet)
def send_nonce_to_alice(self): self.replay_nonce = gen_nonce() des = ToyDES(self.secret_s) encrypted_nonce = des.encrypt(self.replay_nonce) packet = {'nonce_b_e': encrypted_nonce} packet = encode_dict(packet) self.nonce_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.nonce_sock.connect(self.alice_address_2) self.nonce_sock.sendall(packet) self.nonce_sock.close()
def start_NS(self): nonce_a = gen_nonce() packet = {'user1': "alice", 'user2': "bob", 'nonce_a': nonce_a, 'nonce_b_e': self.nonce_b_e} packet = encode_dict(packet) self.kdc_connection.sendall(packet) encrypted_packet = self.kdc_connection.recv(1024) encrypted_packet = encrypted_packet.decode('UTF-8') des = ToyDES(self.secret_s) decrypted_packet = "" for byte in encrypted_packet: decrypted_packet += chr(des.decrypt(ord(byte))) decrypted_packet = json.loads(decrypted_packet) self.k_ab = decrypted_packet['k_ab'] self.k_ab_e = decrypted_packet['k_ab_e'] self.nonce_b_e = decrypted_packet['nonce_b_e']
def send_msg(self, msg): """ Compress, hash, and encrypt msg. Then, send to server. """ msg_bytes = msg.encode() compressed = gzip.compress(msg_bytes) HMAC = compute_HMAC(self.sym_key, compressed, self.alg_handler) compressed_num = int.from_bytes(compressed, byteorder='big') encrypted = self.alg_handler.sym_encrypt(self.sym_key, compressed_num) packet = {'type': msg} packet['msg'] = encrypted packet['num_bytes'] = len(compressed) packet['MAC'] = HMAC packet = encode_dict(packet) self.srv_sock.sendall(packet)
def send_B(self, connection): g = None p = None secret_b = None if connection == self.dh_dict['alice']['connection']: g = self.dh_dict['alice']['g'] p = self.dh_dict['alice']['p'] secret_b = self.dh_dict['alice']['secret_b'] elif connection == self.dh_dict['bob']['connection']: g = self.dh_dict['bob']['g'] p = self.dh_dict['bob']['p'] secret_b = self.dh_dict['bob']['secret_b'] else: print("UH OH") B = (g**secret_b) % p packet = {'B': B} packet = encode_dict(packet) connection.sendall(packet)
def join_client(self, msg): """ Alice will connect on Bob's open socket """ self.bob_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.bob_sock.connect(self.bob_address_2) packet = {'k_ab_e': self.k_ab_e, 'nonce_b_e': self.nonce_b_e} packet = encode_dict(packet) self.bob_sock.sendall(packet) packet = self.bob_sock.recv(1024) des = ToyDES(self.k_ab) msg_to_send = "" for byte in msg: encrypted_byte = chr(des.encrypt(ord(byte))) msg_to_send += encrypted_byte msg_to_send = msg_to_send.encode('UTF-8') self.bob_sock.sendall(msg_to_send)
def host_client(self): """ Bob will host Alice's connection """ self.alice_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.alice_sock.bind(self.bob_address_2) self.alice_sock.listen(1) print("Waiting for Alice at", self.bob_address_2) self.alice_connection, self.alice_address = self.alice_sock.accept() print("Connected to Alice") packet = self.alice_connection.recv(1024) packet = decode_dict(packet) k_ab_e = packet['k_ab_e'] nonce_b_e = packet['nonce_b_e'] des = ToyDES(self.secret_s) nonce_b_e = des.decrypt(nonce_b_e) if nonce_b_e == self.replay_nonce: print("Replay attack prevented; nonces match!") else: print("Replay attack possible; nonces do not match!") k_ab = des.decrypt(k_ab_e) packet = {'type': "ACK"} packet = encode_dict(packet) self.alice_connection.sendall(packet) packet = self.alice_connection.recv(1024) packet = packet.decode('UTF-8') des.set_key(k_ab) decrypted_msg = "" for byte in packet: decrypted_msg += chr(des.decrypt(ord(byte))) print("Recv'd from alice: ", decrypted_msg)
def replace_file(self) -> None: if self.import_path: with open(self.import_path, 'wb') as file: file.write(encode_dict(self.game_save, self.key)) else: self.alert('No path specified!', QMessageBox.Warning)
def send_ACK(self, conn): packet = {'type': 'ACK'} packet = encode_dict(packet) packet = conn.sendall(packet) print("Handshake complete")