Facebook Conceal - Image Encryption and Decryption

后端 未结 1 1092
别跟我提以往
别跟我提以往 2021-01-03 08:31

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 q

相关标签:
1条回答
  • 2021-01-03 08:50

    Here is how i solved the problem.. encryption and decryption works fine now.

    // Encrypts the image and saves to directory
    
    public void encodeAndSaveFile(Bitmap photo, int code) {
    
        try {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
            File mypath = new File(directory, ENCRYPTEDFILENAME);
    
            Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this),
                    new SystemNativeCryptoLibrary());
    
            if (!crypto.isAvailable()) {
                return;
            }
    
            OutputStream fileStream = new BufferedOutputStream(
                    new FileOutputStream(mypath));
            OutputStream outputStream = crypto.getCipherOutputStream(
                    fileStream, new Entity("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 bytes
    private byte[] bitmapToBytes(Bitmap photo) {
    
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }
    
    // convert bytes to Bitmap
    private Bitmap bytesToBitmap(byte[] bytes) {
    
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    
        return bitmap;
    }
    
    // decode encrypted file and returns Bitmap
    private Bitmap decodeFile(String filename) {
    
        Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this),
                new SystemNativeCryptoLibrary());
    
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
        File file = new File(directory, filename);
    
        try {
            FileInputStream fileStream = new FileInputStream(file);
            InputStream inputStream = crypto.getCipherInputStream(fileStream,
                    new Entity("Password"));
    
            ByteArrayOutputStream out = new ByteArrayOutputStream();
    
            int read;
            byte[] buffer = new byte[1024];
    
            while ((read = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
    
            inputStream.close();
    
            Bitmap bitmap = bytesToBitmap(out.toByteArray());
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return null;
    }
    

    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);
    
    0 讨论(0)
提交回复
热议问题