2011-04-06 52 views

回答

1

你可以定義一個加密的成員,你的類的非加密的成員。然後,只是適當地序列化它們。例如,你的班級可能是這樣的:

public Class partiallyEncrypted { 
    private transient byte[] imagePart1Decrypted; // transient member will not be serialized by default 
    private byte[] imagePart1Encrypted; 
    private byte[] imagePart2; 
    private static final int BYTES_TO_ENCRYPT = 2048; 
    // other non-encrypted fields 
    ... 

    public setImage(byte[] imageBytes) { 
    // calc bytes to encrypt and those left over, initialize arrays 
    int bytesToEncrypt = Math.min(imageBytes.length, BYTES_TO_ENCRYPT); 
    int bytesLeftOver = Math.max(imageBytes.length - bytesToEncrypt, 0); 
    imagePart1Decrypted = new byte[bytesToEncrypt]; 
    imagePart2 = new byte[bytesLeftOver]; 

    // copy data into local arrays 
    System.arraycopy(imageBytes, 0, imagePart1Decrypted, 0, bytesToEncrypt); 
    if (bytesLeftOver > 0) 
     System.arraycopy(imageBytes, bytesToEncrypt, imagePart2, 0, bytesLeftOver); 

    imagePart1Encrypted = encrypt(bytesToEncrypt); 
    } 

    public byte[] getImage() { 
    if (imagePart1Decrypted == null) 
     imagePart1Decrypted = decrypt(imagePart1Encrypted); 

    byte[] fullImage = new byte[imagePart1Decrypted.length + imagePart2.length]; 
    System.arraycopy(imagePart1Decrypted, 0, fullImage, 0, imagePart1Decrypted.length); 
    if (imagePart2 != null && imagePart2.length > 0) 
     System.arraycopy(imagePart2, 0, fullImage, imagePart1Decrypted.length, imagePart2.length); 

    return fullImage; 
    } 
} 
+0

嗨,感謝您的答覆。這應該適用於由字符串組成的文件。不過,我也需要這個圖像/音頻文件。任何想法? – icegee 2011-04-06 04:29:25

+0

而不是字符串,你可能只是有一個字節數組。所以,或多或少有相同的區別,只需將String替換爲byte []即可。然後,您必須構建邏輯以將加密部分與未加密部分組合起來。 – squawknull 2011-04-06 04:51:45

+0

是的。你已經明白了。也許我應該早些時候宣佈它。我完全不知道如何將兩者結合起來! – icegee 2011-04-06 05:39:11

相關問題