BGN同态加密算法:

BGN是一种同态加密方案,是Bonel h等人在2005提出的一种具有全同态性质的加密方案。和传统的仅能支持单同态的elgamal和paillier加密方案不一样,BGN能够同时支持加同态和一次乘同态运算。

由于乘法同态的实现是通过双线性对性质实现的,所以仅仅只能实现一次的乘同态。

BGN一般的加密方案如下:

BGN在JAVA中的实现:

BGN的实现主要使用JAVA中的大整数math.BigInteger类以及双线性库JPBC实现,具体代码如下:

package BGN;import java.math.BigInteger;
import java.security.SecureRandom;import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.PairingParameters;
import it.unisa.dia.gas.plaf.jpbc.pairing.a1.TypeA1CurveGenerator;
import it.unisa.dia.gas.plaf.jpbc.pairing.a1.TypeA1Pairing;
import it.unisa.dia.gas.plaf.jpbc.util.math.BigIntegerUtils;public class BGNEncryption {public static final String start = "start";public static final String end = "end";private PairingParameters param;private BigInteger r;private BigInteger q; // This is the private key.private BigInteger order;private SecureRandom rng;public PublicKey gen(int bits) {rng = new SecureRandom();TypeA1CurveGenerator a1 = new TypeA1CurveGenerator(rng, 2, bits); param = a1.generate();TypeA1Pairing pairing = new TypeA1Pairing(param);order = param.getBigInteger("n"); r = param.getBigInteger("n0");q = param.getBigInteger("n1");Field f = pairing.getG1();Element P = f.newRandomElement();P = P.mul(param.getBigInteger("l"));Element Q = f.newElement();Q = Q.set(P);Q = Q.mul(r);return new PublicKey(pairing, P, Q, order);}public Element encrypt(PublicKey PK, int msg) {BigInteger t = BigIntegerUtils.getRandom(PK.getN());int m = msg;
//      System.out.println("Hash is " + m);Field f = PK.getField();Element A = f.newElement();Element B = f.newElement();Element C = f.newElement();A = A.set(PK.getP());A = A.mul(BigInteger.valueOf(m));B = B.set(PK.getQ());B = B.mul(t);C = C.set(A);C = C.add(B);return C;}public Element add(PublicKey PK, Element A, Element B) {BigInteger t = BigIntegerUtils.getRandom(PK.getN());Field f = PK.getField();Element output = f.newElement();Element aux = f.newElement();aux.set(PK.getQ());aux.mul(t);output.set(A);output.add(B);output.add(aux);return output;}public Element mul(PublicKey PK, Element C, Element D) {BigInteger t = BigIntegerUtils.getRandom(PK.getN());
//      double t1 = System.currentTimeMillis();Element T = PK.doPairing(C, D);
//      double t2 = System.currentTimeMillis();
//      System.out.println("一次对运算操作的时间"+(t2-t1)+"ms");Element K = PK.doPairing(PK.getQ(), PK.getQ());K = K.pow(t);return T.mul(K);}public String decryptMul(PublicKey PK, BigInteger sk, Element C) {Element PSK = PK.doPairing(PK.getP(), PK.getP());PSK.pow(sk);Element CSK = C.duplicate();CSK.pow(sk);Element aux = PSK.duplicate();BigInteger m = new BigInteger("1");while (!aux.isEqual(CSK)) {aux = aux.mul(PSK);m = m.add(BigInteger.valueOf(1));}return m.toString();}public String decrypt(PublicKey PK, BigInteger sk, Element C) {Field f = PK.getField();Element T = f.newElement();Element K = f.newElement();Element aux = f.newElement();T = T.set(PK.getP());T = T.mul(sk);K = K.set(C);K = K.mul(sk);aux = aux.set(T);BigInteger m = new BigInteger("1");while (!aux.isEqual(K)) {// This is a brute force implementation of finding the discrete// logarithm.// Performance may be improved using algorithms such as Pollard's// Kangaroo.aux = aux.add(T);m = m.add(BigInteger.valueOf(1));}return m.toString();}public static void main(String[] args) {BGNEncryption b = new BGNEncryption();PublicKey PK = b.gen(256);BigInteger f = PK.getN();Element P = PK.getP();Element Q = PK.getQ();BigInteger order = PK.getN();int len1 = P.getLengthInBytes();int len2 = Q.getLengthInBytes();int len3 = order.bitLength();Element msg1 = b.encrypt(PK, 20);int len = msg1.getLengthInBytes();Element msg2 = b.encrypt(PK, 25);int len6 = msg2.getLengthInBytes();Element add = b.add(PK, msg1, msg2);String jiemi = b.decrypt(PK, b.q, add);System.out.println("Addition: "+jiemi);int len4 = add.getLengthInBytes();
//      double t5 = System.currentTimeMillis();Element mul = b.mul(PK, msg1, msg2);
//      double t6 = System.currentTimeMillis();
//      System.out.println("一次同态乘法的时间"+(t6-t5)+"ms");System.out.println("Mul: " + b.decryptMul(PK, b.q, mul));int len5 = mul.getLengthInBytes();System.out.println("P的长度:"+len1);System.out.println("Q的长度:"+len2);System.out.println("阶为:"+len3);System.out.println("msg1的长度:"+len);System.out.println("加同态"+len4);System.out.println("乘同态"+len5);
package BGN;import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.plaf.jpbc.pairing.a1.TypeA1Pairing;
import java.math.BigInteger;public class PublicKey {private TypeA1Pairing map;private Element P, Q;private BigInteger n;private Field f;public PublicKey(TypeA1Pairing pairing, Element gen, Element point,BigInteger order) {map = pairing;P = gen.set(gen);Q = point.set(point);n = order;f = pairing.getG1();}public Element doPairing(Element A, Element B) {return map.pairing(A, B);}public Element getP() {return this.P;}public Element getQ() {return this.Q;}public BigInteger getN() {return this.n;}public Field getField() {return this.f;}
}

同态实验结果:

Addition: 45
Mul: 500
P的长度:130
Q的长度:130
阶为:512
msg1的长度:130
加同态130
乘同态130

代码出处:https://stackoverflow.com/questions/33581962/bgn-implementation-in-java

https://github.com/andyjojo/bgn

密码学之BGN同态加密算法相关推荐

  1. 密码学中的同态加密算法,保证数据的安全,你了解吗?

    密码学中的同态加密算法,保证数据的安全,你了解吗? 目录 密码学中的同态加密算法,保证数据的安全,你了解吗? 1:什么是同态加密? 2:同态加密算法原理 3:标准化进展 4: 主流同态加密算法原理 4 ...

  2. 【联邦学习邂逅密码学系列】基于同态加密算法python代码实现

    这是我的学习笔记,若有不足和错误之处,欢迎交流和指正,谢谢! 联系方式:lrcgnn@163.com 前言 联邦学习是一种参与方之间联合隐私训练的新范式,受到学术界和工业界的关注.然而一些研究表明,联 ...

  3. gentry同态加密算法_基于Gentry全同态加密算法公钥个数优化的研究

    基于 Gentry 全同态加密算法公钥个数优化的研究 胡勇祥 [期刊名称] <计算机光盘软件与应用> [年 ( 卷 ), 期] 2014(000)022 [摘要] Gentry 的全同态加 ...

  4. BabaSSL:支持半同态加密算法 EC-ElGamal

    -- 数据不出域.可用不可见 01 背 景 随着大数据与人工智能的快速发展,个人隐私数据泄露和滥用时有发生,隐私安全问题也越来越被重视. 国家于 2020 年施行密码法.2021 年施行个人信息保护法 ...

  5. Paillier同态加密算法总结

    一.密码学知识总结 1.卡迈克尔数 对于所有与n互质的正整数b,都有同余式b^(n-1)≡ 1 (mod n)成立,则称合数n为Carmichael数. 卡迈克尔数有至少3个正素因数 2.费马小定理 ...

  6. 开源同态加密库 HEhub 发布|首个由国内隐私计算团队研发的(全)同态加密算法库...

    扫码关注 保护消费者权益 我们在行动 时至今日,数据要素已经成为数字经济时代最重要的生产要素之一,成为众多企业和机构的核心资产,而数据价值的体现依赖于数据的安全流通和利用.隐私计算作为新兴技术为数据的 ...

  7. 高安全性同态加密算法_坏的同态性教程

    高安全性同态加密算法 I was going to write at length about the issues I see in neumorphism and why this trend s ...

  8. 网络安全_密码学实验_非对称加密算法RSA

    网络安全_密码学实验_非对称加密算法RSA 一.实验环境 二.非对称加密RSA 1.理解RSA算法原理 2.加密过程 解密过程 一.实验环境 PyCharm 2019.2.4 (Professiona ...

  9. 经典同态加密算法Paillier解读 - 原理、实现和应用

    摘要 随着云计算和人工智能的兴起,如何安全有效地利用数据,对持有大量数字资产的企业来说至关重要.同态加密,是解决云计算和分布式机器学习中数据安全问题的关键技术,也是隐私计算中,横跨多方安全计算,联邦学 ...

最新文章

  1. python数据写入CSV
  2. Google 选择 Jetty 放弃 Tomcat
  3. html4视频测试方法,3.4 处理视频 - HTML5 Canvas 实战
  4. For循环中不可以嵌套RDD操作
  5. 小程序未来将有广阔的发展前景
  6. GIS实战应用案例100篇(一)-GEE主成分分析(含代码)
  7. Bootstrap导航条所支持的组件
  8. 新型冠状病毒肺炎国内分省分日期从1.16起的全部数据爬取与整理代码(附下载)
  9. 毕业从事汽车行业,转行测试工程师,3个月完成了蜕变,我很满意...
  10. html中背景渐变斜着渐变,CSS3 斜向渐变背景
  11. 已知某网络有一个地址是167.199.170.82/27,问这个网络的网络掩码。
  12. Fiori学习:WEBIDE本地个人版安装
  13. 2023年重庆邮电大学计算机科学与技术(802)初试经验贴
  14. java 多线程重温
  15. 缓存加速------Redis的五种数据类型(String,List,Hash,Set,Sorted Set)
  16. 英伟达Jetson Nano的初步了解
  17. LIN总线解析与UART模拟LIN从机实战
  18. 使用Python在Markdown插入图片并自动获取链接
  19. P4924 [1007]魔法少女小Scarlet
  20. C - Unusual Product

热门文章

  1. 自动驾驶域控制器话题下的软件系统设计和研发管理
  2. 计算机itpt证书有什么用,ITPT信息技术应用专业能力培训项目认证课程体系表.pdf...
  3. Java集合移除某个元素
  4. Mac苹果移动硬盘数据丢失怎么恢复?
  5. maya linux 安装教程视频,Maya快捷键插件增强工具ZooTools Pro 2.2.4 for Maya Win/Linux+ Assets pack 2.3+视频教程...
  6. C# 使用DirectX中的Device类实现3D渲染
  7. 【10个精品网站】找素材、找图片、PDF在线工具、免费图片降噪修复、免费可商用字体、PPT模板
  8. Java编程思想读书笔记——第七章:复用类
  9. 嵌入式Linux的MiniGUI研究和移植
  10. 清橙网A1110. 街道