我只为一个字节实现了一个用于shamir秘密共享算法的java代码,我还是无法做到这一点如何将这些实现用于字节数组? 谢谢你的帮助
这是我的计划
public class Shamir_partage {
static Scanner sc = new Scanner(System.in);
static Scanner sca = new Scanner(System.in);
public static void main(final String[] args) {
System.out.println("entrez le secret");
String ste = sca.nextLine();
BigInteger ascii = new BigInteger("0");
byte bval[] = null;
try {
bval = ste.getBytes("UTF8");
for (int i = 0; i < bval.length; i++) {
System.out.println(bval[i]);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("entrez p et n");
int p = sc.nextInt();
int n = sc.nextInt();
//System.out.println("("+p+", "+n+")");
BigInteger[] array = new BigInteger[200];
//System.out.println("Entrez les valeurs aléatoires k(p-1).");
for (int count = 0; count < p - 1; count++) {
Random rand = new Random();
int nombreAleatoire = rand.nextInt(500 - 1 + 1) + 1;
//System.out.println(nombreAleatoire);
array[count] = BigInteger.valueOf(nombreAleatoire);
}
String tab = "";
// generate N shares for each character
// how to regroup them to have in result N shares for the string?
for (int k = 0; k < ste.length(); k++) {
BigInteger rlt = new BigInteger("0");
for (int i = 1; i <= n; i++) {
for (int j = 0; j < p - 1; j++) {
rlt = rlt.add(array[j].multiply(BigDecimal.valueOf(Math.pow(i, j + 1)).toBigInteger()));
}
rlt = rlt.add(BigInteger.valueOf(bval[k]));
rlt = rlt.mod(BigInteger.valueOf(257));
tab = tab + rlt + "/";
rlt = new BigInteger("0");
}
}
String str[] = tab.split("/");
for (int j = 0; j < str.length; j++) {
System.out.println("(" + (j + 1) + "," + str[j] + ")");
}
System.out.println("-------------reconstitution du secret--------------");
BigInteger[] arrayx = new BigInteger[200];
BigInteger[] arrayy = new BigInteger[200];
for (int count = 0; count < p; count++) {
System.out.print("Entrez la valeur pour x" + count + ": ");
arrayx[count] = sc.nextBigInteger();
}
for (int count = 0; count < p; count++) {
System.out.print("Entrez la valeur pour f(x) = y" + count + ": ");
arrayy[count] = sc.nextBigInteger();
}
BigInteger y = new BigInteger("0");
for (int count = 0; count < p; count++) {
BigInteger numerator = new BigInteger("1");
BigInteger denominator = new BigInteger("1");
for (int count2 = 0; count2 < p; count2++) {
if (count2 != count) {
numerator = numerator.multiply(BigInteger.valueOf(-1)).multiply(arrayx[count2]);
//System.out.println( " num = " + numerator);
denominator = denominator.multiply(arrayx[count].subtract(arrayx[count2]));
//System.out.println( " denom = " + denominator);
}
}
y = y.add(numerator.multiply(arrayy[count]).multiply(denominator.modInverse(BigInteger.valueOf(257))));
//System.out.println( " secret = " + y);
}
//System.out.println( " secret = " + y);
y = y.mod(BigInteger.valueOf(257));
System.out.println(" secret = " + y);
//char c = (char)y;
String c = new String(y.toByteArray());
System.out.println(" secret = " + c);
}
}
答案 0 :(得分:0)
您可以将每个字节的 i -th共享附加到字节数组,结果字节数组是最终的 i -th共享> N 股票。
例如,代码中的变量tab
看起来像这样( B i S j表示第i个字节的第j个共享,1&lt; = i&lt; = k,1&lt; = j&lt; = n):B1S1/B1S2/...B1Sn/B2S1/B2S2/...B2Sn/...BkSn/
。选择每个字节的第i个共享来组成一个字节数组:byte[] share_i = new byte[]{B1Si, B2Si, B3Si,...BkSi}
。 share_i
就是你想要的
此外,您最好为每个字节生成不同的系数。