2016-03-03 38 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; } –