def setup_key_distribution(self, receiver_ID, N, receiver_ip): sender_key_10 = np.concatenate( (DES.byte_to_arr(self.key_), np.zeros(2))) receiver_key_10 = np.concatenate( (DES.byte_to_arr(KEYS[receiver_ID]), np.zeros(2))) session_key = random.getrandbits(8) session_key_enc_a = DES.full_encrypt(str(session_key), sender_key_10) receiver_ID_enc_a = DES.full_encrypt(str(receiver_ip), sender_key_10) N_enc_a = DES.full_encrypt(str(N), sender_key_10) session_key_enc_b = DES.full_encrypt(str(session_key), receiver_key_10) sender_id_enc_b = DES.full_encrypt(str(self.id_), receiver_key_10) timestamp = datetime.datetime.now() timestamp_str = timestamp.strftime('%Y-%m-%d %H:%M:%S') timestamp_str_enc_b = DES.full_encrypt(timestamp_str, receiver_key_10) timestamp_str_enc_a = DES.full_encrypt(timestamp_str, sender_key_10) return str(session_key_enc_a) + "," + str(receiver_ID_enc_a) + "," + \ str(N_enc_a) + "," + \ str(timestamp_str_enc_a) + "," + str(session_key_enc_b) + "," + \ str(sender_id_enc_b) + "," + str(timestamp_str_enc_b)
import socket import DES import datetime import numpy as np port = 8888 if __name__ == "__main__": KDC_ip = '127.0.0.1' print(sys.argv) if len(sys.argv) >= 2: KDC_ip = sys.argv[1] u = b"BOB" key = 192 key10 = np.concatenate((DES.byte_to_arr(key), np.zeros(2))) s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((KDC_ip, port)) s.sendall(u) R = s.recv(1024) R = R.decode("utf-8") if R == "200 OK": print("Everyone connected to KDC.") else: raise RuntimeError("ERROR - Not everyone connected to KDC.")
# should have both Alice and Bob connected!!! send 200 OK to start symmetric key distribution. for conn in conn_dict.values(): conn[1].sendall(b"200 OK") fds.append(conn[1]) # wait patiently for a request to make a symmetric session key. readable, writable, exceptional = select.select(fds, [], []) for C in readable: ID = C.recv(1024) ID = ID.decode("utf-8") # ID user is going to request a session key! # beyond the ID, everything is going to be encrypted with # KDC and user's personal key. request_enc = C.recv(1024).decode("utf-8") print(connection.KEYS[ID]) key10 = np.concatenate( (DES.byte_to_arr(connection.KEYS[ID]), np.zeros(2))) print(key10) request = DES.full_decrypt(request_enc, key10) info = request.split(',') C.sendall( str.encode( Alice.setup_key_distribution(info[0], int(info[1]), conn_dict[info[0]][0]))) # session key is sent. my work here is done. s.close()