Search in sources :

Example 1 with SharedPrefsBackedKeyChain

use of com.facebook.crypto.keychain.SharedPrefsBackedKeyChain in project UltimateAndroid by cymcsg.

the class FakeKeyChain method encryptingContent.

public static void encryptingContent(Context context, File file, byte[] plainTextBytes) throws Exception {
    // 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());
    // 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, new Entity("TEST1"));
    // Write plaintext to it.
    outputStream.write(plainTextBytes);
    outputStream.close();
}
Also used : Entity(com.facebook.crypto.Entity) Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.crypto.keychain.SharedPrefsBackedKeyChain) SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary)

Example 2 with SharedPrefsBackedKeyChain

use of com.facebook.crypto.keychain.SharedPrefsBackedKeyChain in project UltimateAndroid by cymcsg.

the class FakeKeyChain method decryptingContent.

public static void decryptingContent(Context context, File file, String newPath) throws Exception {
    // Get the file to which ciphertext has been written.
    FileInputStream fileStream = new FileInputStream(file);
    Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary());
    // Creates an input stream which decrypts the data as
    // it is read from it.
    InputStream inputStream = crypto.getCipherInputStream(fileStream, new Entity("TEST1"));
    // 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.
    FileOutputStream fs = new FileOutputStream(newPath);
    while ((read = inputStream.read(buffer)) != -1) {
        fs.write(buffer, 0, read);
    }
    inputStream.close();
}
Also used : Entity(com.facebook.crypto.Entity) Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.crypto.keychain.SharedPrefsBackedKeyChain) SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary)

Aggregations

Crypto (com.facebook.crypto.Crypto)2 Entity (com.facebook.crypto.Entity)2 SharedPrefsBackedKeyChain (com.facebook.crypto.keychain.SharedPrefsBackedKeyChain)2 SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)2