2015-09-21 81 views
0

我一直在尋找使用隱藏來加密一些文件。代碼片段提供了狀態,輸入是純文本。它可以用來加密文件二進制文件嗎?爲什麼它特別是純文本而不是一般的二進制文件?使用Conceal加密文件

這裏提供的代碼片段:

// Creates a new Crypto object with default implementations of 
// a key chain as well as native library. 
Crypto crypto = new Crypto(
    new SharedPrefsBackedKeyChain(context), 
    new SystemNativeCryptoLibrary()); 

// Check for whether the crypto functionality is available 
// This might fail if Android does not load libaries correctly. 
if (!crypto.isAvailable()) { 
    return; 
} 

OutputStream fileStream = new BufferedOutputStream(
    new FileOutputStream(file)); 

// Creates an output stream which encrypts the data as 
// it is written to it and writes it out to the file. 
OutputStream outputStream = crypto.getCipherOutputStream(
    fileStream, 
    entity); 

// Write plaintext to it. 
outputStream.write(plainText); 
outputStream.close(); 

回答

0

從庫,方法說:

/** 
    * A convenience method to encrypt data if the data to be processed is small and can 
    * be held in memory. 
    * @param plainTextBytes Bytes of the plain text. 
    * @param entity Entity to process. 
    * @return cipherText. 
    * @throws KeyChainException 
    * @throws CryptoInitializationException 
    * @throws IOException 
    * @throws CryptoInitializationException Thrown if the crypto libraries could not be initialized. 
    * @throws KeyChainException Thrown if there is trouble managing keys. 
    */ 
    public byte[] encrypt(byte[] plainTextBytes, Entity entity) 
    throws KeyChainException, CryptoInitializationException, IOException { 
    int cipheredBytesLength = plainTextBytes.length + mCipherHelper.getCipherMetaDataLength(); 
    FixedSizeByteArrayOutputStream outputStream = new FixedSizeByteArrayOutputStream(cipheredBytesLength); 
    OutputStream cipherStream = mCipherHelper.getCipherOutputStream(outputStream, entity); 
    cipherStream.write(plainTextBytes); 
    cipherStream.close(); 
    return outputStream.getBytes(); 
    } 

您需要將純文本轉換爲字節[]爲了使用這個庫。如果你還可以將二進制文件轉換爲byte [],那麼你可以使用隱藏的目的。

試試這個方法:

byte[] bytes = File.ReadAllBytes("C:\\Mybinaryfile");