同态加密验证。简单的同态加密包含加法同态和乘法同态加法同态算法最经典的是Paillier算法乘法同态算法最经典的是RSA算法。以下是RSA算法同态验证说明: 设: ab=c 基础等式其中:加密函数e解密函数d enctrypt:加密;decrypt:解密 乘法同态特为:eaeb=ecc=dec=deaeb验证c 是否等于ab即可得到乘法同态的正确性。利用python代码
以下是Python代码实现RSA算法同态验证:
导入RSA模块
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5
定义RSA密钥
key = RSA.generate(2048)
获取公钥和私钥
public_key = key.publickey() private_key = key
加密函数
def encrypt(msg): cipher = PKCS1_v1_5.new(public_key) ciphertext = cipher.encrypt(msg.encode()) return ciphertext
解密函数
def decrypt(ciphertext): cipher = PKCS1_v1_5.new(private_key) message = cipher.decrypt(ciphertext, None) return message.decode()
定义a,b,c的值
a = 123 b = 456 c = a * b
对a,b,c进行加密
e_a = encrypt(str(a)) e_b = encrypt(str(b)) e_c = encrypt(str(c))
对e_c进行解密,验证乘法同态的正确性
if c == int(decrypt(e_a)) * int(decrypt(e_b)): print("验证成功") else: print("验证失败"
原文地址: https://www.cveoy.top/t/topic/f2pm 著作权归作者所有。请勿转载和采集!