Facebook隐藏 - 图像加密和解密 [英] Facebook Conceal - Image Encryption and Decryption

查看:128
本文介绍了Facebook隐藏 - 图像加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Facebook隐藏库加密和解密图像。这是我第一次使用它,因此如果它微不足道,就忍受我。我已经看过关于SO的其他问题,以了解我的例外情况的原因,我无法让它工作。



这是我现在所做的。 ..



集成:我正在使用Eclipse,因此,从这里,并将jar文件添加到libs文件夹和.so文件到libs文件夹中的相应文件夹。



我的场景:



我必须从相机捕获图像,将其加密并存储在手机内存中。解密它并在图像视图中显示。在稍后的阶段,我还需要从内存中解密该图像并通过网络发送。



所以,我的代码如下...

  @Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){

if( Const.DEBUGGING){
Log.d(Const.DEBUG,RequestCode:+ requestCode +\\\
ResultCode:
+ resultCode);
}

int tag = getRecordCount();
tag ++;

if(requestCode == KTP_PICTURE_REQUEST_CODE){
if(resultCode == RESULT_OK){

ENCRYPTEDFILENAME = tag +_+ KTP_TAG +.png ;

saveFile((Bitmap)data.getExtras()。get(data),requestCode);
位图decryptptedImage = decodeFile(ENCRYPTEDFILENAME);
mImgBtnKtppicture.setImageBitmap(decryptptedImage);

} else if(resultCode == RESULT_CANCELED){
if(Const.DEBUGGING)
Log.d(Const.DEBUG,KTP_RESULT CANCELED);
} else {

}
}

if(requestCode == PROFILE_PICTURE_REQUEST_CODE){

if(resultCode == RESULT_OK){

ENCRYPTEDFILENAME = tag +_+ PROFILE_TAG +.png;

saveFile((Bitmap)data.getExtras()。get(data),requestCode);
位图decryptptedImage = decodeFile(ENCRYPTEDFILENAME);
mImgBtnPicture.setImageBitmap(decryptptedImage);

} else if(resultCode == RESULT_CANCELED){
if(Const.DEBUGGING)
Log.d(Const.DEBUG,PICTURE_RESULT CANCELED);
} else {

}

}
}

SaveFile():

  public void saveFile(Bitmap photo,int code){

try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
文件目录= cw.getDir(DIRECTORY,Context.MODE_PRIVATE);
文件mypath =新文件(目录,ENCRYPTEDFILENAME);

if(code == KTP_PICTURE_REQUEST_CODE){
mKtppicture = Uri.fromFile(mypath).toString();

if(Const.DEBUGGING)
Log.d(Const.DEBUG,KTP Picture Path:+ mKtppicture);
} else if(code == PROFILE_PICTURE_REQUEST_CODE){
mPicture = Uri.fromFile(mypath).toString();

if(Const.DEBUGGING)
Log.d(Const.DEBUG,Profile Picture Path:+ mPicture);
}


加密加密=新加密(
new SharedPrefsBackedKeyChain(this),
new SystemNativeCryptoLibrary());


if(!crypto.isAvailable()){
return;
}

OutputStream fileStream = new BufferedOutputStream(
new FileOutputStream(mypath));

OutputStream outputStream = crypto.getCipherOutputStream(
fileStream,新的Entity(Password));

ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.write(bitmapToBytes(photo));

objectOutputStream.close(); //具有异常的行
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}

bitmapToBytes():

  private byte [] bitmapToBytes(Bitmap photo){

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG,100,stream);
byte [] byteArray = stream.toByteArray();

return byteArray;
}

decodeFile():



{pre> 私有位图decodeFile(String filename){

加密加密=新加密(
new SharedPrefsBackedKeyChain(this),
new SystemNativeCryptoLibrary ());

ContextWrapper cw = new ContextWrapper(getApplicationContext());
文件目录= cw.getDir(DIRECTORY,Context.MODE_PRIVATE);
文件文件=新文件(目录,文件名);

try {
FileInputStream fileStream = new FileInputStream(file);
InputStream inputStream = crypto.getCipherInputStream(
fileStream,
new Entity(Password));
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Bitmap bitmap = bytesToBitmap((byte [])objectInputStream.readObject());

返回位图;
} catch(Exception e){
e.printStackTrace();
}

返回null;
}

bytesToBitmap():



私人位图bytesToBitmap(byte [] bytes){

位图位图= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
返回位图;
}

当我尝试保存图像时,我得到一个UnsupportedOperationException在 objectOutputStream.close(); 在saveFile()



Logcat跟踪:

  10-01 16:55:34.529:W / System.err(31291):java.lang.UnsupportedOperationException 
10-01 16:55:34.529: System.err(31291):在com.facebook.crypto.streams.NativeGCMCipherOutputStream.write(NativeGCMCipherOutputStream.java:93)
10-01 16:55:34.529:W / System.err(31291):在java .io.DataOutputStream.writeByte(DataOutputStream.java:144)
10-01 16:55:34.529:W / System.err(31291):在java.io.ObjectOutputStream.drain(ObjectOutputStream.java:394)
10-01 16:55:34.529:W / System.err(31291):java.io.ObjectOutputStream.flush(ObjectOutputStream.java:461)
10-01 16:55:34.529: W / System.err(31291):在java.io.ObjectOutputStream.close(ObjectOutputStream.java:337)
10-01 16:55:34.529:W / System.err(31291):在com.xx .xxx.RegistrationActivity.sa veFile(RegistrationActivity.java:761)
10-01 16:55:34.529:W / System.err(31291):at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java:639)
10-01 16:55:34.529:W / System.err(31291):在android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55:34.529:W /系统.err(31291):at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.529:W / System.err(31291):在android.app.ActivityThread。 handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.529:W / System.err(31291):at android.app.ActivityThread.access $ 1300(ActivityThread.java:135)
10-01 16:55:34.529:W / System.err(31291):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244)
10-01 16:55:34.529:W / System.err(31291):在android.os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.529:W / System.err(31291):在android.os.Looper .loop(Looper.java:136)
10-01 16:55:34.529:W / System.err 31291):at android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.529:W / System.err(31291):在java.lang.reflect.Method.invokeNative (Native Method)
10-01 16:55:34.529:W / System.err(31291):在java.lang.reflect.Method.invoke(Method.java:515)
10-01 16:55:34.529:W / System.err(31291):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 16:55:34.529:W /System.err(31291):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.529:W / System.err(31291):at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.529:W / System.err(31291):java.io.IOException:意外加密版本-1
10- 01 16:55:34.529:W / System.err(31291):在com.facebook.crypto.util.Assertions.checkArgumentForIO(Assertions.java:29)
10-01 16:55:34.539:W / System.err(31291):在com.facebook.crypto.CipherHelper.getCipherInputStream(CipherHelper.java:52)
10-01 16:55:34.539:W / S ystem.err(31291):at com.facebook.crypto.Crypto.getCipherInputStream(Crypto.java:83)
10-01 16:55:34.539:W / System.err(31291):at com.xx .xxx.RegistrationActivity.decodeFile(RegistrationActivity.java:821)
10-01 16:55:34.539:W / System.err(31291):at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java: 640)
10-01 16:55:34.539:W / System.err(31291):at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55: 34.539:W / System.err(31291):在android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.539:W / System.err(31291):在android .app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.539:W / System.err(31291):at android.app.ActivityThread.access $ 1300(ActivityThread.java:135 )
10-01 16:55:34.539:W / System.err(31291):android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244)
10-01 16:55 :34.539:W / System.err(31291):在android .os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.539:W / System.err(31291):在android.os.Looper.loop(Looper.java:136)
10-01 16:55:34.539:W / System.err(31291):在android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.539: W / System.err(31291):at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:55:34.539:W / System.err(31291):在java.lang。 ()方法.java:785)
10-01 16:55:34.549:W / System.err(31291):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.549:W / System.err(31291):at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.549:D / BAT(31291): onResume调用

感谢您的帮助...

解决方案

这是我如何解决问题.. enc解密和解密工作正常。

  //加密图像并保存到目录

public void encodeAndSaveFile(Bitmap photo,int code){

try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
文件目录= cw.getDir(DIRECTORY,Context.MODE_PRIVATE);
文件mypath =新文件(目录,ENCRYPTEDFILENAME);

加密加密=新加密(新的SharedPrefsBackedKeyChain(this),
new SystemNativeCryptoLibrary());

if(!crypto.isAvailable()){
return;
}

OutputStream fileStream = new BufferedOutputStream(
new FileOutputStream(mypath));
OutputStream outputStream = crypto.getCipherOutputStream(
fileStream,新实体(密码));
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();
}
}

//将位图转换为字节
private byte [] bitmapToBytes(Bitmap photo){

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG,100,stream);
byte [] byteArray = stream.toByteArray();
return byteArray;
}

//将字节转换为位图
私有位图bytesToBitmap(byte [] bytes){

位图位图= BitmapFactory.decodeByteArray(bytes, 0,bytes.length);

返回位图;
}

//解码加密文件并返回Bitmap
私有位图decodeFile(String filename){

加密加密=新加密(新SharedPrefsBackedKeyChain(这个),
new SystemNativeCryptoLibrary());

ContextWrapper cw = new ContextWrapper(getApplicationContext());
文件目录= cw.getDir(DIRECTORY,Context.MODE_PRIVATE);
文件文件=新文件(目录,文件名);

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();

位图位图= bytesToBitmap(out.toByteArray());
返回位图;
} catch(Exception e){
e.printStackTrace();
}

返回null;
}

以下是我在onActivityResult中调用encodeAndSaveFile()和decodeFile()的方式(),从相机返回。

  encodeAndSaveFile((Bitmap)data.getExtras()。get(data 
requestCode);
位图decryptptedImage = decodeFile(ENCRYPTEDFILENAME);


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 questions on SO to figure out the reasons for my exceptions, my i couldn't get it working.

Here is what I have done till now...

Integrating: I am using Eclipse and hence, downloaded crypto.jar and libs.zip from here and added the jar file to libs folder and .so files to respective folder inside libs folder.

My scenario:

I have to capture an image from camera, encrypt and store it on my phone memory. Decrypt it and show it in an imageview. At a later stage, i also need to decrypt this image from the memory and send it over network.

So, my code goes as below...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (Const.DEBUGGING) {
        Log.d(Const.DEBUG, "RequestCode: " + requestCode + "\nResultCode:"
                + resultCode);
    }

    int tag = getRecordCount();
    tag++;

    if (requestCode == KTP_PICTURE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            ENCRYPTEDFILENAME = tag + "_" + KTP_TAG + ".png";

            saveFile((Bitmap) data.getExtras().get("data"), requestCode);
            Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
            mImgBtnKtppicture.setImageBitmap(decryptedImage);

        } else if (resultCode == RESULT_CANCELED) {
            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "KTP_RESULT CANCELED");
        } else {

        }
    }

    if (requestCode == PROFILE_PICTURE_REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            ENCRYPTEDFILENAME = tag + "_" + PROFILE_TAG + ".png";

            saveFile((Bitmap) data.getExtras().get("data"), requestCode);
            Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME);
            mImgBtnPicture.setImageBitmap(decryptedImage);

        } else if (resultCode == RESULT_CANCELED) {
            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "PICTURE_RESULT CANCELED");
        } else {

        }

    }
}

SaveFile():

public void saveFile(Bitmap photo, int code) {

    try {
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir(DIRECTORY, Context.MODE_PRIVATE);
        File mypath = new File(directory, ENCRYPTEDFILENAME);

        if (code == KTP_PICTURE_REQUEST_CODE) {
            mKtppicture = Uri.fromFile(mypath).toString();

            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "KTP Picture Path: " + mKtppicture);
        } else if (code == PROFILE_PICTURE_REQUEST_CODE) {
            mPicture = Uri.fromFile(mypath).toString();

            if (Const.DEBUGGING)
                Log.d(Const.DEBUG, "Profile Picture Path: " + mPicture);
        }


        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"));

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.write(bitmapToBytes(photo));

        objectOutputStream.close(); //Line with exception
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

bitmapToBytes():

private byte[] bitmapToBytes(Bitmap photo) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

decodeFile():

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"));
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        Bitmap bitmap = bytesToBitmap((byte[])objectInputStream.readObject());

        return bitmap;
    }catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

bytesToBitmap():

private Bitmap bytesToBitmap(byte[] bytes) {

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return bitmap;
}

When i try to save the image, i get a UnsupportedOperationException at objectOutputStream.close(); in saveFile()

Logcat Trace:

10-01 16:55:34.529: W/System.err(31291): java.lang.UnsupportedOperationException
10-01 16:55:34.529: W/System.err(31291):    at com.facebook.crypto.streams.NativeGCMCipherOutputStream.write(NativeGCMCipherOutputStream.java:93)
10-01 16:55:34.529: W/System.err(31291):    at java.io.DataOutputStream.writeByte(DataOutputStream.java:144)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.drain(ObjectOutputStream.java:394)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:461)
10-01 16:55:34.529: W/System.err(31291):    at java.io.ObjectOutputStream.close(ObjectOutputStream.java:337)
10-01 16:55:34.529: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.saveFile(RegistrationActivity.java:761)
10-01 16:55:34.529: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java:639)
10-01 16:55:34.529: W/System.err(31291):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-01 16:55:34.529: W/System.err(31291):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.529: W/System.err(31291):    at android.os.Looper.loop(Looper.java:136)
10-01 16:55:34.529: W/System.err(31291):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.529: W/System.err(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:55:34.529: W/System.err(31291):    at java.lang.reflect.Method.invoke(Method.java:515)
10-01 16:55:34.529: W/System.err(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 16:55:34.529: W/System.err(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.529: W/System.err(31291):    at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.529: W/System.err(31291): java.io.IOException: Unexpected crypto version -1
10-01 16:55:34.529: W/System.err(31291):    at com.facebook.crypto.util.Assertions.checkArgumentForIO(Assertions.java:29)
10-01 16:55:34.539: W/System.err(31291):    at com.facebook.crypto.CipherHelper.getCipherInputStream(CipherHelper.java:52)
10-01 16:55:34.539: W/System.err(31291):    at com.facebook.crypto.Crypto.getCipherInputStream(Crypto.java:83)
10-01 16:55:34.539: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.decodeFile(RegistrationActivity.java:821)
10-01 16:55:34.539: W/System.err(31291):    at com.xx.xxx.RegistrationActivity.onActivityResult(RegistrationActivity.java:640)
10-01 16:55:34.539: W/System.err(31291):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-01 16:55:34.539: W/System.err(31291):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 16:55:34.539: W/System.err(31291):    at android.os.Looper.loop(Looper.java:136)
10-01 16:55:34.539: W/System.err(31291):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-01 16:55:34.539: W/System.err(31291):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 16:55:34.539: W/System.err(31291):    at java.lang.reflect.Method.invoke(Method.java:515)
10-01 16:55:34.539: W/System.err(31291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 16:55:34.549: W/System.err(31291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 16:55:34.549: W/System.err(31291):    at dalvik.system.NativeStart.main(Native Method)
10-01 16:55:34.549: D/BAT(31291): onResume called

Thanks for your help...

解决方案

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);

这篇关于Facebook隐藏 - 图像加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆