From e927729e59ed27649bc3323d7d259b0ddd44e649 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 1 Dec 2024 18:46:07 +0700 Subject: [PATCH] Create homomorphic_encryption.py --- src/security/homomorphic_encryption.py | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/security/homomorphic_encryption.py diff --git a/src/security/homomorphic_encryption.py b/src/security/homomorphic_encryption.py new file mode 100644 index 000000000..e9f40dcfc --- /dev/null +++ b/src/security/homomorphic_encryption.py @@ -0,0 +1,42 @@ +import numpy as np + +class Paillier: + def __init__(self, p, q): + self.p = p + self.q = q + self.n = p * q + self.n_squared = self.n ** 2 + self.g = self.n + 1 + self.lambda_ = (p - 1) * (q - 1) + + def encrypt(self, plaintext): + r = np.random.randint(1, self.n) + ciphertext = (pow(self.g, plaintext, self.n_squared) * pow(r, self.n, self.n_squared)) % self.n_squared + return ciphertext + + def decrypt(self, ciphertext): + u = pow(ciphertext, self.lambda_, self.n_squared) + plaintext = (u - 1) // self.n % self.n + return plaintext + + def add_encrypted(self, c1, c2): + return (c1 * c2) % self.n_squared + +# Example usage +if __name__ == "__main__": + p = 11 + q = 13 + paillier = Paillier(p, q) + + plaintext1 = 5 + plaintext2 = 7 + + encrypted1 = paillier.encrypt(plaintext1) + encrypted2 = paillier.encrypt(plaintext2) + + encrypted_sum = paillier.add_encrypted(encrypted1, encrypted2) + decrypted_sum = paillier.decrypt(encrypted_sum) + + print(f"Encrypted 1: {encrypted1}") + print(f"Encrypted 2: {encrypted2}") + print(f"Decrypted Sum: {decrypted_sum}")