调用说明:
+ *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)个分片即可还原出密钥
totalPieceNumber
和 secretKey
* @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 模,所有的运算将在此模下进行。素数,大于 totalPieceNumber
和 secretKey
* @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 @@
+
+
+
+
+
+
+
+