0
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 方法?

4

1 回答 1

0

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

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

于 2016-03-03T16:39:02.583 回答