diff --git a/e-voting-system/backend/routes/admin.py b/e-voting-system/backend/routes/admin.py index 4812575..f32f2f1 100644 --- a/e-voting-system/backend/routes/admin.py +++ b/e-voting-system/backend/routes/admin.py @@ -176,6 +176,17 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)): else: logger.info(f"Election {election_id} already has valid public key") + # Ensure public key is base64-encoded for client + pubkey_to_return = election.public_key + if isinstance(pubkey_to_return, bytes): + pubkey_str = pubkey_to_return.decode('utf-8') + # If it's plain "p:g:h" format, encode it to base64 + if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64 + pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii') + else: + # Already base64, just decode to string + pubkey_to_return = pubkey_str + return { "status": "success", "election_id": election_id, @@ -183,7 +194,7 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)): "elgamal_p": election.elgamal_p, "elgamal_g": election.elgamal_g, "public_key_generated": True, - "public_key": election.public_key.decode('ascii') if isinstance(election.public_key, bytes) else election.public_key + "public_key": pubkey_to_return } except HTTPException: diff --git a/e-voting-system/backend/routes/votes.py b/e-voting-system/backend/routes/votes.py index 6bcd533..36f9aab 100644 --- a/e-voting-system/backend/routes/votes.py +++ b/e-voting-system/backend/routes/votes.py @@ -499,18 +499,29 @@ async def setup_election( # Générer les clés ElGamal si nécessaire if not election.public_key: - elgamal = ElGamal() + elgamal = ElGamalEncryption() # Store as base64-encoded bytes (database column is LargeBinary) # public_key_bytes returns UTF-8 "p:g:h", then encode to base64 election.public_key = base64.b64encode(elgamal.public_key_bytes) db.add(election) db.commit() + # Ensure public key is base64-encoded for client + pubkey_to_return = election.public_key + if isinstance(pubkey_to_return, bytes): + pubkey_str = pubkey_to_return.decode('utf-8') + # If it's plain "p:g:h" format, encode it to base64 + if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64 + pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii') + else: + # Already base64, just decode to string + pubkey_to_return = pubkey_str + return { "status": "initialized", "election_id": election_id, "public_keys": { - "elgamal_pubkey": election.public_key.decode('ascii') if isinstance(election.public_key, bytes) else election.public_key + "elgamal_pubkey": pubkey_to_return }, "blockchain_blocks": blockchain.get_block_count() } @@ -546,8 +557,19 @@ async def get_public_keys( detail="Election keys not initialized. Call /setup first." ) + # Ensure public key is base64-encoded for client + pubkey_to_return = election.public_key + if isinstance(pubkey_to_return, bytes): + pubkey_str = pubkey_to_return.decode('utf-8') + # If it's plain "p:g:h" format, encode it to base64 + if ':' in pubkey_str and not pubkey_str.startswith('MjM6'): # Not already base64 + pubkey_to_return = base64.b64encode(pubkey_str.encode('utf-8')).decode('ascii') + else: + # Already base64, just decode to string + pubkey_to_return = pubkey_str + return { - "elgamal_pubkey": election.public_key.decode('ascii') if isinstance(election.public_key, bytes) else election.public_key + "elgamal_pubkey": pubkey_to_return }