Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/actions/elections_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions app/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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}}
Expand Down Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand Down
33 changes: 20 additions & 13 deletions app/templates/app/elections/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ <h1>{{ election.name }}</h1>
</header>

{% if election.active %}
<p>{{ election.votes.count }} votes</p>
<p>{{ election.votes.count }} votes</p>
{% endif %}

{% if voted %}
<p>You have already voted in this election.</p>
<p><b>Vote details:</b></p>
<p>Hash: {{ receipt.hashed }}</p>
<p>Date: {{ receipt.created }}</p>
<p>You have already voted in this election.</p>
<p><b>Vote details:</b></p>
<p>Hash: {{ receipt.hashed }}</p>
<p>Date: {{ receipt.created }}</p>

{% else %}
<div class="row">

{% if election.active %}

<div class="row">
{% for candidate in election.candidates.all %}
<div class="col-md-4">
<div class="card card-body">
Expand All @@ -26,18 +29,22 @@ <h2 class="text-center">{{ candidate.user.get_username }}</h2>
<p class="mb-0 text-center"><a class="btn btn-primary" href="{% url 'vote' election.id candidate.id %}">VOTE</a></p>
</div>
</div>
</div>
{% endfor %}
</div>

{% endif %}
{% endif %}

{% if not election.active %}
<div class="row">
<div class="col-md-12">
<h2>Election Results</h2>
<p>Decrypted Total: {{ election.decrypted_total }}</p>
<button class="btn btn-primary" hx-get="{% url 'verify_results' election.id %}" hx-target="#results">Verify Results</button>
<div id="results"></div>
</div>
<div class="row">
<div class="col-md-12">
<h2>Election Results</h2>
<p>Decrypted Total: {{ election.decrypted_total }}</p>
<button class="btn btn-primary" hx-get="{% url 'verify_results' election.id %}" hx-target="#results">Verify
Results</button>
<div id="results"></div>
</div>
</div>
{% endif %}
{% endblock %}