Quick start
If you're using
BUCK,
you can grab the repo from
Github
and build it.
The resulting binaries will be in buck-out/
If you're not using BUCK, you can just grab the binaries from
Add a dependency to libconceal.jar as well as conceal_android.jar from java code using your favorite dependency management system,
and drop the .so files in libs.zip into your jniLibs/ folder located at src/main/jniLibs
Encrypting content
// 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(plainTextBytes);
outputStream.close();
Decrypting content
// Get the file to which ciphertext has been written.
FileInputStream fileStream = new FileInputStream(file);
// Creates an input stream which decrypts the data as
// it is read from it.
InputStream inputStream = crypto.getCipherInputStream(
fileStream,
entity);
// Read into a byte array.
int read;
byte[] buffer = new byte[1024];
// You must read the entire stream to completion.
// The verification is done at the end of the stream.
// Thus not reading till the end of the stream will cause
// a security bug.
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
inputStream.close();
Creating a new keychain
The default KeyChain stores it's data into shared preferences. You might want to implement
your own KeyChain to persist key material somewhere else.
public class CustomKeyChain implements KeyChain {
...
}