def test_e_add(): for i in range(5): priv, pub = paillier.generate_keypair(128) for j in range(5): a = int(random.randint(0, 1000000)) b = int(random.randint(0, 1000000)) ca, cb = paillier.encrypt(pub, a), paillier.encrypt(pub, b) cs = paillier.e_add(pub, ca, cb) s = paillier.decrypt(priv, pub, cs) assert a + b == s
print "Generating keypair..." priv, pub = p.generate_keypair(1024) #generate public and private keys #public key gets given to voters to encrypt their votes csum = p.encrypt( pub, 0 ) #store the encrypted sum of all votes, turn this into an array with slots for each candidate later x = int( input("Enter 1 to vote yes and 0 to vote no: ") ) #have user input their votes somehow and store the encrypted votes in some array cx print "x =", x print "Encrypting x..." cx = p.encrypt(pub, x) print "cx =", cx csum = p.e_add(pub, cx, csum) #add the encrypted votes to the encrypted sum y = int(input("Enter 1 to vote yes and 0 to vote no: ")) print "y =", y print "Encrypting y..." cy = p.encrypt(pub, y) print "cy =", cy csum = p.e_add(pub, cy, csum) print "Computing encrypted total..." print "csum = ", csum print "Decrypting csum..." dsum = p.decrypt( priv, pub, csum ) #decrypt the sum using both keys to find the total votes while preserving privacy
def requestHandler(self, server_socket, address): # Receive query & search parameters from the client client_query_object = receive_object(server_socket) # Info print "Received request for: " + client_query_object.query_string # Start timer start_time = time() query = client_query_object.query_string top_k = client_query_object.top_k # Prepare the hashed query time_1 = time() stemmed_query = stem_text(query) query_terms = list(set(stemmed_query.split())) hashed_query_terms = [sha256(self.salt.encode() + query.encode()).hexdigest() for query in query_terms] time_2 = time() print "Query hashing: " + "{0:.4f}".format(time_2 - time_1) # Ranked result will be stored here sort_index = dict() time_1 = time() # Main logic for fetching ranked result from encrypted index goes here for keyword in hashed_query_terms: if keyword in self.index: keyword_search_index = self.index[keyword] for filename, value in keyword_search_index.iteritems(): # AES decrypt now filename = aes_decrypt(filename, self.aes_key) if filename in sort_index: sort_index[filename] = e_add(self.paillier_keys[1], sort_index[filename], value) else: sort_index[filename] = value time_2 = time() print "Search & Paillier add: " + "{0:.4f}".format(time_2 - time_1) time_1 = time() # Decrypt the sort index for filename, value in sort_index.iteritems(): sort_index[filename] = decrypt(self.paillier_keys[0], self.paillier_keys[1], value) # Sort the final sort_index ranked_result = sorted(sort_index.items(), key=operator.itemgetter(1), reverse=True) ranked_result = [result[0] for result in ranked_result] time_2 = time() print "Decrypt & Sort: " + "{0:.4f}".format(time_2 - time_1) # Get top-k results if top_k == 0: if len(ranked_result) > 170: ranked_result = ranked_result[:170] else: ranked_result = ranked_result[:top_k] # Note end time end_time = time() # Create response to client response = Server_Response(float("{0:.4f}".format(end_time - start_time)), ranked_result) # Send response back to the client send_object(server_socket, response) # Close socket server_socket.close()
def get_candidate_total(self, i, vlist): candidate_votes = [vlist[x][i] for x in range(len(vlist))] return reduce(lambda x, y: p.e_add(self.public, x, y), candidate_votes)
query_terms = list(set(stemmed_query.split())) hashed_query_terms = [ hashpw(query.encode('utf-8'), bcryt_salt) for query in query_terms ] sort_index = dict() for keyword in hashed_query_terms: if keyword in index: keyword_search_index = index[keyword] for filename, value in keyword_search_index.iteritems(): # AES decrypt now filename = aes_decrypt(filename, aes_key) if filename in sort_index: sort_index[filename] = e_add( public_key, sort_index[filename], value) else: sort_index[filename] = value # Decrypt the sort index for filename, value in sort_index.iteritems(): sort_index[filename] = decrypt(private_key, public_key, value) # Sort the final sort_index ranked_result = sorted(sort_index.items(), key=operator.itemgetter(1), reverse=True) # Print for now for filename, score in ranked_result: print filename except Exception as details: