Skip to content

Commit

Permalink
feat: 添加前端界面
Browse files Browse the repository at this point in the history
Took 13 hours 14 minutes
  • Loading branch information
catmade committed Sep 20, 2020
1 parent 3b5d850 commit 5a7d5c5
Show file tree
Hide file tree
Showing 18 changed files with 488 additions and 60 deletions.
31 changes: 29 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.py7hon</groupId>
<artifactId>ThresholdScheme</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -19,5 +32,19 @@
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Binary file added readme-image/image-20200920221204227.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-image/image-20200920221222770.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-image/image-20200920221240858.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-image/image-20200920221308615.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-image/image-20200920221337291.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added readme-image/image-20200920221355462.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,26 @@ public class Piece {



## 一、Shamir(k, n)门限方案
## 一、界面效果

- 输入参数

![image-20200920221204227](readme-image/image-20200920221204227.png)

- 生成密钥
![image-20200920221222770](readme-image/image-20200920221222770.png)

- 选择“恢复密钥“选项卡
![image-20200920221240858](readme-image/image-20200920221240858.png)

- 点击”获取分片”
![image-20200920221308615](readme-image/image-20200920221308615.png)
- 保留任意 3 个分片,可以正常恢复出密钥
![image-20200920221337291](readme-image/image-20200920221337291.png)
- 保留任意两个分片,不能准确恢复密钥
![image-20200920221355462](readme-image/image-20200920221355462.png)

## 二、Shamir(k, n)门限方案

> 参考:
>
Expand Down Expand Up @@ -176,4 +195,4 @@ public long restoreSecretKey(Piece[] pieces, long mod) {
}
```

## 、基于中国剩余定理的(k, n)门限方案
## 、基于中国剩余定理的(k, n)门限方案
26 changes: 26 additions & 0 deletions src/main/java/com/py7hon/ThresholdSchemeApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.py7hon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* 启动类
*
* @author Seven
* @version 1.0
* @date 2020/9/19 22:52
*/
@SpringBootApplication
@Controller
public class ThresholdSchemeApplication {
public static void main(String[] args) {
SpringApplication.run(ThresholdSchemeApplication.class, args);
}

@RequestMapping({"/", "/home", "/index"})
public String home() {
return "index";
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/py7hon/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.py7hon.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* 安全配置
*
* @author Seven
* @version 1.0
* @date 2020/9/13 22:57
*/
@Configuration
public class SecurityConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
// .allowedOrigins("http://127.0.0.1:5500", "https://127.0.0.1:*")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
76 changes: 76 additions & 0 deletions src/main/java/com/py7hon/controller/ThresholdSchemeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.py7hon.controller;

import com.py7hon.entity.Piece;
import com.py7hon.threshold.scheme.ThresholdScheme;
import com.py7hon.threshold.scheme.chinese.remainder.theorem.ChineseRemainderTheorem;
import com.py7hon.threshold.scheme.shamir.Shamir;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 门限方案接口
*
* @author Seven
* @version 1.0
* @date 2020/9/19 23:10
*/
@RestController
public class ThresholdSchemeController {

@GetMapping("/api/generate-piece/{type}")
public ResponseEntity<?> genPiece(
@PathVariable("type") String type,
@RequestParam("secretKey") long secretKey,
@RequestParam("totalPieceNumber") int totalPieceNumber,
@RequestParam("minEffectivePieceNumber") int minEffectivePieceNumber,
@RequestParam("mod") long mod
) {
ThresholdScheme thresholdScheme = getThresholdScheme(type);

if (thresholdScheme == null) {
return ResponseEntity.badRequest().build();
}

Piece[] pieces = thresholdScheme.genPieces(secretKey, totalPieceNumber, minEffectivePieceNumber, mod);
return ResponseEntity.ok(pieces);
}

@RequestMapping("/api/restore-secret-key/{type}")
public ResponseEntity<?> restoreSecretKey(
@PathVariable("type") String type,
@RequestParam("mod") long mod,
@RequestBody List<Piece> pieces
) {
ThresholdScheme thresholdScheme = getThresholdScheme(type);

if (thresholdScheme == null) {
return ResponseEntity.badRequest().build();
}

long secretKey = thresholdScheme.restoreSecretKey(pieces.toArray(new Piece[0]), 1, 1, mod);
return ResponseEntity.ok(secretKey);
}

/**
* 根据类别获取门限方案
*
* @param type 类别。shamir 或 chinese-remainder
* @return 门限方案
*/
private ThresholdScheme getThresholdScheme(String type) {
if (type == null) {
return null;
}

switch (type) {
case "shamir":
return new Shamir();
case "chinese-remainder":
return new ChineseRemainderTheorem();
default:
return null;
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/py7hon/entity/Piece.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.py7hon.entity;

import lombok.Data;

/**
* 块。将密钥按照某种规则分解成的密码片。对应于多项式的坐标,index表示横坐标,value表示纵坐标
*
* @author Seven
* @version 1.0
* @date 2020-09-14 22:18
*/
@Data
public class Piece {
/**
* 块下标,从 1 开始
*/
private int index;

/**
* 块存储的数值
*/
private long value;

public Piece() {
}

/**
* 构造器
*
* @param index 块下标
* @param value 块存储的数值
*/
public Piece(int index, long value) {
this.index = index;
this.value = value;
}
}
48 changes: 12 additions & 36 deletions src/main/java/com/py7hon/threshold/scheme/ThresholdScheme.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.py7hon.threshold.scheme;

import lombok.Data;
import com.py7hon.entity.Piece;

/**
* 定义了门限方案的一些方法
Expand All @@ -11,55 +11,31 @@
*/
public interface ThresholdScheme {
/**
* 计算分片
* 计算分片。
* <p>调用说明:</p>
* <P>1. 确定好要进行分片的密钥:secretKey([0, Long.MAX_VALUE])</P>
* <p>2. 确定好分片数:totalPieceNumber ([0, Integer.MAX_VALUE])</p>
* <p>3. 确定最小有效分片数:minEffectivePieceNumber ([0, totalPieceNumber])</p>
* <p>4. 任取一个大于的素数:mod (mod > <code>totalPieceNumber</code> 且 mod > <code>secretKey</code> ),用作模运算的模</p>
* <p>5. 调用方法即可得到分片。任选 n (n >= minEffectivePieceNumber)个分片即可还原出密钥</p><br>
*
* @param secretKey 密钥
* @param totalPieceNumber 总分片数(n)
* @param minEffectiveSliceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥
* @param minEffectivePieceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥
* @param mod 模,所有的运算将在此模下进行。素数,大于 <code>totalPieceNumber</code> 和 <code>secretKey</code>
* @return 分片后的数据
*/
Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectiveSliceNumber, long mod);
Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectivePieceNumber, long mod);

/**
* 根据部分分片,尝试还原出密钥
*
* @param pieces 部分分片
* @param totalPieceNumber 总分片数(n)
* @param minEffectiveSliceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥
* @param minEffectivePieceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥
* @param mod 模,所有的运算将在此模下进行。素数,大于 <code>totalPieceNumber</code> 和 <code>secretKey</code>
* @return 还原出的密钥,如果分片不充分,则不能还原出正确的密钥
*/
long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectiveSliceNumber, long mod);
long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectivePieceNumber, long mod);

/**
* 块。将密钥按照某种规则分解成的密码片。对应于多项式的坐标,index表示横坐标,value表示纵坐标
*
* @author Seven
* @version 1.0
* @date 2020-09-14 22:18
*/
@Data
public class Piece {
/**
* 块下标,从 1 开始
*/
private int index;

/**
* 块存储的数值
*/
private long value;

/**
* 构造器
*
* @param index 块下标
* @param value 块存储的数值
*/
public Piece(int index, long value) {
this.index = index;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.py7hon.threshold.scheme.chinese.remainder.theorem;


import com.py7hon.entity.Piece;
import com.py7hon.threshold.scheme.ThresholdScheme;

/**
Expand All @@ -13,12 +14,12 @@
public class ChineseRemainderTheorem implements ThresholdScheme {

@Override
public Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectiveSliceNumber, long mod) {
public Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectivePieceNumber, long mod) {
return new Piece[0];
}

@Override
public long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectiveSliceNumber, long mod) {
public long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectivePieceNumber, long mod) {
return 0;
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/py7hon/threshold/scheme/shamir/Shamir.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.py7hon.threshold.scheme.shamir;

import com.py7hon.entity.Piece;
import com.py7hon.threshold.scheme.ThresholdScheme;
import org.junit.jupiter.api.Test;

Expand All @@ -21,10 +22,10 @@ public class Shamir implements ThresholdScheme {
// region ============= 生成密码片 =============

@Override
public Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectiveSliceNumber, long mod) {
public Piece[] genPieces(long secretKey, int totalPieceNumber, int minEffectivePieceNumber, long mod) {
// 1. 随机生成多项式的系数。多项式的项数为:totalPieceNumber - minEffectiveSliceNumber
// 系数,下标 0 表示 x^1 的系数,下标 n 表示 x^(n + 1) 的系数
long[] coefficients = new long[totalPieceNumber - minEffectiveSliceNumber];
long[] coefficients = new long[totalPieceNumber - minEffectivePieceNumber];
// 生成随机数,取值范围为 [0, mod)
genRandomNum(coefficients, mod);

Expand Down Expand Up @@ -111,7 +112,7 @@ private void genRandomNum(long[] coefficients, long bound) {
// region ============= 通过密码片解析出密码 =============

@Override
public long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectiveSliceNumber, long mod) {
public long restoreSecretKey(Piece[] pieces, int totalPieceNumber, int minEffectivePieceNumber, long mod) {
// 使用欧几里得插值多项式求解 x = 0 时多项式的值
long x = 0;
// 这里使用 double 是为了防止 long 数值溢出
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8089
Loading

0 comments on commit 5a7d5c5

Please sign in to comment.