serengil / LightPHE

A Lightweight Partially Homomorphic Encryption Library for Python
https://bit.ly/46vWz9w
MIT License
53 stars 5 forks source link

Public key export deletes private key #18

Closed gR0OT closed 7 months ago

gR0OT commented 7 months ago

When performing export_keys() in lightphe.init in case of exporting (publishing) the public key, it deletes private key in the process. This leaves the LightPHE object (who should have private key, because it generated it) unable to decrypt.

246: if public is True and keys.get("private_key") is not None:
247:     del keys["private_key"]

Current workaround:

gm = LightPHE('Paillier')
gm.export_keys('private.key', public=False)
gm.export_keys('public.key', public=True)
gm = LightPHE('Paillier', key_file='private.key')

Proposed fix: Don't delete the private_key from keys when performing export_keys(), just export the public_key

pubkey = {'public_key': keys['public_key']}
with open(target_file, "w", encoding="UTF-8") as file:
    file.write(pubkey)
serengil commented 7 months ago

Closed with PR - https://github.com/serengil/LightPHE/pull/19