Compare commits

..

No commits in common. "78f0140342e496e1e30e4f1c51461c096d29b915" and "41e7fc08ed2d4fd9cdd66a30e8648d65980d786c" have entirely different histories.

2 changed files with 7 additions and 44 deletions

View File

@ -155,11 +155,7 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)):
pubkey_is_invalid = False
if election.public_key:
try:
# Public key is stored as base64-encoded bytes, try to decode it
pubkey_b64_str = election.public_key.decode('ascii') if isinstance(election.public_key, bytes) else str(election.public_key)
# Try to decode the base64 to verify it's valid
pubkey_bytes = base64.b64decode(pubkey_b64_str)
pubkey_str = pubkey_bytes.decode('utf-8')
pubkey_str = election.public_key.decode('utf-8') if isinstance(election.public_key, bytes) else str(election.public_key)
# Check if it's valid (should be "p:g:h" format, not "pk_ongoing_X")
if not ':' in pubkey_str or pubkey_str.startswith('pk_') or pubkey_str.startswith('b\''):
pubkey_is_invalid = True
@ -169,24 +165,13 @@ async def init_election_keys(election_id: int, db: Session = Depends(get_db)):
if not election.public_key or pubkey_is_invalid:
logger.info(f"Generating ElGamal public key for election {election_id}")
elgamal = ElGamalEncryption(p=election.elgamal_p or 23, g=election.elgamal_g or 5)
# Store as base64-encoded bytes (public_key_bytes returns UTF-8 "p:g:h", then encode to base64)
election.public_key = base64.b64encode(elgamal.public_key_bytes)
# Use the property that returns properly formatted bytes "p:g:h"
election.public_key = elgamal.public_key_bytes
db.commit()
logger.info(f"✓ Generated public key for election {election_id}")
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,
@ -194,7 +179,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": pubkey_to_return
"public_key": base64.b64encode(election.public_key).decode() if election.public_key else None
}
except HTTPException:

View File

@ -499,29 +499,18 @@ async def setup_election(
# Générer les clés ElGamal si nécessaire
if not election.public_key:
elgamal = ElGamalEncryption()
elgamal = ElGamal()
# 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": pubkey_to_return
"elgamal_pubkey": election.public_key.decode('utf-8') if election.public_key else None
},
"blockchain_blocks": blockchain.get_block_count()
}
@ -557,19 +546,8 @@ 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": pubkey_to_return
"elgamal_pubkey": election.public_key.decode('utf-8') if election.public_key else None
}