Skip to content

Commit

Permalink
Create homomorphic_encryption.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 26, 2024
1 parent 71cf781 commit 0cb42d9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions QuantumNexusProtocol/src/cryptography/homomorphic_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from phe import paillier

class HomomorphicEncryption:
def __init__(self):
self.public_key, self.private_key = paillier.generate_paillier_keypair()

def encrypt(self, value):
return self.public_key.encrypt(value)

def add_encrypted(self, encrypted_value1, encrypted_value2):
return encrypted_value1 + encrypted_value2

def decrypt(self, encrypted_value):
return self.private_key.decrypt(encrypted_value)

# Example usage
if __name__ == "__main__":
he = HomomorphicEncryption()
encrypted_value1 = he.encrypt(10)
encrypted_value2 = he.encrypt(20)
encrypted_sum = he.add_encrypted(encrypted_value1, encrypted_value2)
decrypted_sum = he.decrypt(encrypted_sum)
print(f"Decrypted Sum: {decrypted_sum}")

0 comments on commit 0cb42d9

Please sign in to comment.