IS Lab
This commit is contained in:
parent
9db9ac05ce
commit
df44199a02
25 changed files with 544 additions and 0 deletions
103
IS/Lab/Lab7/Pallier.py
Normal file
103
IS/Lab/Lab7/Pallier.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
import random
|
||||
from math import gcd
|
||||
from sympy import isprime, lcm
|
||||
|
||||
|
||||
def generate_prime(bits=512):
|
||||
while True:
|
||||
num = random.getrandbits(bits)
|
||||
if isprime(num):
|
||||
return num
|
||||
|
||||
|
||||
def generate_keypair(bits=512):
|
||||
p = generate_prime(bits)
|
||||
q = generate_prime(bits)
|
||||
|
||||
n = p * q
|
||||
n_squared = n * n
|
||||
lambda_n = lcm(p - 1, q - 1)
|
||||
|
||||
g = n + 1
|
||||
|
||||
def L(x):
|
||||
return (x - 1) // n
|
||||
|
||||
mu = pow(L(pow(g, lambda_n, n_squared)), -1, n)
|
||||
|
||||
public_key = (n, g)
|
||||
private_key = (lambda_n, mu)
|
||||
|
||||
return public_key, private_key
|
||||
|
||||
|
||||
def encrypt(public_key, plaintext):
|
||||
n, g = public_key
|
||||
n_squared = n * n
|
||||
|
||||
while True:
|
||||
r = random.randint(1, n - 1)
|
||||
if gcd(r, n) == 1:
|
||||
break
|
||||
|
||||
ciphertext = (pow(g, plaintext, n_squared) * pow(r, n, n_squared)) % n_squared
|
||||
|
||||
return ciphertext
|
||||
|
||||
|
||||
def decrypt(public_key, private_key, ciphertext):
|
||||
n, g = public_key
|
||||
lambda_n, mu = private_key
|
||||
n_squared = n * n
|
||||
|
||||
def L(x):
|
||||
return (x - 1) // n
|
||||
|
||||
plaintext = (L(pow(ciphertext, lambda_n, n_squared)) * mu) % n
|
||||
|
||||
return plaintext
|
||||
|
||||
|
||||
def homomorphic_add(public_key, ciphertext1, ciphertext2):
|
||||
n, g = public_key
|
||||
n_squared = n * n
|
||||
|
||||
result = (ciphertext1 * ciphertext2) % n_squared
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
print("Paillier Encryption Scheme Implementation\n")
|
||||
|
||||
print("Generating keypair...")
|
||||
public_key, private_key = generate_keypair(bits=512)
|
||||
print("Keys generated successfully.\n")
|
||||
|
||||
m1 = 15
|
||||
m2 = 25
|
||||
print(f"Original integers: {m1} and {m2}")
|
||||
print(f"Expected sum: {m1 + m2}\n")
|
||||
|
||||
print("Encrypting integers...")
|
||||
c1 = encrypt(public_key, m1)
|
||||
c2 = encrypt(public_key, m2)
|
||||
print(f"Ciphertext of {m1}: {c1}")
|
||||
print(f"Ciphertext of {m2}: {c2}\n")
|
||||
|
||||
print("Performing homomorphic addition on encrypted values...")
|
||||
c_sum = homomorphic_add(public_key, c1, c2)
|
||||
print(f"Encrypted sum: {c_sum}\n")
|
||||
|
||||
print("Decrypting the result...")
|
||||
decrypted_sum = decrypt(public_key, private_key, c_sum)
|
||||
print(f"Decrypted sum: {decrypted_sum}\n")
|
||||
|
||||
if decrypted_sum == m1 + m2:
|
||||
print("✓ Verification successful! The decrypted sum matches the original sum.")
|
||||
else:
|
||||
print("✗ Verification failed! The decrypted sum does not match.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue