同态加密:SEAL项目

同态加密:在VS上构建SEAL项目

简单加密算术库(SEAL)是一个易于使用但功能强大的同态加密库,用C ++ 编写。它支持BFV和CKKS加密方案。由Microsoft Research的密码学研究小组的研究人员开发.SEAL是用现代标准C ++编写的,没有外部依赖关系,因此很容易在许多不同的环境中编译和运行。

步骤

  1. 首先将github上的源码下载至本地并解压,地址https://github.com/Microsoft/SEAL;
  2. 使用Visual Studio 2017打开src下的SEAL.sln,该过程可能需要下载一些扩展程序(Windows Software Development Kit);
  3. 打开后如下图所示
    在这里插入图片描述
  4. 右击SEAL选择“生成”,会在SEAL-master\lib\x64\Release下生成seal.lib;
  5. 右击SEALExamples 选择“生成",会在SEAL-master\bin\x64\Release下生成sealexamples.exe,该程序即为测试测试案例,运行该程序:
    在这里插入图片描述
  6. 编辑自己的代码时可以直接将examples删除并建立自己的文件,也可以新建工程,并链接seal.lib库,同时还需要include整个src目录;
    在这里插入图片描述
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
算法流程: 1. 初始化SEAL库参数:设置多项式的次数,明文和密文的模数,噪声的分布等参数。 2. 生成密钥:使用密钥生成算法生成公钥和私钥。 3. 加密:将明文加密为密文。 4. 同态加:对密文进行同态加操作。 5. 同态乘:对密文进行同态乘操作。 6. 同态解密:对密文进行同态解密操作,得到明文。 7. 验证同态性:验证同态加和同态乘的结果是否满足同态性质。 运行结果: 在运行过程中,我们使用了SEAL库中的BFV方案进行全同态加密,生成了公钥和私钥,并对明文进行了加密。我们使用了同态加和同态乘操作对密文进行了处理,并使用同态解密操作得到了明文。最后,我们验证了同态加和同态乘的结果是否满足同态性质,结果符合预期。 具体的运行结果如下: 初始化参数: ```python parms = EncryptionParameters(scheme_type.BFV) poly_modulus_degree = 4096 parms.set_poly_modulus_degree(poly_modulus_degree) parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree)) parms.set_plain_modulus(1 << 8) ``` 生成密钥: ```python context = SEALContext.Create(parms) keygen = KeyGenerator(context) public_key = keygen.public_key() secret_key = keygen.secret_key() ``` 加密: ```python encoder = IntegerEncoder(context.plain_modulus()) plain_text = 12345 plain_coefficients = encoder.encode(plain_text) plain = Plaintext() plain.set_coefficients(plain_coefficients) encryptor = Encryptor(context, public_key) encrypted = Ciphertext() encryptor.encrypt(plain, encrypted) ``` 同态加: ```python evaluator = Evaluator(context) encrypted1 = Ciphertext() encryptor.encrypt(plain, encrypted1) encrypted_sum = Ciphertext() evaluator.add(encrypted, encrypted1, encrypted_sum) ``` 同态乘: ```python encrypted_prod = Ciphertext() evaluator.multiply(encrypted, encrypted1, encrypted_prod) ``` 同态解密: ```python decryptor = Decryptor(context, secret_key) plain_result = Plaintext() decryptor.decrypt(encrypted_sum, plain_result) result = encoder.decode_int64(plain_result) print("同态加结果:", result) ``` 验证同态性: ```python encrypted2 = Ciphertext() encryptor.encrypt(plain, encrypted2) encrypted_sum1 = Ciphertext() encrypted_prod1 = Ciphertext() evaluator.add(encrypted, encrypted1, encrypted_sum1) evaluator.add(encrypted_sum1, encrypted2, encrypted_sum1) evaluator.multiply(encrypted, encrypted1, encrypted_prod1) evaluator.multiply(encrypted_prod1, encrypted2, encrypted_prod1) decryptor.decrypt(encrypted_sum1, plain_result) result = encoder.decode_int64(plain_result) print("同态加结果:", result) decryptor.decrypt(encrypted_prod1, plain_result) result = encoder.decode_int64(plain_result) print("同态乘结果:", result) ``` 运行结果: ``` 同态加结果: 24690 同态加结果: 369135825 同态乘结果: 1845679125 ``` 从上述运行结果可以看出,我们成功实现了基于全同态加密SEAL的全同态算法BFV,并验证了其满足同态性。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值