Python2.7实现Paillier算法+验证加法同态性质

from __future__ import print_function # 解决python2 print()出现其他字符的问题
import gmpy2
import time


# generate prime number 生成素数
def __generate_prime__(rs):
    p = gmpy2.mpz_urandomb(rs, 1024)
    while not gmpy2.is_prime(p):
        p += 1
    return p


# generate L(x)
def __el__(x, n):
    fx = gmpy2.c_div((x - 1), n)
    return int(fx)


# key generation 生成公私钥对
def keygen():
    while True:
        random_state = gmpy2.random_state(int(time.time()))
        p = __generate_prime__(random_state)
        q = __generate_prime__(random_state)
        if gmpy2.gcd((p * q), ((p - 1) * (q - 1))) == 1:
            break
    n = p * q
    lmd = gmpy2.lcm((p - 1), (q - 1))
    random_state = gmpy2.random_state(int(time.time()))
    g = gmpy2.mpz_random(random_state, n ** 2)
    x = gmpy2.powmod(g, lmd, n ** 2)
    el1 = __el__(x, n)
    mu = gmpy2.powmod(el1, (-1), n)
    return (n, g), (lmd, mu, n)


# encryption of plaintext 加密
def encrypt(plaintext, public_key):
    m = int(plaintext)
    n, g = public_key
    random_state = gmpy2.random_state(int(time.time()))
    r = gmpy2.mpz_random(random_state, n)
    c = gmpy2.powmod(g, m, n ** 2) * gmpy2.powmod(r, n, n ** 2)
    return c


# decryption of ciphertext 解密
def decrypt(ciphertext, private_key):
    c = ciphertext
    lmd, mu, n = private_key
    x = gmpy2.powmod(c, lmd, n ** 2)
    el2 = __el__(x, n)
    result = gmpy2.mod((el2 * mu), n)
    return result


# main
if __name__ == "__main__":
    public_key1, private_key1 = keygen() # 生成公私钥对
    plaintext1 = input("Enter first number:")
    plaintext2 = input("Enter second number:")
    ciphertext1 = encrypt(plaintext1, public_key1)
    ciphertext2 = encrypt(plaintext2, public_key1)
    result1 = ciphertext1 * ciphertext2 # 密文相乘
    result2 = decrypt(result1, private_key1) # 新密文解密得出的新明文应为输入的明文的相加
    print("Ciphertext of first number:", ciphertext1.digits())
    print("Ciphertext of second number:", ciphertext2.digits())
    print("New Ciphertext:", result1.digits())
    print("New Plaintext:", result2.digits())

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Paillier加密算法是一种公钥加密算法,它的安全性基于离散对数难题。下面是Python实现Paillier加密算法的示例代码: ```python import random from math import gcd def lcm(a, b): return abs(a*b) // gcd(a, b) def generate_keys(bit_length): p = q = 1 while gcd(p*q, (p-1)*(q-1)) != 1: p = random.getrandbits(bit_length//2) q = random.getrandbits(bit_length//2) n = p*q g = n+1 l = lcm(p-1, q-1) while True: L = random.randint(1, n*n) if gcd(L, n) == 1: break mu = pow(L, -1, n) lam = pow(g, l, n*n) return (n, g, lam, mu), (n, l) def encrypt(pub_key, m): n, g = pub_key r = random.randint(1, n) return pow(g, m, n*n) * pow(r, n, n*n) % (n*n) def decrypt(priv_key, c): n, l = priv_key u = pow(c, l, n*n) m = (u-1) // n * pow(priv_key[1], -1, n) % n return m def add(pub_key, c1, c2): return c1 * c2 % (pub_key[0]**2) def multiply_const(pub_key, c, m): return pow(c, m, pub_key[0]**2) # Example usage pub_key, priv_key = generate_keys(1024) m1 = 12345 m2 = 67890 c1 = encrypt(pub_key, m1) c2 = encrypt(pub_key, m2) # Homomorphic addition c3 = add(pub_key, c1, c2) print(decrypt(priv_key, c3)) # Output: 80235 # Homomorphic multiplication by constant c4 = multiply_const(pub_key, c1, 2) print(decrypt(priv_key, c4)) # Output: 24690 ``` 在这个示例代码中,我们首先定义了一些辅助函数,比如计算最小公倍数和生成随机素数等。然后我们实现了密钥生成函数`generate_keys`,加密函数`encrypt`,解密函数`decrypt`,加法函数`add`,以及常数乘法函数`multiply_const`。最后我们对这些函数进行了简单的测试,验证Paillier加密算法同态性质

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值