Example #1
0
# Here is the length of my message
L = len(message_to_be_sent_serialized)
sL = str(L) + ' ' * (10 - len(str(L)))  #pad len to 10 bytes

# I first send the length of the message to the server
client.sendall((sL).encode())
print("Sending the context and ciphertext to the server....")
# Now I send the message to the server
client.sendall(message_to_be_sent_serialized)
print("Waiting for the servers's answer...")

# Here is the answer obtained from the server
answer = b""
while True:
    data = client.recv(4096)
    if not data: break
    answer += data

# Here is the decryption of the answer
deserialized_ciphertext = pickle.loads(answer)
verdict = ts.bfv_vector_from(private_context,
                             deserialized_ciphertext).decrypt()
if (verdict == [0]):
    print("My password is in the database.")
else:
    print("My password is not in the database.")

print("Disconnecting...")
client.close()
def recreate_bfv(vec):
    vec_proto = vec.serialize()
    return ts.bfv_vector_from(vec.context(), vec_proto)
    conn, addr = serv.accept()
    L = conn.recv(10).decode().strip()
    L = int(L, 10)

    # Getting bytes of context and encrypted query
    final_data = b""
    while len(final_data) < L:
        data = conn.recv(4096)
        if not data: break
        final_data += data
    deserialized_message = pickle.loads(final_data)

    # Here we recover the context and ciphertext received from the client
    context = ts.context_from(deserialized_message[0])
    ciphertext = deserialized_message[1]
    ct = ts.bfv_vector_from(context, ciphertext)

    # Evaluate the database polynomial at the ciphertext received from the client
    response = ct - [new_database[0]]
    for i in range(1, len_database):
        factor = ct - [new_database[i]]
        response = response * factor

    # Prepare the answer to be sent to the client
    response_serialized = response.serialize()
    response_to_be_sent = pickle.dumps(response_serialized, protocol=None)
    conn.sendall(response_to_be_sent)

    # Close the connection
    conn.close()
    print("Client disconnected")