Search in sources :

Example 1 with Entity

use of com.facebook.crypto.Entity in project hawk by orhanobut.

the class ConcealEncryption method encrypt.

@Override
public String encrypt(String key, String plainText) throws Exception {
    Entity entity = Entity.create(key);
    byte[] bytes = crypto.encrypt(plainText.getBytes(), entity);
    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
Also used : Entity(com.facebook.crypto.Entity)

Example 2 with Entity

use of com.facebook.crypto.Entity in project hawk by orhanobut.

the class ConcealTest method testConceal.

@Test
public void testConceal() throws Exception {
    Entity entity = Entity.create("key");
    String value = "value";
    byte[] encryptedValue = crypto.encrypt(value.getBytes(), entity);
    assertThat(encryptedValue).isNotNull();
    String decryptedValue = new String(crypto.decrypt(encryptedValue, entity));
    assertThat(decryptedValue).isEqualTo("value");
}
Also used : Entity(com.facebook.crypto.Entity) Test(org.junit.Test)

Example 3 with Entity

use of com.facebook.crypto.Entity 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 4 with Entity

use of com.facebook.crypto.Entity 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)

Example 5 with Entity

use of com.facebook.crypto.Entity in project hawk by orhanobut.

the class ConcealEncryption method decrypt.

@Override
public String decrypt(String key, String cipherText) throws Exception {
    Entity entity = Entity.create(key);
    byte[] decodedBytes = Base64.decode(cipherText, Base64.NO_WRAP);
    byte[] bytes = crypto.decrypt(decodedBytes, entity);
    return new String(bytes);
}
Also used : Entity(com.facebook.crypto.Entity)

Aggregations

Entity (com.facebook.crypto.Entity)5 Crypto (com.facebook.crypto.Crypto)2 SharedPrefsBackedKeyChain (com.facebook.crypto.keychain.SharedPrefsBackedKeyChain)2 SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)2 Test (org.junit.Test)1