package com.xyl.xzyf2020;
/**
* @BelongsProject: JavaSE_code
* @BelongsPackage: com.xyl.xzyf2020
* @Auther: Yongliang Xu l1264009784@163.com
* @Date: 2021/4/1 09:11
* @Description:
* @since JDK 1.8
*/
public interface XZYF {
void buildSystem();//系统建立
void setSecretValue();//设置秘密值
void setPK();//设置公钥
void setPartialSK();//部分私钥提取
void setSK();//设置私钥
void Sc();//签密
void Dsc();//解签密
}
package com.xyl.xzyf2020;/**
* @BelongsProject: JavaSE_code
* @BelongsPackage: com.xyl.xzyf2020
* @Auther: Yongliang Xu l1264009784@163.com
* @Date: 2021/4/1 09:25
* @Description:
* @since JDK 1.8
*/
import com.xyl.de.TimeCountProxyHandle;
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;
import java.math.BigInteger;
/**
*@ClassName XZYF2020
*@Description TODO
*@Author Yongliang Xu
*@Date 2021/4/1 9:25
*@Version 1.0
*/
public class XZYF2020 implements XZYF{
Pairing pairing;
Field Zr;
Element x_a,x_b,h0_a,h0_b,PK_a,PK_b,e,d,d_a,d_b,r1,r2,R1,R2,r,s,h,u1,u2,R11,R22,rr,zero;
public XZYF2020() {
init();
}
public void init(){
pairing = PairingFactory.getPairing("a.properties");
PairingFactory.getInstance().setUsePBCWhenPossible(true);
checkSymmetric(pairing);
Zr = pairing.getZr();
x_a=Zr.newElement();//秘密值
x_b=Zr.newElement();
h0_a=Zr.newElement();//哈希
h0_b=Zr.newElement();//哈希
PK_a=Zr.newElement();//公钥
PK_b=Zr.newElement();
e=Zr.newElement();//主公钥
d=Zr.newElement();//主私钥
d_a=Zr.newElement();//部分私钥
d_b=Zr.newElement();
r1=Zr.newElement();//随机数
r2=Zr.newElement();
R1=Zr.newElement();
R2=Zr.newElement();
r=Zr.newElement();
s=Zr.newElement();
h=Zr.newElement();
u1=Zr.newElement();
u2=Zr.newElement();
R11=Zr.newElement();
R22=Zr.newElement();
rr=Zr.newElement();
zero=Zr.newElement();
}
private void checkSymmetric(Pairing pairing) {
if (!pairing.isSymmetric()) {
throw new RuntimeException("密钥不对称");
}
}
@Override
public void buildSystem() {
e=Zr.newRandomElement().getImmutable();
d=e.invert().getImmutable();
System.out.println("----------------系统建立----------------");
System.out.println("e="+e);
System.out.println("d="+d);
System.out.println("ed="+e.mul(d));
}
@Override
public void setSecretValue() {
x_a=Zr.newRandomElement().getImmutable();//秘密值
x_b=Zr.newRandomElement().getImmutable();//秘密值
System.out.println("---------------设置秘密值---------------");
System.out.println("x_a="+x_a);
System.out.println("x_b="+x_b);
}
@Override
public void setPK() {
h0_a=Zr.newRandomElement().getImmutable();//模拟H0(ID_a)
h0_b=Zr.newRandomElement().getImmutable();//模拟H0(ID_a)
PK_a = h0_b.powZn(x_a).getImmutable();
PK_b = h0_b.powZn(x_b).getImmutable();
System.out.println("----------------设置公钥----------------");
System.out.println("PK_a="+PK_a);
System.out.println("PK_b="+PK_b);
}
@Override
public void setPartialSK() {
d_a=PK_a.powZn(d).getImmutable();
d_b=PK_b.powZn(d).getImmutable();
System.out.println("---------------部分私钥提取---------------");
System.out.println("d_a="+d_a);
System.out.println("d_b="+d_b);
}
@Override
public void setSK() {
System.out.println("----------------设置私钥----------------");
System.out.println("SK_a=(x_a,d_a)="+x_a+","+d_a);//完全私钥
System.out.println("SK_b=(x_b,d_b)="+x_b+","+d_b);
}
@Override
public void Sc() {
r1 = Zr.newRandomElement().getImmutable();
r2 = Zr.newRandomElement().getImmutable();
R1= h0_b.powZn(e.mul(r1)).getImmutable();//
R2= h0_b.powZn(r2).getImmutable();
r=(PK_b.powZn(r2)).mul(d_a.powZn(x_a.invert())).getImmutable();
h=Zr.newRandomElement().getImmutable();
// zero=zero.setToZero();
// u1=(h0_b.powZn(r1)).mul((d_a.invert()).powZn(h)).getImmutable();
// System.out.println("u1="+u1);
u1=(h0_b.powZn(r1)).mul(d_a.powZn(h.negate())).getImmutable();
// System.out.println("u1="+u1);
// u1=(h0_b.powZn(r1)).mul(d_a.powZn(zero.sub(h))).getImmutable();
// System.out.println("u1="+u1);
u2=r2.div(h.add(x_a)).getImmutable();
System.out.println("------------------签密------------------");
System.out.println("R1="+R1);
System.out.println("R2="+R2);
System.out.println("r="+r);
System.out.println("u1="+u1);
System.out.println("u2="+u2);
System.out.println("h="+h);
}
@Override
public void Dsc() {
System.out.println("-----------------解签密------------------");
R11=(u1.powZn(e)).mul(PK_a.powZn(h)).getImmutable();//
R22=(PK_a.powZn(u2)).mul((h0_b).powZn(h.mul(u2))).getImmutable();
System.out.println("R11="+R11);
System.out.println("R22="+R22);
if(R1.isEqual(R11)&&R2.isEqual(R22)){//
System.out.println("验证成功");
rr=R22.powZn(x_b).mul(d_b.powZn(x_b.invert())).getImmutable();
System.out.println("rr="+rr);
if(r.isEqual(rr)){
System.out.println("解密成功");
}else {
System.out.println("解密失败");
}
}else{
System.out.println("验证失败");
}
}
public static void main(String[] args) {
XZYF2020 xzyf2020 = new XZYF2020();
XZYF identProxy = (XZYF) Proxy.newProxyInstance(XZYF.class.getClassLoader(),
new Class[]{XZYF.class},new TimeCountProxyHandle(xzyf2020));
identProxy.buildSystem();
identProxy.setSecretValue();
identProxy.setPK();
identProxy.setPartialSK();
identProxy.setSK();
identProxy.Sc();
identProxy.Dsc();
}
}