eclipse实现Schnorr数字签名

网友投稿 303 2022-12-03


eclipse实现Schnorr数字签名

Schnorr数字签名,供大家参考,具体内容如下

一、实验目的

学习Schnorr算法在数字签名方面的使用,掌握公钥签名中最基础的签名算法-Schnorr数字签名算法的编写。

二、实验要求

1. 熟悉Schnorr算法的描述,已经其使用场景。

2. 熟悉Schnorr数字签名算法。

3. 掌握如何使用java语言,实现Schnorr签名算法。

三、开发环境/p>

JDK1.8,eclipse。

四、实验原理

数字签名是指消息发送方利用特定参数产生的一段消息码,该消息码可以用来标识消息发送者真实身份,同时可以标识发送的数据的完整性,所以在一定程度防止了发送的数据在发送过程中被攻击者篡改的行为。简而言之,数字签名就是由消息发送者利用身份信息与消息相结合产生的一个消息摘要。

在数字签名过程中核心的两个步骤就是产生签名信息和对签名信息的验证,产生签名就是消息发送方使用特定的签名算法将数据产生成消息摘要,同时使用私钥对其摘要进行加密,最后将加密后的密文和原始的数据一起发送。消息接收方收到信息后,首先用发送者的公钥来算数据的摘要,然后把此结果与收到的摘要对比,如果一致,则验证通过,认为该消息是真实有效的。反之无效,丢弃该消息。过程如下图1所示。

a) 数字签名加密过程

b) 数字签名解密验证过程

图1 数字签名过程

Schnorr签名算法是由德国数学家、密码学家Claus Schnorr提出,是Elgamal签名方案的变种。

具体步骤如下:

首先是生成公钥/私钥对,过程如下:

a. 选择素数 和 ,其中 是 的素因子;

b. 选择一个整数 ,使得 ; 、 和 构成全局公钥参数,在用户组内的每个用户都可以使用;

c. 选择随机整数 作为用户的私钥,其中 ;

d. 计算公钥 ;

对于密钥对为 的用户,通过以下过程产生签名:

a. 选择随机整数,计算 ;

b. 将 附在消息后面,一起计算Hash值 :

c. 计算。

签名对为 ,其他用户通过以为过程来验证签名:

a. 计算;

b. 验证等式 是否成立。

代码段:

SchnorrSignature

GetProjectPath:

import java.io.File;

/**

* @ClassName: GetProjectPath

* @date: 2020年6月16日 下午10:58:53

* @Description: 获取项目绝对路径

*/

public class GetProjectPath {

public static String getPath() {

File directory = new File("");

String courseFile = null;

try {

courseFile = directory.getCanonicalPath().replace("\\", "/");

}catch (Exception e) {

e.printStackTrace();

}

return courseFile;

}

}

KeyPairOperate

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

/**

* @ClassName: KeyPairOperate

* @date: 2020年6月16日 下午9:53:11

* @Description: 密钥对的管理与存储

*/

public class KeyPairOperate {

public static String getFileName(String path) {

int indexOf = path.lastIndexOf("\\")+1;

int last = path.lastIndexOf(".");

return path.substring(indexOf, last);

}

/**

* @Description: 工具类,数组转集合

* @param para

* @return

* @Return:List

* @Date:上午10:24:33

*/

public static List transArryToLi(String[] para){

List li = new ArrayList();

for(int i=0; i

li.add(para[i]);

}

return li;

}

/**

* @Description: 获取配置文件参数

* @param path : 文件路径

* @param para : 获取参数的key

* @Date:上午9:46:26

*/

public static String getDataFromFile(String path, String key) {

String para = null;

try {

Properties pro = new Properties();

pro.load(new FileInputStream(path));

para = (String) pro.get(key);

} catch (Exception e) {

e.printStackTrace();

}

return para;

}

/**

* @Description:写公共密钥到文件 -- 公钥证书列表

* @param path : 存储的路径

* @param param : 参数

* @param flag : 是否是追加存储,公共参数不是追加,公钥是追加; 追加: true, 覆盖: flase

* @Return:void

* @Date:上午10:20:25

*/

public static void writePublicKeyToFile(String path, List param, Boolean flag) {

try {

PrintWriter printWriter = new PrintWriter(new FileWriter(path,flag));

for(String element : param) {

printWriter.println(element);

}

printWriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Shtest

import org.junit.Test;

public class Shtest {

String pathFile ="C:\\Users\\89763\\Desktop\\rtf";

@Test

public void initPara() {

SchnorrSignature.initPara(12);

}

@TCWetqrmKGNest

public void generateKeyForUser() {

SchnorrSignature.generateKeyForUser("xiaoming");

SchnorrSignature.generateKeyForUser("xiaowang");

}

@Test

public void makeSign() {

SchnorrSignature.makeSign(pathFile,"xiaoming");

SchnorrSignature.makeSign(pathFile,"xiaowang");

}

@Test

public void checkSign() {

SchnorrSignature.checkSign( pathFile ,"xiaoming");

SchnorrSignature.checkSign( pathFile ,"xiaowang");

}

}

li.add(para[i]);

}

return li;

}

/**

* @Description: 获取配置文件参数

* @param path : 文件路径

* @param para : 获取参数的key

* @Date:上午9:46:26

*/

public static String getDataFromFile(String path, String key) {

String para = null;

try {

Properties pro = new Properties();

pro.load(new FileInputStream(path));

para = (String) pro.get(key);

} catch (Exception e) {

e.printStackTrace();

}

return para;

}

/**

* @Description:写公共密钥到文件 -- 公钥证书列表

* @param path : 存储的路径

* @param param : 参数

* @param flag : 是否是追加存储,公共参数不是追加,公钥是追加; 追加: true, 覆盖: flase

* @Return:void

* @Date:上午10:20:25

*/

public static void writePublicKeyToFile(String path, List param, Boolean flag) {

try {

PrintWriter printWriter = new PrintWriter(new FileWriter(path,flag));

for(String element : param) {

printWriter.println(element);

}

printWriter.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Shtest

import org.junit.Test;

public class Shtest {

String pathFile ="C:\\Users\\89763\\Desktop\\rtf";

@Test

public void initPara() {

SchnorrSignature.initPara(12);

}

@TCWetqrmKGNest

public void generateKeyForUser() {

SchnorrSignature.generateKeyForUser("xiaoming");

SchnorrSignature.generateKeyForUser("xiaowang");

}

@Test

public void makeSign() {

SchnorrSignature.makeSign(pathFile,"xiaoming");

SchnorrSignature.makeSign(pathFile,"xiaowang");

}

@Test

public void checkSign() {

SchnorrSignature.checkSign( pathFile ,"xiaoming");

SchnorrSignature.checkSign( pathFile ,"xiaowang");

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot编写拦截器教程实例解析
下一篇:详解maven依赖冲突以及解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~