使用JPBC实现双线性对加密算法(BasicIdent体制的java实现)

前言

现在网上关于java版的双线性对算法的实现的代码很少,我在前段时间想找下这方面的代码都找不到,结果只能自己摸索。经过一段时间的摸索,终于配置好了jPBC库并实现了一些代码。所以为了让大家学习双线性算法的实现时少走点弯路,想通过博客的形式将我前段时间的学习成果跟大家分享下,为处于研究双线对密码初级阶段,还不懂得如何用java实现算法的菜鸟们提供一篇技术性的博客。

正文

关于java配对库网址为http://gas.dia.unisa.it/projects/jpbc/index.html#.VTDrLSOl_Cw,在这个网址http://gas.dia.unisa.it/projects/jpbc/docs/pairing.html#.Vz2yzOyEAzB有基本函数的用法,只看这个网址,相信很多对于Pairing-Based Cryptography Library (JPBC)还没入门的同学肯定是看的云里雾里的,看着就烦,其实我当初就是这样的。大家先不用急,学习这个库也是一个循序渐进的过程。关于jPBC库的配置,推荐一篇博客http://blog.csdn.net/liuweiran900217/article/details/23414629,这个博客已经将jPBC的配置写的很详细了,我就不赘述了。JPBC库中函数的基本使用和一些简单的算法实现,现在还没有发现专门介绍java的PBC库的相关书籍,推荐一本李发根,吴威峰著的书——《基于配对的密码学》,这本书详细讲了PBC库中每个函数的使用,还有经典算法的代码实现,这本书是C语言版的,对照这本书来研究JPBC会相对轻松很多。

用JPBC实现的算法实例
BasicIdent的基于身份的加密体制是由Boneh和Franklin在《Identity-Based Encryption fromthe Weil Pairing》提出的,算法的加解密过程大家可以自行参考下这篇论文,过程还是比较简单的。
Ident接口
package cn.edu.zjut.pbc;


public interface Ident {
	
	void buildSystem();
	
	void extractSecretKey();
	
	void encrypt();
	
	void decrypt();

}
这个类是核心类,包括初始化init(),配对的对称性判断checkSymmetric(),系统建立buildSystem(),密钥提取extractSecretKey(),加密encrypt(),解密decrypt()。
package cn.edu.zjut.pbc;

import it.unisa.dia.gas.jpbc.*;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import java.lang.reflect.Proxy;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BasicIdent2 implements Ident {

	private Element s, r, P, Ppub, Su, Qu, V, T1, T2;
	private Field G1, Zr;
	private Pairing pairing;

	public BasicIdent2() {
		init();
	}

	/**
    * 初始化
    */
	private void init() {
		pairing = PairingFactory.getPairing("a.properties");//
		PairingFactory.getInstance().setUsePBCWhenPossible(true);
		checkSymmetric(pairing);
		//将变量r初始化为Zr中的元素
		Zr = pairing.getZr();
		r = Zr.newElement();
		//将变量Ppub,Qu,Su,V初始化为G1中的元素,G1是加法群
		G1 = pairing.getG1();
		Ppub = G1.newElement();
		Qu = G1.newElement();
		Su = G1.newElement();
		V = G1.newElement();
		//将变量T1,T2V初始化为GT中的元素,GT是乘法群
		Field GT = pairing.getGT();
		T1 = GT.newElement();
		T2 = GT.newElement();
	}

	/**
	 * 判断配对是否为对称配对,不对称则输出错误信息
	 * 
	 * @param pairing
	 */
	private void checkSymmetric(Pairing pairing) {
		if (!pairing.isSymmetric()) {
			throw new RuntimeException("密钥不对称!");
		}
	}

	@Override
	public void buildSystem() {
		System.out.println("-------------------系统建立阶段----------------------");
		s = Zr.newRandomElement().getImmutable();// //随机生成主密钥s
		P = G1.newRandomElement().getImmutable();// 生成G1的生成元P
		Ppub = P.mulZn(s);// 计算Ppub=sP,注意顺序
		System.out.println("P=" + P);
		System.out.println("s=" + s);
		System.out.println("Ppub=" + Ppub);
	}

	@Override
	public void extractSecretKey() {
		System.out.println("-------------------密钥提取阶段----------------------");
		Qu = pairing.getG1().newElement().setFromHash("IDu".getBytes(), 0, 3)
				.getImmutable();// //从长度为3的Hash值IDu确定用户U产生的公钥Qu
		Su = Qu.mulZn(s).getImmutable();
		System.out.println("Qu=" + Qu);
		System.out.println("Su=" + Su);
	}

	@Override
	public void encrypt() {
		System.out.println("-------------------加密阶段----------------------");
		r = Zr.newRandomElement().getImmutable();
		V = P.mulZn(r);
		T1 = pairing.pairing(Ppub, Qu).getImmutable();// 计算e(Ppub,Qu)
		T1 = T1.powZn(r).getImmutable();
		System.out.println("r=" + r);
		System.out.println("V=" + V);
		System.out.println("T1=e(Ppub,Qu)^r=" + T1);
	}

	@Override
	public void decrypt() {
		System.out.println("-------------------解密阶段----------------------");
		T2 = pairing.pairing(V, Su).getImmutable();
		System.out.println("e(V,Su)=" + T2);
		int byt = V.getLengthInBytes();// 求V的字节长度,假设消息长度为128字节
		System.out.println("文本长度" + (byt + 128));
	}

	public static void main(String[] args) {
		BasicIdent2 ident = new BasicIdent2();
		// 动态代理,统计各个方法耗时
		Ident identProxy = (Ident) Proxy.newProxyInstance(
				BasicIdent2.class.getClassLoader(),
				new Class[] { Ident.class }, new TimeCountProxyHandle(ident));

		identProxy.buildSystem();
		identProxy.extractSecretKey();
		identProxy.encrypt();
		identProxy.decrypt();
	}

}
最后使用了动态代理(java自带的代理模式)用来统计各个阶段的耗时。
package cn.edu.zjut.pbc;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 时间统计处理机,用于统计各方法耗时
 * @author Administrator
 *
 */
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;
    }
}
运行结果展示

由于电脑配置的不同,运行耗时会有差异,大家对于这篇博客如有任何问题,欢迎在CSDN上与我讨论。

 

                
  • 19
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
MIRACL库提供了实现双线性对计算的函数。下面是一个简单的示例代码,用于计算双线性对: ```c #include <miracl.h> int main() { // 初始化MIRACL库 miracl *mip = mirsys(1000, 16); mip->IOBASE = 16; // 定义双线性对参数 big a, b, p, n; epoint *G1, *G2; ecurve *C1, *C2; big order; // 初始化参数 a = mirvar(0); b = mirvar(3); p = mirvar("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF"); n = mirvar("400000000000000000002BEC12BE2262D39BCF14D"); order = mirvar("400000000000000000002BEC12BE2262D39BCF14B"); G1 = epoint_init(); G2 = epoint_init(); C1 = ecurve_init(); C2 = ecurve_init(); // 设置参数 ecurve_init(a, b, p, MR_PROJECTIVE); epoint_set(MIRACL_INFINITY, 0, G1); epoint_set(MIRACL_INFINITY, 0, G2); ecurve_mult(n, G1, G1); ecurve_mult(n, G2, G2); ecurve_copy(C1, &ecurve); ecurve_copy(C2, &ecurve2); // 计算双线性对 big x, y; epoint *P = epoint_init(); epoint *Q = epoint_init(); epoint_set(x, y, 0, P); epoint_set(x, y, 0, Q); big r = mirvar(0); pairing(P, Q, r); // 输出结果 cotnum(r, stdout); // 释放资源 mirkill(a); mirkill(b); mirkill(p); mirkill(n); mirkill(order); epoint_free(P); epoint_free(Q); epoint_free(G1); epoint_free(G2); ecurve_free(C1); ecurve_free(C2); mirsys_end(); return 0; } ``` 在上面的示例代码中,我们首先初始化MIRACL库,并定义了双线性对所需的参数。然后我们使用`ecurve_init()`函数初始化了椭圆曲线,使用`epoint_set()`函数设置了点坐标,最后使用`pairing()`函数计算双线性对。最后我们使用`cotnum()`函数将结果输出到控制台,并释放了所有资源。 需要注意的是,在使用MIRACL库时,必须先初始化MIRACL库,并在使用完之后释放所有资源,否则可能导致内存泄漏等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值