资源简介
BGN是一种同态加密方案,是Boned D等人在2005提出的一种具有全同态性质的加密方案。和传统的仅能支持单同态的elgamal和paillier加密方案不一样,BGN能够同时支持加同态和一次乘同态运算。
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); // Requires
// 2
// prime
// numbers.
param = a1.generate();
TypeA1Pairing pairing = new TypeA1Pairing(param);
order = param.getBigInteger(“n“); // Must extract the prime numbers for
// both keys.
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());
Element T = PK.doPairing(C D);
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 =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 988 2018-08-22 18:27 BGN\.classpath
文件 379 2018-08-22 18:26 BGN\.project
文件 598 2018-08-22 18:26 BGN\.settings\org.eclipse.jdt.core.prefs
文件 5456 2018-08-22 18:27 BGN\bin\BGN\BGNEncryption.class
文件 1579 2018-08-22 18:27 BGN\bin\BGN\PublicKey.class
文件 2149 2018-08-22 18:27 BGN\bin\BGN\text.class
文件 4455 2018-08-22 18:15 BGN\src\BGN\BGNEncryption.java
文件 803 2018-08-21 18:09 BGN\src\BGN\PublicKey.java
文件 1265 2018-08-22 16:49 BGN\src\BGN\text.java
目录 0 2018-08-22 18:27 BGN\bin\BGN
目录 0 2018-08-22 12:03 BGN\src\BGN
目录 0 2018-08-22 18:26 BGN\.settings
目录 0 2018-08-22 18:27 BGN\bin
目录 0 2018-08-22 18:26 BGN\src
目录 0 2018-08-22 18:26 BGN
----------- --------- ---------- ----- ----
17672 15
- 上一篇:junit4.12.zip和依赖的jar包
- 下一篇:简单的RSA算法的实现
评论
共有 条评论