Java-реализация секрета обмена Шамиром
Я реализовал код 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);
}
}
1 ответ
Вы можете просто добавить i- ую долю каждого байта в байтовый массив, а результирующий байтовый массив - это i-я доля ваших последних N долей.
Например, переменная tab
в коде выглядит так (B iS j означает j-ую долю i-го байта, 1<= i <=k, 1<= j <=n): B1S1/B1S2/...B1Sn/B2S1/B2S2/...B2Sn/...BkSn/
, Выберите i-ую долю каждого байта, чтобы составить байтовый массив: byte[] share_i = new byte[]{B1Si, B2Si, B3Si,...BkSi}
, share_i
это то, что вы хотите.
Кроме того, вам лучше генерировать различные коэффициенты для каждого байта.