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;
    }
}

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值