0
public byte[][] createShares(byte[] secret, int shares, int threshold, Random rnd) 

{
// some code here
}

I have this method and i am going to apply SSS for file of byte array . byte [] secret is method parameter where i am going to pass as argument each byte of the file and then apply for each byte the SSS algorithm. I have also implemented a java code of how to read the file and then convert it to a byte array. I am stuck of how to implement this SSS algorithm for each byte of files. I know i need a for loop for that . The point is i want to call to my main method this byte [] secret and assign to it each byte of the file but i am stuck of how to do it .

My method which will read the file and convert it to the array of bit is as below:

public  byte[] readFile(File fileName) throws IOException {
      InputStream is = new FileInputStream(fileName);

  // Get the size of the file
  long length = fileName.length();


  // to ensure that file is not larger than Integer.MAX_VALUE.
  if (length > Integer.MAX_VALUE) {
    throw new IOException("Could not completely read file " + fileName.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
  }

  // Create the byte array to hold the data
  byte[] secret = new byte[(int)length];


  int offset = 0;
  int numRead = 0;
  while (offset < secret.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
      offset += numRead;
  }

  // Ensure all the bytes have been read in
  if (offset < secret.length) {
      throw new IOException("Could not completely read file " + fileName.getName());
  }

  // Close the input stream and return bytes
  is.close();
  return secret;


 }

Can anyone help me how to loop for each byte of the file and then pass it as the argument to my createshares method ?

Robert Martin
  • 1,585
  • 1
  • 13
  • 20
john smith
  • 21
  • 6
  • You should use any of this methods to read the file (so you are not reinventing the wheel) http://stackoverflow.com/questions/858980/file-to-byte-in-java Also you should explain better your issue. – Paco Abato Mar 03 '16 at 15:19
  • @PacoAbato suppose OP tries to implement kind of this: https://en.wikipedia.org/wiki/Shamir's_Secret_Sharing – Alex Salauyou Mar 03 '16 at 15:25

1 Answers1

0

I understand you are trying to read bytes from the file and also trying to loop through the byte[].

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import java.nio.file.Path;


public class SSSAlgorithm {

public static void main(String[] args) {
    System.out.println("Reading file");
    try {
        byte[] secret = readFile();
        createShares(secret, 2, 3, 100);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static byte[][] createShares(byte[] secret, int shares, int threshold, int i) 
{
    // some code here
    for (byte coeff : secret){
        System.out.println("Use the byte here " + coeff);
    }

    return null;
}


public static byte[] readFile() throws IOException {
    Path path = Paths.get("/Users/droy/var/crypto.txt");
    try {
        byte[] secret = Files.readAllBytes(path);
        return secret;

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
}

**Output:

Stored secret as 1234
byte array representation: [49, 50, 51, 52, 10]

Use the byte here 49
Use the byte here 50
Use the byte here 51
Use the byte here 52
Use the byte here 10

The Roy
  • 2,178
  • 1
  • 17
  • 33
  • this is working fine but still when i use this i do not have my file shared for each byte . I just got something like your output below. Can you help me how can i get each file of my byte shared – john smith Mar 03 '16 at 18:52
  • public byte[][] createShares(byte[] secret, int shares, int threshold, Random rnd) {byte[][] share = new byte[shares][m + 1]; for (int i = 0; i < shares; i++) share[i][0] = (byte) (i + 1); byte[] a = null; try { a = new byte[threshold]; for (int i = 0; i < m; i++) { rnd.nextBytes(a); a[0] = secret[i]; for (int j = 0; j < shares; j++) share[j][i + 1] = (byte) eval(share[j][0], a); } } finally { if (a != null) Arrays.fill(a, (byte) 0); } return share; } – john smith Mar 03 '16 at 18:57