diff --git a/pom.xml b/pom.xml index ea19e82..77246eb 100644 --- a/pom.xml +++ b/pom.xml @@ -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"> 4.0.0 - + + org.springframework.boot + spring-boot-starter-parent + 2.3.2.RELEASE + com.py7hon ThresholdScheme - 1.0-SNAPSHOT + 1.0 + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + org.projectlombok lombok @@ -19,5 +32,19 @@ junit-jupiter-api 5.6.2 + + com.alibaba + fastjson + 1.2.71 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + \ No newline at end of file diff --git a/readme-image/image-20200920221204227.png b/readme-image/image-20200920221204227.png new file mode 100644 index 0000000..246f123 Binary files /dev/null and b/readme-image/image-20200920221204227.png differ diff --git a/readme-image/image-20200920221222770.png b/readme-image/image-20200920221222770.png new file mode 100644 index 0000000..3bb02c3 Binary files /dev/null and b/readme-image/image-20200920221222770.png differ diff --git a/readme-image/image-20200920221240858.png b/readme-image/image-20200920221240858.png new file mode 100644 index 0000000..bc7aa2f Binary files /dev/null and b/readme-image/image-20200920221240858.png differ diff --git a/readme-image/image-20200920221308615.png b/readme-image/image-20200920221308615.png new file mode 100644 index 0000000..1809039 Binary files /dev/null and b/readme-image/image-20200920221308615.png differ diff --git a/readme-image/image-20200920221337291.png b/readme-image/image-20200920221337291.png new file mode 100644 index 0000000..02ef48c Binary files /dev/null and b/readme-image/image-20200920221337291.png differ diff --git a/readme-image/image-20200920221355462.png b/readme-image/image-20200920221355462.png new file mode 100644 index 0000000..4a93aab Binary files /dev/null and b/readme-image/image-20200920221355462.png differ diff --git a/readme.md b/readme.md index 8f30df2..74633ff 100644 --- a/readme.md +++ b/readme.md @@ -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)门限方案 > 参考: > @@ -176,4 +195,4 @@ public long restoreSecretKey(Piece[] pieces, long mod) { } ``` -## 二、基于中国剩余定理的(k, n)门限方案 \ No newline at end of file +## 三、基于中国剩余定理的(k, n)门限方案 \ No newline at end of file diff --git a/src/main/java/com/py7hon/ThresholdSchemeApplication.java b/src/main/java/com/py7hon/ThresholdSchemeApplication.java new file mode 100644 index 0000000..1febc56 --- /dev/null +++ b/src/main/java/com/py7hon/ThresholdSchemeApplication.java @@ -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"; + } +} diff --git a/src/main/java/com/py7hon/config/SecurityConfig.java b/src/main/java/com/py7hon/config/SecurityConfig.java new file mode 100644 index 0000000..b3b794d --- /dev/null +++ b/src/main/java/com/py7hon/config/SecurityConfig.java @@ -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("*"); + } +} diff --git a/src/main/java/com/py7hon/controller/ThresholdSchemeController.java b/src/main/java/com/py7hon/controller/ThresholdSchemeController.java new file mode 100644 index 0000000..c3f7dae --- /dev/null +++ b/src/main/java/com/py7hon/controller/ThresholdSchemeController.java @@ -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 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; + } + } +} diff --git a/src/main/java/com/py7hon/entity/Piece.java b/src/main/java/com/py7hon/entity/Piece.java new file mode 100644 index 0000000..381cc2f --- /dev/null +++ b/src/main/java/com/py7hon/entity/Piece.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/py7hon/threshold/scheme/ThresholdScheme.java b/src/main/java/com/py7hon/threshold/scheme/ThresholdScheme.java index 7d92c5c..afe08a8 100644 --- a/src/main/java/com/py7hon/threshold/scheme/ThresholdScheme.java +++ b/src/main/java/com/py7hon/threshold/scheme/ThresholdScheme.java @@ -1,6 +1,6 @@ package com.py7hon.threshold.scheme; -import lombok.Data; +import com.py7hon.entity.Piece; /** * 定义了门限方案的一些方法 @@ -11,55 +11,31 @@ */ public interface ThresholdScheme { /** - * 计算分片 + * 计算分片。 + *

调用说明:

+ *

1. 确定好要进行分片的密钥:secretKey([0, Long.MAX_VALUE])

+ *

2. 确定好分片数:totalPieceNumber ([0, Integer.MAX_VALUE])

+ *

3. 确定最小有效分片数:minEffectivePieceNumber ([0, totalPieceNumber])

+ *

4. 任取一个大于的素数:mod (mod > totalPieceNumber 且 mod > secretKey ),用作模运算的模

+ *

5. 调用方法即可得到分片。任选 n (n >= minEffectivePieceNumber)个分片即可还原出密钥


* * @param secretKey 密钥 * @param totalPieceNumber 总分片数(n) - * @param minEffectiveSliceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥 + * @param minEffectivePieceNumber 最小有效分片数(k),取 k 个以上的分片才能还原出密钥 * @param mod 模,所有的运算将在此模下进行。素数,大于 totalPieceNumbersecretKey * @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 模,所有的运算将在此模下进行。素数,大于 totalPieceNumbersecretKey * @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; - } - } } diff --git a/src/main/java/com/py7hon/threshold/scheme/chinese/remainder/theorem/ChineseRemainderTheorem.java b/src/main/java/com/py7hon/threshold/scheme/chinese/remainder/theorem/ChineseRemainderTheorem.java index cadf1a8..4e6a0fe 100644 --- a/src/main/java/com/py7hon/threshold/scheme/chinese/remainder/theorem/ChineseRemainderTheorem.java +++ b/src/main/java/com/py7hon/threshold/scheme/chinese/remainder/theorem/ChineseRemainderTheorem.java @@ -1,6 +1,7 @@ package com.py7hon.threshold.scheme.chinese.remainder.theorem; +import com.py7hon.entity.Piece; import com.py7hon.threshold.scheme.ThresholdScheme; /** @@ -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; } } diff --git a/src/main/java/com/py7hon/threshold/scheme/shamir/Shamir.java b/src/main/java/com/py7hon/threshold/scheme/shamir/Shamir.java index 653ecc5..c64b225 100644 --- a/src/main/java/com/py7hon/threshold/scheme/shamir/Shamir.java +++ b/src/main/java/com/py7hon/threshold/scheme/shamir/Shamir.java @@ -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; @@ -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); @@ -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 数值溢出 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..0330a39 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8089 \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..185b3f4 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,252 @@ + + + + + + + + 门限方案 + + + + + + + + + + + + + + + + + + + + + + + + + + k + + + + + + + + + + + *程序不对输入的数据进行校验,请自行校验 + + + 生成密钥分片 + + + + + + + + + + + + + + + + + + {{tag.index + '-' + tag.value}} + +
+ + 获取分片 + + +
+ + 恢复密钥 + + +
+
+
+
+ + +

信息展示

+ + + + + + + + +
+ + + + {{tag.index + '-' + tag.value}} + +
+
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/src/test/java/com/py7hon/threshold/scheme/shamir/ShamirTest.java b/src/test/java/com/py7hon/threshold/scheme/shamir/ShamirTest.java deleted file mode 100644 index 727c197..0000000 --- a/src/test/java/com/py7hon/threshold/scheme/shamir/ShamirTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.py7hon.threshold.scheme.shamir; - -import com.py7hon.threshold.scheme.ThresholdScheme; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; - -public class ShamirTest { - - @Test - public void genSlices() { - ThresholdScheme.Piece[] pieces = new Shamir().genPieces(11, 5, 3, 19); - System.out.println("pieces = " + Arrays.toString(pieces)); - } -} \ No newline at end of file