Exemplo n.º 1
0
def test_decrypt():
    for i in range(5):
        priv, pub = paillier.generate_keypair(64)
        for j in range(5):
            pt = int(random.randint(0, 1000000))
            ct = paillier.encrypt(pub, pt)
            assert pt == paillier.decrypt(priv, pub, ct)
Exemplo n.º 2
0
def test_e_mul_const():
    for i in range(5):
        priv, pub = paillier.generate_keypair(128)
        for j in range(5):
            a = int(random.randint(0, 1000000))
            c = paillier.encrypt(pub, a)
            for n in range(0, 11):
                cs = paillier.e_mul_const(pub, c, n)
                s = paillier.decrypt(priv, pub, cs)
                assert a * n == s
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
print "vote total =", dsum

# THE PROCESS
# Voter registers to vote, gets given public key by EB, encrypts vote with EB key then that with own key
# encrypts his votes with the EB's public paillier key and sends it to the EB
# id also gets sent over with just ssh encryption or whatever
# EB verifies that the id is registered and signs last slot of vote array with "verifiedxxxxx" or whatever, some hash value or something based on the id
# this gets sent back to voter, who decrypts with his own key
# zero knowledge proof with BB, make sure vote is 1 or 0
# encrypted votes get sent to the BB
# BB checks to make sure your votes are verified by the EB
# check for duplicates, reject if duplicate
# once all votes are in/voting period is done, send all encrypted votes to the counting authority
# counting authority basically just gets encrypted votes and outputs encrypted sums to the EB
    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()
Exemplo n.º 6
0
 def arr_decrypt(self, vlist):
     return [p.decrypt(self.private, self.public, x) for x in vlist]
            ]
            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:
        print "Server shutting down..."
        print details