Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组字节的文件中循环_Java_Arrays_Shared Secret - Fatal编程技术网

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

Java 如何在数组字节的文件中循环,java,arrays,shared-secret,Java,Arrays,Shared Secret,我有这个方法,我将对字节数组的文件应用SSS。 byte[]secret是一个方法参数,在这里,我将把文件的每个字节作为参数传递,然后对每个字节应用SSS算法。我还实现了一个java代码,说明如何读取文件,然后将其转换为字节数组。我被困在如何为每个字节的文件实现这个SSS算法。 我知道我需要一个for循环。关键是我想调用我的main方法这个byte[]secret并将文件的每个字节分配给它,但我一直不知道怎么做 我将读取文件并将其转换为位数组的方法如下: public byte[][] crea

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

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

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

{
// some code here
}
public byte[]readFile(文件名)引发IOException{
InputStream is=新文件InputStream(文件名);
//获取文件的大小
long length=fileName.length();
//以确保文件不大于Integer.MAX_值。
if(长度>整数最大值){
抛出新IOException(“无法完全读取文件”+fileName.getName()+,因为它太长(“+length+”字节,支持的最大值“+Integer.max_VALUE+”));
}
//创建字节数组以保存数据
字节[]秘密=新字节[(int)长度];
整数偏移=0;
int numRead=0;
而(偏移量=0){
偏移量+=numRead;
}
//确保已读入所有字节
if(偏移量

有谁能帮助我如何循环文件的每个字节,然后将其作为参数传递给我的createshares方法吗?

我知道您正在尝试从文件中读取字节,并尝试循环通过字节[]

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;


 }
**输出:

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

使用此处的字节49
使用此处的字节50
使用此处的字节51
使用此处的字节52

使用此处的字节10

您应该使用这些方法中的任何一种来读取文件(这样您就不会重新发明轮子),您也应该更好地解释您的问题。@PacoAbato假设OP尝试实现这种方法:这很好,但当我使用它时,我没有为每个字节共享我的文件。我只是得到了一些类似于你的输出。你能帮助我如何获取我的字节共享的每个文件公共字节[][]创建共享(字节[]秘密,int共享,int阈值,随机rnd){byte[][]共享=新字节[shares][m+1];for(int i=0;iimport 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; } }