我一直在寻找使用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 :(得分: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 [],那么你可以使用隐藏来实现你的目的。
试试这个方法:
byte[] bytes = File.ReadAllBytes("C:\\Mybinaryfile");