def get_max_index(distances, secret_key): encrypted_index = encrypt_binary(0, PRECISION) encrypted_max_distance = distances[0] for i in range(1, len(distances)): less_than = secure_comparison(encrypted_max_distance, distances[i], secret_key) encrypted_index = secure_multiplexer( encrypted_index, encrypt_binary(i, PRECISION), less_than, secret_key, ) encrypted_max_distance = secure_multiplexer( encrypted_max_distance, distances[i], less_than, secret_key, ) return decrypt_binary(encrypted_index, secret_key)
def get_distances(attrs, coeffs, secret_key): distances = [] for row in coeffs: distance = encrypt_binary(0, PRECISION) for attr, coeff in zip(attrs, row): print('add one term') distance = secure_add(distance, secure_multiply(attr, coeff, secret_key), secret_key) distance = secure_add(distance, row[-1], secret_key) distances.append(distance) print(decrypt_binary(distance, secret_key)) return distances
def generate_keys(request): if request.method == 'POST': PublicKeys.objects.all().delete() p, g, h, sk = generate_distributed_exponential_elgamal_keys( 3, PRIME_LENGTH) public_key = PublicKeys( public_key=str(h), prime=str(p), generator=str(g), secret_keys=';'.join([str(k) for k in sk]), ) public_key.save() Coefficients.objects.all().delete() set_encryption_scheme(ConditionalGate(3, p, g, h)) coefficient_attrs = dict() for attr_name, coeffs in zip(['c1', 'c2', 'c3', 'c4', 'c5'], get_coefficients()): ciphers = [] for c in coeffs: print(c) for cipher in encrypt_binary(c, PRECISION): ciphers.append(str(cipher.c1)) ciphers.append(str(cipher.c2)) coefficient_attrs[attr_name] = ';'.join(ciphers) Coefficients(**coefficient_attrs).save() return render( request, 'ui\\generate_keys.html', { 'public_key': public_key, 'private_key': ';'.join([str(k) for k in sk]), }) try: public_key = PublicKeys.objects.get() except ObjectDoesNotExist: public_key = None return render(request, 'ui\\generate_keys.html', { 'public_key': public_key, })
def get_encrypted_attrs(query_dict): return [encrypt_binary(attr, PRECISION) for attr in get_attrs(query_dict)]
from CryptoLibrary import secure_comparison from CryptoLibrary import secure_multiply from CryptoLibrary import secure_xor from CryptoLibrary import secure_inequality from KeyGenerator import generate_distributed_exponential_elgamal_keys NUMBER_OF_SERVERS = 3 p, g, h, secret_keys = generate_distributed_exponential_elgamal_keys( NUMBER_OF_SERVERS, 128) set_encryption_scheme(ConditionalGate(NUMBER_OF_SERVERS, p, g, h)) # =========================== XOR ============================ # Encrypt binary 1100 x = encrypt_binary(12) # Encrypt binary 1010 y = encrypt_binary(10) # Result should be 6 = binary 110 x_xor_y = secure_xor(x, y, secret_keys) print(decrypt_binary(x_xor_y, secret_keys)) # =========================== ADD ============================= # Encrypt binary 00011100 x = encrypt_binary(28, binary_length=8) # Encrypt binary 00011010 y = encrypt_binary(26, binary_length=8)