JPBC库基于椭圆曲线的Elgamal算法实现(java)

1.算法原理
(原理图片直接截的知乎ID徐摆渡)
在这里插入图片描述2.代码实现

package csdn;

import java.math.BigInteger;

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.jpbc.PairingParameters;
import it.unisa.dia.gas.jpbc.Point;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import it.unisa.dia.gas.plaf.jpbc.pairing.a.TypeACurveGenerator;
import it.unisa.dia.gas.plaf.jpbc.pbc.curve.PBCTypeACurveGenerator;
class Cipher{
	//定义密文类型
	Element X;
	Element c;
}
class Algorithm{
	Element s,P,Q;
	Field G1,G2;
	 public void keygen() {
			Pairing bp=PairingFactory.getPairing("a.properties");		 
			G1=bp.getG1();
			G2=bp.getZr();
			//e= new EllipticCurve(new secp256r1());
			BigInteger n=G2.getOrder();//椭圆曲线阶
			Element G=G1.newRandomElement(); //椭圆曲线生成元
			s=G2.newRandomElement();//私钥
			P=G1.newRandomElement();//公钥
			Q=P.duplicate().powZn(s);//公钥
			System.out.println("public key is:"+P+" "+Q);
			System.out.println("secret key is:"+s);
	   }
	 public Cipher enc(BigInteger m) {
		 Cipher ci=new Cipher();
		 Element r=G2.newRandomElement();
		 Element X1=P.duplicate().powZn(r);
		 Element X2=Q.duplicate().powZn(r);
		 Point X=(Point)X2;
		 Element c=X.getX().duplicate().mul(m);
		 ci.c=c;
		 ci.X=X1;
		 return ci;
	 }
	 public void Dec(Cipher ci) {
		 Element X3=ci.X.duplicate().powZn(s);
		 Point x=(Point)X3;
		 Element m=x.getX().duplicate().invert().mul(ci.c);
		 System.out.println("after decryption:"+m);
	 }
}
public class Elagamal {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Algorithm A=new Algorithm();
		A.keygen();
		BigInteger m=new BigInteger("178531257647819");//需要加密的明文,是一个大整数
		Cipher c=A.enc(m);
		A.Dec(c);
	}

}

3.运算结果

public key is:6147642565949958508479793805198530026337706910044571940373701624048606296621639659716770523799809818875700108141306425063776316632389673692533068805432397,5807892737884636779977288008610194764293234855900208907484823780615667829992763728251242120523928614556036032284795094479936945398472193932456161925838585,0 2723377172141760623381345874347733744508532255110023260231320062964219922778033315396154860555909057963519888211088113400117660242449632763135899929101514,1201299932957086649327230028748357924226747702090875676399666620185245374107300800622569227242992973861259877487340411144395443748135070677427480394933416,0
secret key is:243278257380795219406081348922159421043848811809
after decryption:178531257647819
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅逼码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值