This commit is contained in:
sherlock 2025-10-07 03:32:17 +05:30
parent 9db9ac05ce
commit df44199a02
25 changed files with 544 additions and 0 deletions

View file

@ -0,0 +1,50 @@
from Crypto.PublicKey import RSA
from Crypto.Util.number import bytes_to_long, long_to_bytes
def generate_keys():
key = RSA.generate(2048)
return key
def encrypt(message, public_key):
n = public_key.n
e = public_key.e
ciphertext = pow(message, e, n)
return ciphertext
def decrypt(ciphertext, private_key):
n = private_key.n
d = private_key.d
plaintext = pow(ciphertext, d, n)
return plaintext
# Generate keys
key = generate_keys()
public_key = key.publickey()
private_key = key
# Original values
m1 = 7
m2 = 3
# Encrypt
c1 = encrypt(m1, public_key)
c2 = encrypt(m2, public_key)
print(f"Ciphertext 1: {c1}")
print(f"Ciphertext 2: {c2}")
# Homomorphic multiplication
c_product = (c1 * c2) % public_key.n
print(f"Encrypted product: {c_product}")
# Decrypt result
decrypted_product = decrypt(c_product, private_key)
print(f"Decrypted product: {decrypted_product}")
# Verify
expected_product = m1 * m2
print(f"Expected product: {expected_product}")
print(f"Match: {decrypted_product == expected_product}")

103
IS/Lab/Lab7/Pallier.py Normal file
View 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()