google introduced security-crypto jetpack library
i want to use this library for encrypt image files, in documents of library there is no sample for encryption of image files.
i converted image to bitmap - bitmap to byte array - then used library As mentioned in the document finaly file is encrypted, but when the file is decoded, a black image appears i donot get any exception where is the problem?
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =findViewById(R.id.image);
// get image from asset and convert to bitmap
Bitmap bitmap = getBitmapFromAsset(this,"temp.jpg");
byte[] bytesOfBitmap= BitmapToByteArray(bitmap);
// encrypt
File pathForSaveEncryptedFile=new File(getFilesDir().getAbsolutePath() + File.separator+"encrypted.me");
try {
encryptToFile(this,pathForSaveEncryptedFile,bytesOfBitmap);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
// decrypt and show to image view
try {
byte[] decryptedFile = decryptFile(this,pathForSaveEncryptedFile);
imageView.setImageBitmap(ByteArrayToBitmap(decryptedFile));
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
private void encryptToFile(Context context,File pathToSave,byte[] contents) throws GeneralSecurityException, IOException {
MasterKey mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
pathToSave,
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
OutputStream outputStream = encryptedFile.openFileOutput();
outputStream.write(contents);
outputStream.flush();
outputStream.close();
}
private byte[] decryptFile(Context context ,File target) throws GeneralSecurityException, IOException {
MasterKey mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
target,
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
InputStream inputStream = encryptedFile.openFileInput();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int nextByte = inputStream.read();
while (nextByte != -1) {
byteArrayOutputStream.write(nextByte);
nextByte = inputStream.read();
}
return byteArrayOutputStream.toByteArray();
}
private byte[] BitmapToByteArray(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
private Bitmap ByteArrayToBitmap(byte[] bytes){
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
private Bitmap getBitmapFromAsset(Context context, String fileName) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(fileName);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// handle exception
}
return bitmap;
}
}
also i converted image to base64(String) and use the library but not work.