JPBC库实现基于身份的签名体制
作者:互联网
JPBC库实现基于身份的签名体制---Hess体制
Hess算法:
代码:
Hess类:
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import java.lang.reflect.Proxy;
public class Hess implements Ident{
private Element s,r,P,Ppub,Su,Qu,W,h,P1,T,TT,T1,T2,W1,W2;
private Field G1,Zr;
private Pairing pairing;
public Hess(){
init();
}
private void init(){
pairing = PairingFactory.getPairing("a.properties");
PairingFactory.getInstance().setUsePBCWhenPossible(true);
checkSymmetric(pairing);
Zr = pairing.getZr();
G1 = pairing.getG1();
Ppub = G1.newElement();
Su = G1.newElement();
Qu = G1.newElement();
W = G1.newElement();
W1 = G1.newElement();
W2 = G1.newElement();
h = Zr.newElement();
Field GT = pairing.getGT();
T = GT.newElement();
T1 = GT.newElement();
T2 = GT.newElement();
}
private void checkSymmetric(Pairing pairing){
if(!pairing.isSymmetric()){
throw new RuntimeException("密钥不对称");
}
}
@Override
public void buildSystem() {
System.out.println("---------系统建立阶段--------------");
s = Zr.newRandomElement().getImmutable();
P = G1.newRandomElement().getImmutable();
Ppub = P.mulZn(s).getImmutable();
System.out.println("P=" + P);
System.out.println("s=" + s);
System.out.println("Ppub=" + Ppub);
}
@Override
public void extractSecretKey() {
Qu = pairing.getG1().newElement().setFromHash("IDu".getBytes(),0,3).getImmutable();
Su = Qu.mulZn(s).getImmutable();
System.out.println("Qu=" + Qu);
System.out.println("Su=" + Su);
}
@Override
public void sign() {
System.out.println("---------签名阶段--------------");
r = Zr.newRandomElement().getImmutable();
P1 = G1.newRandomElement().getImmutable();
T = pairing.pairing(P1,P);
T = T.powZn(r);
h = Zr.newRandomElement().getImmutable();//模拟h = H2(m,T)
W1 = P1.mulZn(r);
W2 = Su.mulZn(h);
W = W2.add(W1);
System.out.println("h=" + h);
System.out.println("W=" + W);
}
@Override
public void verify() {
System.out.println("--------------验证阶段------------------");
T1 = pairing.pairing(W,P);
Ppub = Ppub.invert();//求Ppub的乘法逆元
T2 = pairing.pairing(Qu,Ppub);//Ppub实际上是-Ppub
T2 = T2.powZn(h);
TT = T1.mul(T2);
if(TT.isEqual(T))
System.out.println("T");
else
System.out.println("F");
int byte1 = W.toBytes().length;
int byte2 = h.toBytes().length;
System.out.println("签名的长度=" + (byte1+byte2));
}
public static void main(String[] args){
Hess hess = new Hess();
Ident identProxy = (Ident) Proxy.newProxyInstance(Hess.class.getClassLoader(),
new Class[]{Ident.class},new TimeCountProxyHandle(hess));
identProxy.buildSystem();
identProxy.extractSecretKey();
identProxy.sign();
identProxy.verify();
}
}
Ident 接口:
public interface Ident {
void buildSystem();
void extractSecretKey();
void sign();
void verify();
}
TimeCountProxyHandle 类:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class TimeCountProxyHandle implements InvocationHandler {
private Object proxied;
public TimeCountProxyHandle(Object obj){
proxied = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long begin = System.currentTimeMillis();
Object result = method.invoke(proxied,args);
long end = System.currentTimeMillis();
System.out.println(method.getName() + "耗时" +(end-begin) +"ms");
return result;
}
}
标签:void,pairing,System,体制,newElement,签名,println,out,JPBC 来源: https://blog.csdn.net/kangege21/article/details/114636795