2016-03-03 57 views
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; 


} 

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

+0

你应该使用任何这种方法来读取文件(所以你不是重新发明轮子)http://stackoverflow.com/questions/858980/file-to-byte-in-java你也应该更好地解释你的问题。 –

+0

@PacoAbato假设OP试图实现这种:https://en.wikipedia.org/wiki/Shamir's_Secret_Sharing –

回答

0

我知道你正试图从文件中读取字节,并试图循环访问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; 
} 
} 

**输出:

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

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

+0

这是工作正常,但仍然当我使用这个我没有我的文件共享每个字节。我刚刚得到了类似以下输出的内容。你能帮我怎么获得我的字节共享的每个文件 –

+0

public byte [] [] createShares(byte [] secret,int shares,int threshold,Random rnd){byte [] [] share = new byte [shares] [m + 1]; \t \t \t \t \t \t对(INT I = 0; I <股;我++) \t份额[I] [0] =(字节)第(i + 1); \t \t byte [] a = null; \t \t尝试{ \t \t \t a =新字节[阈值]; \t \t \t对(INT I = 0; I <米;我++){ \t \t \t \t RND。的nextBytes(一); \t \t \t \t a [0] = secret [i]; \t \t \t \t对(INT J = 0;Ĵ<股; J ++) \t \t \t \t \t份额[j]的第[i + 1] =(字节)的eval(份额[J] [0],A); \t \t \t} \t \t}最后{ \t \t \t如果(一个!= NULL) \t \t \t \t Arrays.fill(一,(字节)0); \t \t} \t \t return share; } –