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);
}
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");
}
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();
}
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();
}
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);
}
Aggregations