ECC加密
一,实验目的通过使用JPBC库调用ECC椭圆曲线加解密算法,能够编写简单的实验代码进行正确的ECC加密和解密。二,实验要求
- 熟悉的ECC椭圆曲线加解密算法。2。了解如何使用Java简单实现ECC椭圆曲线加密算法。3。掌握三阶椭圆曲线加密算法的简单代码实验。4。熟悉的JPBC密码学库。三,开发环境 JDK1.8,Java相关开发环境(本实验以Windows平台为例)四,实验内容 需要约会相关的jpbc包【1-1】ECC椭圆曲线加密实验1,给定一个弦类型的参数,使用如下的函数进行系统初始化:
java“ public static Pairing initPairing(字符串参数){系统。出来。println(“系统正在传送椭圆形曲线的相关参数……”));;;; //读入参数配对对=PairingFactory。getPairing(参数);系统。出来。println(“系统已经导入完成”);返回配对;}公共静态串口initG_1()。
配对配对){系统出来.println(“系统正在产生椭圆曲线…”); //读入参数变量G1 =对.getG1();系统。出来。println(“系统已经产生椭圆曲线”);返回G1;}公共静态元素initG(插入G1){系统。出来。println(“系统正在选择生成元点G……”
); // RandomElement()。getImmutable(); //选择生成元点G元素G = G1。系统。出来。println(“系统已经挑选好生成元点G”);返回G;}公共静态元素initP_t (分区G1){系统。出来。println(“系统正在选择随机点P_t……”))))));; //选择随机点P_t元素P_t = G1
。newRandomElement()。getImmutable(); 系统。出来。println(“系统已经选择好随机点P_t”);返回P_t;} `
2,给定的字符串类型的密钥和待加密消息,使用如下的函数进行初始化密钥:
//生成公私钥对
public static void KeyGenerator(Element G,Field G_1) throws Exception {
//随机选择一个整数n_b,作为私钥
Random random = new Random();
BigInteger n_b = new BigInteger(160, 160,random);//大整数作为私钥1
//构造公钥P_b
Element P_b = G.duplicate().mul(n_b);
byte[] b = P_b.toCanonicalRepresentation();
System.out.println("\n");
//把公钥和私钥分别存储在publicKey.key和privateKey.key文件里
String path = new File("").getCanonicalPath();
out(path + "\\privateKey.key", n_b.toString());
out(path + "\\publicKey.key",
Base64.getEncoder().encodeToString(P_b.toCanonicalRepresentation())
+ ",,,,,," + Base64.getEncoder().encodeToString(G.toCanonicalRepresentation()));
System.out.println("你的私钥存放在:" + path + "\\privateKey.key");
System.out.println("你的公钥存放在:" + path + "\\publicKey.key");
}
3、 使用如下的函数实现加密操作:
//加密算法
public static String encrypt(Element P_b, String data, int k, Element P_t, Element G){
try {
byte[] datasource=data.getBytes("utf8");
String CArray = "A";
//计算P_1
Element P_1 = G.duplicate().getImmutable().mul(k);
System.out.println("加密过程中计算出的P_1:"+ P_1);
//计算P_2
Element P_2 = P_b.duplicate().getImmutable().mul(k);
System.out.println("加密过程中计算出的P_2:"+ P_2);
//计算P_end
Element P_end = P_t.add(P_2);
System.out.println("加密过程中计算出的P_end:"+ P_end);
//计算密文C
String[] p_txy = P_t.toString().split(",");
BigInteger p_tx = new BigInteger(p_txy[0]);
BigInteger p_ty = new BigInteger(p_txy[1]);
for(int i=0;i<datasource.length;i++)
{
BigInteger M = new BigInteger(datasource[i]+"");
BigInteger C_mid = M.multiply(p_tx).add(p_ty);
CArray = CArray +","+C_mid.toString();
}
CArray = CArray + ",,"+
Base64.getEncoder().encodeToString(P_1.toCanonicalRepresentation())+",,"+
Base64.getEncoder().encodeToString(P_end.toCanonicalRepresentation());
return Base64.getEncoder().encodeToString(CArray.getBytes());
}
catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
4、 给定String类型的密钥和待解密的密文,使用如下的函数进行解密:
public static String decrypt(BigInteger Privatekey, String data, Field G_1,Element G) {
try {
String ciphertext= new String(Base64.getDecoder().decode(data),"utf8");
//分解密文
String[] CS=ciphertext.split(",,");
String m = "";
//取出P_t+kP_b
Element P_end = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[2]));
//取出P_1
Element P_1 = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[1]));
//计算P_t
Element P_t = P_end.getImmutable().sub(P_1.getImmutable().mul(Privatekey));
System.out.println("解密过程中计算出的P_t:"+ P_t);
//计算明文M
String[] p_txy = P_t.toString().split(",");
BigInteger p_tx = new BigInteger(p_txy[0]);
BigInteger p_ty = new BigInteger(p_txy[1]);
//取出密文c
String[] Plaintext = CS[0].split(",");
for(int i=1;i<Plaintext.length;i++){
BigInteger C = new BigInteger(Plaintext[i]);
BigInteger M_mid = C.subtract(p_ty).divide(p_tx);
m = m+new String(M_mid.toByteArray(),"GBK");;
}
return m;
}
catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
全部代码
java导入它。南澳大学。直径。气。jpbc 。* ; 导入它。南澳大学。直径。气。af 。jpbc 。配对。PairingFactory ; 导入java 。io 。* ; 导入java 。数学。BigInteger ; 导入java 。实用程序。* ; 上市
class ECC {
public static Pairing initPairing(String parameter) {
System.out.println(“系统正在导入椭圆曲线的相关参数……”);
//读入参数
Pairing pairing = PairingFactory.getPairing(parameter);
System.out.println(“系统已经导入完毕”);
return pairing;
}
public static Field initG_1(Pairing pairing) {
System.out.println(“系统正在产生椭圆曲线……”);
//读入参数
Field G1 = pairing.getG1();
System.out.println(“系统已经产生椭圆曲线”);
return G1;
}
public static Element initG(Field G1) {
System.out.println(“系统正在挑选生成元点G……”);
//挑选生成元点G
Element G = G1.newRandomElement().getImmutable();
System.out.println(“系统已经挑选好生成元点G”);
return G;
}
public static Element initP_t(Field G1) {
System.out.println("系统正在挑选随机点P_t……");
//挑选随机点P_t
Element P_t = G1.newRandomElement().getImmutable();
System.out.println("系统已经挑选好随机点P_t");
return P_t;
}
//生成公私钥对
public static void KeyGenerator(Element G,Field G_1) throws Exception {
//随机选择一个整数n_b,作为私钥
Random random = new Random();
的BigInteger N_B = 新 的BigInteger (160 , 160 ,随机); //大整数作为私钥1 //构造公钥P_B 元P_B = g ^ 。复制()。mul ( n_b ); 字节[ ] b = P_b 。toCanonicalRepresentation (); 系统。出来。println (“ \ n” );
//把公钥和私钥分别存储在publicKey.key和privateKey.key文件里
String path = new File("").getCanonicalPath();
out(path + "\\privateKey.key", n_b.toString());
out(path + "\\publicKey.key",
Base64.getEncoder().encodeToString(P_b.toCanonicalRepresentation()) + ",,,,,," +
Base64.getEncoder().encodeToString(G.toCanonicalRepresentation()));
System.out.println("你的私钥存放在:" + path + "\\privateKey.key");
System.out.println("你的公钥存放在:" + path + "\\publicKey.key");
}
//加密算法
public static String encrypt(Element P_b, String data, BigInteger k, Element P_t, Element G){
try {
byte[] datasource=data.getBytes("utf8");
String CArray = "A";
//计算P_1
Element P_1 = G.duplicate().getImmutable().mul(k);
系统。出来。println (“加密过程中计算出的P_1:” + P_1 );//计算P_2 元素P_2 = P_b 。复制()。getImmutable ()。多( k ); 系统。出来。println (“加密过程中计算出的P_2:” + P_2 );//计算P_end 元素P_end = P_t 。添加(
P_2 );
系统。出来。println (“加密过程中计算出的P_end:” + P_end );//计算密文C String [ ] p_txy = P_t 。toString ()。split (“,” ); BigInteger p_tx =新的BigInteger ( p_txy [ 0 ] ); BigInteger p_ty =新的BigInteger ( p_txy
[1]);
for(int i=0;i<datasource.length;i++)
{
BigInteger M = new BigInteger(datasource[i]+"");
BigInteger C_mid = M.multiply(p_tx).add(p_ty);
CArray = CArray +","+C_mid.toString();
}
CArray = CArray + “,”+
Base64.getEncoder().encodeToString(P_1.toCanonicalRepresentation())+","+
Base64.getEncoder().encodeToString(P_end.toCanonicalRepresentation()); 返回Base64 。getEncoder ()。encodeToString ( CARRAY 。的getBytes ()); } catch (例外ex ){ ex 。printStackTrace (); }返回空值; }公共静态字符串解密( BigInteger专用密钥,字符串数据,字段G_1 ,元素G ){试试
{
String ciphertext= new String(Base64.getDecoder().decode(data),"utf8");
//分解密文
String[] CS=ciphertext.split(",,");
String m = "";
//取出P_t+kP_b
Element P_end = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[2]));
//取出P_1
Element P_1 = G_1.newElementFromBytes(Base64.getDecoder().decode(CS[1]));
//计算P_t
Element P_t = P_end.getImmutable().sub(P_1.getImmutable().mul(Privatekey));
System.out.println("解密过程中计算出的P_t:"+ P_t);
//计算明文M
String[] p_txy = P_t.toString().split(",");
BigInteger p_tx = new BigInteger(p_txy[0]);
BigInteger p_ty = new BigInteger(p_txy[1]);
//取出密文c
String[] Plaintext = CS[0].split(",");
for(int i=1;i<Plaintext.length;i++){
BigInteger C = new BigInteger(Plaintext[i]);
的BigInteger M_mid = Ç 。减去( p_ty )。除( p_tx );
米=米+新 字符串( M_mid 。toByteArray (),“GBK” ); ; } return m ; } catch (例外ex ){ ex 。printStackTrace (); }回空
; } //封装输出流public static void out ( String path , String val ){试试{ val = Base64 。getEncoder ()。encodeToString ( VAL 。的getBytes (“UTF8” )) ; FileWriter fw =新的FileWriter (路径); BufferedWriter bw =新的BufferedWriter ( fw )
; PrintWriter输出=新的PrintWriter(bw);出局。println(val);出局。冲洗();出局。关闭();} catch(例外ex){ex。printStackTrace();}} //从文件中读公钥public staticElementreadPk(字符串路径,分段G_1){元素sk = null; 尝试{文件f =新文件(路径);FileReader fr =新的FileReader(f); BufferedReader br =新的BufferedReader(fr);字符串line = null; StringBuffer sb = new StringBuffer(); =空){字节[] b = Base64的。getDecoder()。解码(行);而((((线= BR。的readLine())!!!分割(“ ,,,,);字节[] a =Base64。getDecoder()。decode(键[0]); System.out.println( “ \ n”); if(key.length == 2){sk = G_1.newElementFromBytes(a);}其他{ 引发新异常(“文件错误”); } }
br。关闭(); 返回sk; } catch(例外ex){ex。printStackTrace();} return sk; } //从文件中读取私钥公共静态BigInteger readSk(字符串路径,字符串G_1){BigInteger sk = null; 尝试{文件f =新文件(
路径); FileReader fr =新的FileReader(f); BufferedReader br =新的BufferedReader(fr);字符串line = null; StringBuffer sb = new StringBuffer(); 解码(行); while((line = br.readLine())!= null){字节[] b =Base64。getDecoder()。String []键=新的String(b)。split(“ ,) ; if(键。长度== 1){sk =新BigInteger(键[0]);}其他{引发新异常(“文件错误”));}} br。关闭();返回sk; } catch(例外ex){ex。printStackTrace();} return sk; } public static void main(String [] args){试试{BigInteger k = new
BigInteger(10,新的Random()); //执行系统初始化配对配对= initPairing(“ C:\用户\ 89763 \桌面\ a.properties”);; G_1 = initG_1(配对);元素G = initG(G_1);元素P_t = initP_t(G_1); //构造公私钥KeyGenerator(G,G_1);扫描器SC =新扫描仪(系统。在);字符串str =“”;打印(“ \ n” +“请输入公钥地址按回车结束:”);sc。useDelimiter(“ \ n”);系统。出来。如果(SC。hasNext()){STR =SC。next();} //获取公钥元公钥= readPk(STR。子串(0,STR。长度()-1),G_1);打印(“ \ n” +“请输入需要加密的文字按回车结束:” );如果(SC。hasNext()){STR = SC。下一个()
; } //加密明文串c =加密(公钥,STR,ķ,P_T,G ^); println(“ \ n” +“加密结果:” + c +“ \ n”);系统。出来。输入私钥地址按回车可对密文数据进行解密:”;如果(SC。hasNext()){str = sc.next();系统。} //获得私钥BigInteger privateKey = readSk(str.substring (0,str.length()-1),G_1); //解密密文字串m =解密(privateKey,c,G_1,G); System.out.println(“ \ n” +“解密明文:” + m +“ \ n”); sc.close();} catch(ex ex){ex.printStackTrace();}}
}
注:相关jpbc导入方法![在这里插入图片描述(https://www.icode9.com/i/ll/?i=20200606165228398.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MDU2MjE2, size_16,color_FFFFFF,t_70)![在这里插入图片描述(https://www.icode9.com/i/ll/?i=20200606165235943.jpg?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9 t_70)相关JPBC文件已上传,自行解压,其中
的java的配对的配对= initPairing(“ C:\用户\ 89763个\桌面\ a.properties”);
`中为您保存a.properties的文件位置