javaencryptioncryptographyshared-secret

Java implementation of shamir's sharing secret


I have implemented a java code for shamir secret sharing algorithm for only one byte , The only thing i still am not able to do it how to use these implementation for a byte array? thanks for your help

here my program

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);

    }
}

Solution

  • You can just append the i-th share of each byte to a byte array, and the result byte array is the i-th share of your final N shares.
    For example, the variable tab in the code looks like this(BiSj means the j-th share of the i-th byte, 1<= i <=k, 1<= j <=n): B1S1/B1S2/...B1Sn/B2S1/B2S2/...B2Sn/...BkSn/. Pick the i-th share of each byte to compose a byte array: byte[] share_i = new byte[]{B1Si, B2Si, B3Si,...BkSi}. share_i is what you want.
    Additionally, you'd better to generate different coefficients for each byte.