如何在数组字节的文件中循环

How to loop in the file of array byte

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

{
// some code here
}

我有这个方法,我要为字节数组的文件应用SSS。 byte [] secret 是方法参数,我将在其中将文件的每个字节作为参数传递,然后为每个字节应用 SSS 算法。我还实现了如何读取文件然后将其转换为字节数组的 java 代码。我对如何为文件的每个字节实现这个 SSS 算法感到困惑。 我知道我需要一个 for 循环。关键是我想调用我的主要方法这个字节 [] 秘密并将文件的每个字节分配给它,但我不知道如何去做。

我读取文件并将其转换为位数组的方法如下:

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;


 }

任何人都可以帮助我如何循环文件的每个字节,然后将其作为参数传递给我的 createshares 方法吗?

我了解到您正在尝试从文件中读取字节并尝试遍历字节[]。

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

**输出:

存储的秘密为 1234
字节数组表示:[49, 50, 51, 52, 10]

这里使用字节49
这里使用字节 50
这里使用字节 51
这里使用字节 52
这里使用字节 10