def decrypt_data(self, data: str): json_str = AESUtil(self.key).decryptByECB(data) try: return json.loads(json_str) except Exception as e: print('convert json error {}'.format(e)) return None
def action_transform(self, data: dict): encrypt_data = data['data']['data'] nonce = data['data']['nonce'] random_key = data['data']['key'] real_key = RSAUtil().decrypt(self.pri_key, random_key) real_message = AESUtil(real_key).decryptByCTR(encrypt_data, nonce) print('[{} online]{} >>{}'.format(data['online'], data['from'], real_message))
def decrypt_data(self, data: str): if data: json_str = AESUtil(key).decryptByECB(data) try: return json.loads(json_str) except Exception as e: print('Json loads with exception {}'.format(e)) return None else: print('The data is None when decrypt') return None
def start(self): conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((self.connect_domain, self.connect_port)) self.conn = conn print('Connect succeed...') t = threading.Thread(target=self.read_thread, args=(conn, )) t.start() self.send_handle(conn) while True: input_result = input() if input_result: if input_result == "\q": data = {"action": "quit"} else: if self.pub_key: random_key = get_random_bytes(16) encrypt_key = RSAUtil().encrypt( self.pub_key, random_key) encrypt_content = AESUtil(random_key).encryptByCTR( input_result) encrypt_content['key'] = encrypt_key data = {"action": "transform", "data": encrypt_content} else: print( 'public key is None could not send message to server' ) data = None if data: json_str = json.dumps(data) encrypt_str = AESUtil(self.key).encryptByECB(json_str) try: conn.send(bytes(encrypt_str + "\n", "utf-8")) # quit the client if input_result == "\q": break except Exception as e: print('send data for server error >>{}'.format(e)) break conn.close() os._exit(0)
def _send_keys(self): keys = key_pool[self.channel] if keys: json_str = json.dumps({"action": "handle", "data": keys}) encrypt_data = AESUtil(key).encryptByECB(json_str) try: self.wfile.write(bytes(encrypt_data, 'utf8')) except Exception as e: print('send ras keys failure {}'.format(e)) self.finish() else: genarate_keys = RSAUtil().createKey() key_pool[self.channel] = genarate_keys self._send_keys()
def action_transform(self, data: dict): message = data['data'] if self.channel: self._send_all_by_channel( AESUtil(key).encryptByECB( json.dumps({ "action": "transform", "data": message, "from": "{}".format(self.client_address), "online": "{}".format(len(client_pool[self.channel])) })))
def action_handle(self, data: dict): channel = data['channel'] if channel: # append client to client pool self.channel = channel client_pool[channel].append(self) # send ras key for client self._send_keys() # notify other client the new client joined self._send_all_by_channel( AESUtil(key).encryptByECB( json.dumps({ "action": "notify", "data": "{} join the chatroom!!!".format(self.client_address) })))
def finish(self): print('{} client is closed!'.format(self.client_address)) if hasattr(self, "channel") and self.channel: # remove client from client pool if it exist if self in client_pool[self.channel]: client_pool[self.channel].remove(self) # notify leave message self._send_all_by_channel( AESUtil(key).encryptByECB( json.dumps({ "action": "notify", "data": "{} leave the chatroom.".format(self.client_address) }))) #remove ras key if the channel do not have any client if len(client_pool[self.channel]) == 0: del key_pool[self.channel] print('The rsa key for channel {} has been removed.'.format( self.channel))
def send_handle(self, con): data = {"action": "handle", "channel": self.channel} json_str = json.dumps(data) encrypt_str = AESUtil(self.key).encryptByECB(json_str) con.send(bytes(encrypt_str + "\n", "utf-8"))