def retrieval(exactkw, trapdoor_set, key): results = [] with open('D:\\Fuzzy Keywords Search\\encrypted_index.csv', 'r') as f: reader = csv.DictReader(f) for item in reader: kw = AES.decrypt(item['Keyword'], key) if kw == exactkw: de_fid = AES.decrypt(item['FIDS'], key) de_fid = de_fid.split('\x00')[0] de_fid = de_fid.split('|') for i in de_fid: if i != 'NULL': results.append(i) break else: ba = bitarray(item['BloomFilter']) fliter = BloomFilter(capacity=100) fliter.bitarray = ba flag = False for i in trapdoor_set: if i in fliter: flag = True if flag: de_fid = AES.decrypt(item['FIDS'], key) de_fid = de_fid.split('\x00')[0] de_fid = de_fid.split('|') for i in de_fid: if i != 'NULL': results.append(i) if results: # 列表结果去重 temp_list = [] # 定义一个临时空列表,用于保存临时数据。 for i in results: # 遍历原列表,判断如果元素不在临时列表,就追加进去,如果在,就不加。 if i not in temp_list: temp_list.append(i) return temp_list else: return '检索失败'
def search(client_socket): while True: # 接受检索的关键字及陷门集合 exact_kw = client_socket.recv(1024).decode('utf-8') if exact_kw == 'exit': break trapdoor_string = client_socket.recv(1024 * 1024).decode("utf-8") trapdoor_list = trapdoor_string.split(' ') # 进行关键字检索 print("检索中......") results = [] with open('D:\\Fuzzy Keywords Search\\Sever\\encrypted_index.csv', 'r') as f: reader = csv.DictReader(f) for item in reader: if item['Keyword'] == exact_kw: print('关键字检索成功...') results.append(item['FIDS']) break else: ba = bitarray(item['BloomFilter']) fliter = BloomFilter(capacity=100) fliter.bitarray = ba flag = False for i in trapdoor_list: if i in fliter: flag = True if flag: results.append(item['FIDS']) if not results: print('检索失败') client_socket.send('wrong'.encode()) continue else: results_string = ' '.join(results) client_socket.send(results_string.encode()) print('关键字检索成功...')