def __init__(self, myconfig,connectionBefore, connectionAfter): self.address, self.port = self.config_construct(myconfig) self.IPfailed = False self.tcpSerSocket = self.creatMySockets() self.connectionBefore=connectionBefore self.connectionAfter=connectionAfter self.key_id_key = {} self.diffkey=diff.DiffieHellman() self.aeskey='' t = threading.Thread(target=self.listen, args=(self,)) t.start()
def __init__(self): # Generate local keys` self._local_keys = diffiehellman.DiffieHellman(group=1, key_length=KEY_LENGTH) self._local_keys.generate_private_key() self._local_keys.generate_public_key()
class RequestHandler(BaseHTTPRequestHandler): '''Static keys''' keys = dh.DiffieHellman(group=1, key_length=KEY_LENGTH) keys.generate_private_key() keys.generate_public_key() publicKey = keys.public_key def __init__(self, request, client_address, server) -> None: super().__init__(request, client_address, server) def _set_200_headers(self, keyword, value): self.send_response(200) # self.send_header('Content-type', 'application/json') self.send_header(keyword, value) self.end_headers() def _set_404_headers(self): self.send_response(404) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): parse = urlparse(self.path) request_path = parse.path query = parse.query key_byte_array = self.publicKey.to_bytes((self.publicKey.bit_length() + 7) // 8, 'big') b64_key = base64.standard_b64encode(key_byte_array).decode() if (request_path == "/spotzc") and (query == "action=getInfo" or query == "action=getInfo&version=2.7.1"): self._set_200_headers('Content-type', 'application/json') self.wfile.write(json.dumps( { "status": 101, "statusString": "ERROR-OK", "spotifyError": 0, "version": VERSION_STRING, "deviceID": DEVICE_ID, "remoteName": REMOTE_NAME_DEFAULT, "activeUser": "", "publicKey": b64_key, "deviceType": "SPEAKER", "libraryVersion": "0.1.0", "accountReq": "PREMIUM", "brandDisplayName": "librespot", "modelDisplayName": "librespot" }).encode()) else: self._set_404_headers() # POST echoes the message adding a JSON field def do_POST(self): content_length = int(self.headers['Content-Length']) # <--- Gets the size of data data_bytes = self.rfile.read(content_length) # <--- Gets the data itself data_str = data_bytes.decode('utf-8') path = self.path headers = self.headers var = parse_qs(data_str) print("Var: ", var) if path != "/spotzc" or 'action' not in var or 'addUser' not in var['action']: self._set_404_headers() return user_name = var['userName'][0].encode() blob_bytes = var['blob'][0].encode() client_key = var['clientKey'][0].encode() device_name = var['deviceName'][0] response = json.dumps( { "status": 101, "spotifyError": 0, "statusString": "ERROR-OK" }).encode() self._set_200_headers('Content-Length', str(len(response))) self.wfile.write(response) blob = Blob() login = blob.decrypt(self.keys, blob_bytes, client_key, user_name, DEVICE_ID) self.server.notify_listener(blob) with open('credentials/' + device_name + '.dat', 'w') as f: f.write(text_format.MessageToString(login)) f.close() '''
def distributeKey(self): Topology_1 = Topology.Topology() Topology_1.config_construct('config/topology.ini') G1 = nx.DiGraph() G1.add_edge(1, 2, weight=Topology_1.costs.get(('172.16.1.1', '172.16.2.1'))) G1.add_edge(2, 5, weight=Topology_1.costs.get(('172.16.2.1', '172.16.5.3'))) G1.add_edge(2, 6, weight=Topology_1.costs.get(('172.16.2.1', '172.16.6.4'))) G1.add_edge(2, 7, weight=Topology_1.costs.get(('172.16.2.1', '172.16.7.5'))) G1.add_edge(5, 3, weight=Topology_1.costs.get(('172.16.5.3', '172.16.3.2'))) G1.add_edge(5, 4, weight=Topology_1.costs.get(('172.16.5.3', '172.16.4.2'))) G1.add_edge(7, 3, weight=Topology_1.costs.get(('172.16.7.5', '172.16.3.2'))) G1.add_edge(7, 5, weight=Topology_1.costs.get(('172.16.7.5', '172.16.5.3'))) G1.add_edge(6, 4, weight=Topology_1.costs.get(('172.16.6.4', '172.16.4.2'))) G1.add_edge(4, 8, weight=Topology_1.costs.get(('172.16.4.2', '172.16.8.6'))) G1.add_edge(3, 4, weight=Topology_1.costs.get(('172.16.3.2', '172.16.4.2'))) self.path = self.dijkstra(G1, 1, 8) self.textout.insert(END, 'Random path to Bob: ' + str(self.path) + '\n') pathLength = len(self.path) self.keyList = [] #list of AES keys self.keyIDList = [] #list of keyIDs self.keyIDReceivedList = [ ] # indicates the receiving status of each key reply for idx in range(pathLength - 1): key_id = random.randint(1, 1000000) NewKey = diff.DiffieHellman(key_id=key_id) NewKey.generate_private_key() NewKey.generate_public_key() self.keyList.append(NewKey) self.keyIDList.append(key_id) self.keyIDReceivedList.append(0) destination = self.iplist[self.path[idx + 1] - 1] # get destination ip msgGenerator = messageclass.message() keyInitMsg = msgGenerator.create_KeyInit(destination, key_id, NewKey.generator, NewKey.prime, NewKey.public_key) self.keyMessageQueue.put(keyInitMsg) if idx == 0: self.sendKeyInitMsg()