如何循环数组字节的文件

时间:2016-03-03 15:12:16

标签: java arrays shared-secret

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

{
// some code here
}

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

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

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

1 个答案:

答案 0 :(得分:0)

我知道你正在尝试从文件中读取字节,并尝试遍历byte []。

 $request .= 'User-Agent: Safari/537.16\r\n'; 

**输出:

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

使用此处的字节49
在这里使用字节50
使用此处的字节51
在这里使用字节52
使用此处的字节10