public byte[][] createShares(byte[] secret, int shares, int threshold, Random rnd)
{
// some code here
}
I have this method and i am going to apply SSS for file of byte array . byte [] secret is method parameter where i am going to pass as argument each byte of the file and then apply for each byte the SSS algorithm. I have also implemented a java code of how to read the file and then convert it to a byte array. I am stuck of how to implement this SSS algorithm for each byte of files. I know i need a for loop for that . The point is i want to call to my main method this byte [] secret and assign to it each byte of the file but i am stuck of how to do it .
My method which will read the file and convert it to the array of bit is as below:
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;
}
Can anyone help me how to loop for each byte of the file and then pass it as the argument to my createshares method ?
I understand you are trying to read bytes from the file and also trying to loop through the byte[].
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;
}
}
**Output:
Stored secret as 1234
byte array representation: [49, 50, 51, 52, 10]
Use the byte here 49
Use the byte here 50
Use the byte here 51
Use the byte here 52
Use the byte here 10