From d265ce91d3b199e0a8f35dd4ac0a9958a2bb837a Mon Sep 17 00:00:00 2001 From: Qudsia tariw Date: Tue, 25 Feb 2025 13:23:55 +0500 Subject: [PATCH] removed randomness. fixed a bug with receipt view after elections end --- app/actions/elections_actions.py | 2 +- app/encryption.py | 20 +++++------- app/templates/app/elections/detail.html | 42 ++++++++++++++----------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/app/actions/elections_actions.py b/app/actions/elections_actions.py index 58efb75..55a2884 100644 --- a/app/actions/elections_actions.py +++ b/app/actions/elections_actions.py @@ -59,7 +59,7 @@ def end_election(modeladmin, request, queryset): for i in range(len(votes)): ballot = json.loads(votes[i].ballot) for j in range(len(ballot)): - json_str_positive = json.dumps({'ciphertext': ballot[j], 'randomness': None}) + json_str_positive = json.dumps({'ciphertext': ballot[j]}) ct_temp_positive = Ciphertext.from_json(json_str_positive) if i == 0: encrypted_positive_total[j] = ct_temp_positive diff --git a/app/encryption.py b/app/encryption.py index 05581b2..25c493b 100644 --- a/app/encryption.py +++ b/app/encryption.py @@ -4,29 +4,26 @@ import json class Ciphertext: - def __init__(self, ciphertext: int, randomness: int = None): + def __init__(self, ciphertext: int): """ Initialize a Ciphertext object. :param ciphertext: The encrypted value - :param randomness: The randomness used in encryption (optional) """ self.ciphertext = ciphertext - self.randomness = randomness def __repr__(self): """ Provide a string representation of the CipherData object for debugging. """ - return f"CipherData(ciphertext='{self.ciphertext}', randomness='{self.randomness}')" + return f"CipherData(ciphertext='{self.ciphertext}')" def to_json(self): """ Convert the ciphertext object to JSON format. """ return json.dumps({ - 'ciphertext': str(self.ciphertext), - 'randomness': str(self.randomness) if self.randomness else None + 'ciphertext': str(self.ciphertext) }) @classmethod @@ -35,7 +32,7 @@ def from_json(cls, json_str): Create a Ciphertext object from JSON string. """ data = json.loads(json_str) - return cls(int(data['ciphertext']), int(data['randomness']) if data['randomness'] else None) + return cls(int(data['ciphertext'])) class Encryption: def __init__(self, public_key: str = None, private_key: str = None): @@ -46,7 +43,7 @@ def __init__(self, public_key: str = None, private_key: str = None): :param private_key: The private key phi value """ if public_key is None and private_key is None: - self.paillier = Paillier() + self.paillier = Paillier(key_size=2048) elif public_key is not None and private_key is None: public_key_g, public_key_n = map(int, public_key.split(',')) keys = {"public_key": {"g": public_key_g, "n": public_key_n}} @@ -77,7 +74,7 @@ def encrypt(self, plaintext: int, rand: int = None) -> Ciphertext: if rand is None: rand = self.generate_random_key() ct = self.paillier.encrypt(plaintext, rand) - return Ciphertext(ct, rand) + return Ciphertext(ct) def add(self, ct1: Ciphertext, ct2: Ciphertext) -> Ciphertext: """ @@ -88,10 +85,7 @@ def add(self, ct1: Ciphertext, ct2: Ciphertext) -> Ciphertext: :return: Ciphertext object containing encrypted sum """ sum_ct = self.paillier.add(ct1.ciphertext, ct2.ciphertext) - combined_randomness = None - if ct1.randomness and ct2.randomness: - combined_randomness = (ct1.randomness * ct2.randomness) % self.paillier.ciphertext_modulo - return Ciphertext(sum_ct, combined_randomness) + return Ciphertext(sum_ct) def decrypt(self, ct: Ciphertext) -> int: """ diff --git a/app/templates/app/elections/detail.html b/app/templates/app/elections/detail.html index f2f0297..e53f840 100644 --- a/app/templates/app/elections/detail.html +++ b/app/templates/app/elections/detail.html @@ -7,36 +7,42 @@

{{ election.name }}

{% if election.active %} -

{{ election.votes.count }} votes

+

{{ election.votes.count }} votes

{% endif %} {% if voted %} -

You have already voted in this election.

-

Vote details:

-

Hash: {{ receipt.hashed }}

-

Date: {{ receipt.created }}

+

You have already voted in this election.

+

Vote details:

+

Hash: {{ receipt.hashed }}

+

Date: {{ receipt.created }}

{% else %} -
+ +{% if election.active %} + +
{% for candidate in election.candidates.all %} -
-
-

{{ candidate.user.get_username }}

-

VOTE

-
+
+
+

{{ candidate.user.get_username }}

+

VOTE

+
{% endfor %}
{% endif %} +{% endif %} + {% if not election.active %} -
-
-

Election Results

-

Decrypted Total: {{ election.decrypted_total }}

- -
-
+
+
+

Election Results

+

Decrypted Total: {{ election.decrypted_total }}

+ +
+
{% endif %} {% endblock %} \ No newline at end of file