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