Facebook Conceal - Image Encryption And Decryption
I am trying to encrypt and decrypt image using Facebook Conceal Library. This is the first time i am using it and hence bear with me if its trivial. I have looked at other question
Solution 1:
Here is how i solved the problem.. encryption and decryption works fine now.
// Encrypts the image and saves to directorypublicvoidencodeAndSaveFile(Bitmap photo, int code) {
try {
ContextWrappercw=newContextWrapper(getApplicationContext());
Filedirectory= cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
Filemypath=newFile(directory, ENCRYPTEDFILENAME);
Cryptocrypto=newCrypto(newSharedPrefsBackedKeyChain(this),
newSystemNativeCryptoLibrary());
if (!crypto.isAvailable()) {
return;
}
OutputStreamfileStream=newBufferedOutputStream(
newFileOutputStream(mypath));
OutputStreamoutputStream= crypto.getCipherOutputStream(
fileStream, newEntity("Password"));
outputStream.write(bitmapToBytes(photo));
outputStream.close();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// convert Bitmap to bytesprivatebyte[] bitmapToBytes(Bitmap photo) {
ByteArrayOutputStreamstream=newByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
// convert bytes to Bitmapprivate Bitmap bytesToBitmap(byte[] bytes) {
Bitmapbitmap= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
// decode encrypted file and returns Bitmapprivate Bitmap decodeFile(String filename) {
Cryptocrypto=newCrypto(newSharedPrefsBackedKeyChain(this),
newSystemNativeCryptoLibrary());
ContextWrappercw=newContextWrapper(getApplicationContext());
Filedirectory= cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
Filefile=newFile(directory, filename);
try {
FileInputStreamfileStream=newFileInputStream(file);
InputStreaminputStream= crypto.getCipherInputStream(fileStream,
newEntity("Password"));
ByteArrayOutputStreamout=newByteArrayOutputStream();
int read;
byte[] buffer = newbyte[1024];
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
inputStream.close();
Bitmapbitmap= bytesToBitmap(out.toByteArray());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
returnnull;
}
Here is how i am calling encodeAndSaveFile() and decodeFile(), in onActivityResult(), after returning from camera.
encodeAndSaveFile((Bitmap) data.getExtras().get("data"),
requestCode);
Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
Post a Comment for "Facebook Conceal - Image Encryption And Decryption"