2014-10-01 98 views
5

我試圖使用Facebook隱藏庫加密和解密圖像。這是我第一次使用它,因此如果它是微不足道的,我就會忍受。我已經看過其他的問題來找出我的例外的原因,我不能得到它的工作。Facebook隱藏 - 圖像加密和解密

這裏是我做了什麼至今...

整合:我使用Eclipse,因此,下載crypto.jar和libs.zip從here並添加jar文件到libs文件夾和.so文件到libs文件夾內的各個文件夾。

我的情景:

我要捕捉攝像頭,加密的圖像,並將其存儲在我的手機內存。解密它並在imageview中顯示它。在稍後階段,我還需要從內存中解密此圖像並通過網絡發送。

所以,我的代碼放在如下...

@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; 
} 

當我嘗試保存圖像,我在saveFile的在objectOutputStream.close();得到一個UnsupportedOperationException異常()

logcat的跟蹤:

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 

感謝您的幫助。 ..

+0

的OutputStream的OutputStream = crypto.getCipherOutputStream( FILESTREAM,新的實體( 「密碼」)); ..是這樣做的正確方法。我認爲最後一個參數Entity是密碼。所以,爲了測試,我使用「密碼」 – 2014-10-01 11:34:57

回答

3

下面是我如何解決這個問題..現在加密和解密工作正常。

// 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; 
} 

這裏是我如何打電話encodeAndSaveFile()和decodeFile(),在onActivityResult(),從相機返回後。

encodeAndSaveFile((Bitmap) data.getExtras().get("data"), 
         requestCode); 
Bitmap decryptedImage = decodeFile(ENCRYPTEDFILENAME); 
+0

,這個例外的原因是什麼,請您詳細說明一下。 – SimpleCoder 2016-02-04 11:09:27

+0

這差不多有一年半的時間了!現在不記得了.. – 2016-02-04 11:12:26

+0

我仍然繼續字節解密,仍然得到這個錯誤。這裏的問題和答案的代碼可能不同。 – SimpleCoder 2016-02-04 11:14:18