2015-12-18 76 views
0

有沒有一種方法可以將我的字節文件轉換爲文件輸入流,所以我可以解密它之前我將它轉換爲位圖。我試圖將其更改爲fileinputstream,然後再次,但它似乎並沒有工作。轉換字節到文件輸入流

ParseFile fileObject = (ParseFile) object.get("ImageFile");fileObject.getDataInBackground(new GetDataCallback() { 

    public void done(byte[] data, ParseException e) { 
     if (e == null) { 
      Log.d("test", 
      "We've got data in data."); 
      FileInputStream data1 = new FileInputStream(data); 
      // Decode the Byte[] into 
      // Bitmap 
      Bitmap bmp = BitmapFactory.decodeByteArray(decrypt(key, data), 0, data.length); 
      // Get the ImageView from 
      // main.xml 
      ImageView imgFile = (ImageView) findViewById(R.id.image); 

      // Set the Bitmap into the 
      // ImageView 
      imgFile.setImageBitmap(bmp); 

      // Close progress dialog 
      progressDialog.dismiss(); 

我的解密類需要字節的FileInputStream作爲參數

私人字節[]解密(字節[] SKEY,的FileInputStream FIS){

SecretKeySpec skeySpec = new SecretKeySpec(skey, "AES"); 
    Cipher cipher; 
    byte[] decryptedData=null; 
    CipherInputStream cis=null; 
    try { 
     cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv)); 
     // Create CipherInputStream to read and decrypt the image data 
     cis = new CipherInputStream(fis, cipher); 
     // Write encrypted image data to ByteArrayOutputStream 
     ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
     byte[] data = new byte[2048]; 
     while ((cis.read(data)) != -1) { 
      buffer.write(data); 
     } 
     buffer.flush(); 
     decryptedData=buffer.toByteArray(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     try { 
      fis.close(); 
      cis.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    return decryptedData; 
} 

回答

4

你似乎在問到這個問題:

public void done(byte[] data, ParseException e) { 
    ... 
    FileInputStream data1 = new FileInputStream(data); 

哪(顯然)不會編譯。如果您確實想要爲字節數組中的數據打開FileInputStream,則需要將數據寫入文件系統中的(臨時)文件,然後爲該文件創建一個FileInputStream。這很麻煩。

一個更好的想法是改變

private byte[] decrypt(byte[] skey, FileInputStream fis) { 

private byte[] decrypt(byte[] skey, InputStream fis) { 

然後打開ByteArrayInputStream到您的data1陣列並將其傳遞到decrypt

+0

是不是解析傳遞文件作爲字節?所以我怎麼不能通過它來解密! – Anasbzr

+0

因爲......你已經定義了你的'decrypt'方法,所以不起作用!如果你改變'decrypt'方法將數據作爲'byte []',那麼你可以*將數據作爲'byte []'傳遞。但是你需要在'decrypt'方法中創建一個ByteArrayInputStream ...因爲'CipherInputStream'必須用某種'InputStream'實例化。 –