2014-10-27 56 views
-1

任何人都可以告訴我我的代碼中做錯了什麼!雖然我沒有收到任何錯誤。圖像文件被加密,但當我解密時,我無法查看圖像。它仍然顯示爲一個加密的。如我錯了請糾正我。我不熟悉這種加密和解密。以前感謝。Android中的多個文件的加密和解密

如果你投下了票,你最好告訴我什麼是錯的,爲什麼你這樣做的原因!

public class MainActivity extends Activity { 

Button button1, button2; 

String KEY = "MyKey"; 
SparseArray<byte[]> array = new SparseArray<byte[]>(); 
SparseArray<byte[]> decryptArray = new SparseArray<byte[]>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      try { 
       new EncryptAsyncTask().execute(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    button2 = (Button) findViewById(R.id.button2); 
    button2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      try { 
       new DecryptAsyncTask().execute(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 


    public class EncryptAsyncTask extends AsyncTask<String, String, String>{ 

    ProgressDialog mDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mDialog = ProgressDialog.show(MainActivity.this, "", "Please wait"); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     byte[] incrept = null; 
     try { 
      getImageFile(); 
      if(array!=null && array.size()>0){ 
       for(int i=0 ; i<array.size() ; i++){ 
        byte[] byteArray = array.get(i); 
        incrept = encrypt(KEY, byteArray); 
        FileOutputStream fos = null; 
         try { 
          fos = new FileOutputStream(new File(
            Environment.getExternalStorageDirectory()+File.separator 
            +"EncryptedImages"+File.separator+i+"_Image.jpg")); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
         try { 
          fos.write(incrept); 
         } catch (IOException e1) { 
          e1.printStackTrace(); 
         } 
         try { 
          fos.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
       } 
      }    
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     if(mDialog.isShowing()){ 
      mDialog.dismiss(); 
     } 
    } 
} 

public class DecryptAsyncTask extends AsyncTask<String, String, String>{ 

    ProgressDialog mDialog; 

    @Override 
protected void onPreExecute() { 
super.onPreExecute(); 
    mDialog = ProgressDialog.show(MainActivity.this, "", "Please wait"); 
} 

    @Override 
    protected String doInBackground(String... params) { 
     byte[] incrept = null; 
     try { 
      getImageFileFromSdCard(); 
      if(decryptArray!=null && decryptArray.size()>0){ 
       for(int i=0 ; i<decryptArray.size() ; i++){ 
        byte[] byteArray = decryptArray.get(i); 
        incrept = decrypt(KEY, byteArray);       
        FileOutputStream fos = null; 
         try { 
          fos = new FileOutputStream(new File(
            Environment.getExternalStorageDirectory()+File.separator 
            +"DecryptedImages"+File.separator+i+"_Image.jpg")); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
         try { 
          fos.write(incrept); 
         } catch (IOException e1) { 
          e1.printStackTrace(); 
         } 
         try { 
          fos.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
       } 
      }    
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    public byte[] getImageFile() throws FileNotFoundException 
{ 
    byte[] Image_data = null; 
    byte[] arry = null; 
    try { 
     File file = new File(Environment.getExternalStorageDirectory()+File.separator+"Images"+File.separator); 
     if(file.exists() && file.isDirectory()){ 
      File[] listOfImages = file.listFiles(); 
      if(listOfImages.length>0){ 
       for(int i=0; i<listOfImages.length; i++){ 
        byte[] inarry = null; 
        InputStream is = new BufferedInputStream(new FileInputStream(listOfImages[i])); 
        int length = is.available(); 
         Image_data = new byte[length]; 

         int bytesRead; 
         ByteArrayOutputStream output = new ByteArrayOutputStream(); 
         while ((bytesRead = is.read(Image_data)) != -1) 
         { 
          output.write(Image_data, 0, bytesRead); 
         } 
         inarry = output.toByteArray(); 
         array.put(i, inarry); 
         is.close(); 
       } 
      } 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

return arry; 
} 

public void getImageFileFromSdCard() throws FileNotFoundException 
{ 
try { 
    File file = new   File(Environment.getExternalStorageDirectory()+File.separator+"EncryptedImages"+File.separator); 
    if(file.exists() && file.isDirectory()){ 
     File[] listOfFiles = file.listFiles(); 
     if(listOfFiles.length>0){ 
      for(int i=0 ; i<listOfFiles.length ; i++){ 
       FileInputStream fileInputStream = new FileInputStream(listOfFiles[i]); 
       byte[] bFile = new byte[(int) listOfFiles[i].length()]; 
       fileInputStream.read(bFile); 
       fileInputStream.close(); 
       decryptArray.put(i, bFile); 
      } 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
} 

public byte[] encrypt(String seed, byte[] cleartext) throws Exception { 

    byte[] rawKey = getRawKey(seed.getBytes()); 
     byte[] result = encrypt(rawKey, cleartext); 
     return result; 
} 

public byte[] decrypt(String seed, byte[] encrypted) throws Exception { 
     byte[] rawKey = getRawKey(seed.getBytes()); 
     byte[] enc = encrypted; 
     byte[] result = decrypt(rawKey, enc); 

     return result; 
} 

private byte[] getRawKey(byte[] seed) throws Exception { 
     KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
     SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); 
     sr.setSeed(seed); 
    kgen.init(128, sr); 
    SecretKey skey = kgen.generateKey(); 
    byte[] raw = skey.getEncoded(); 
    return raw; 
} 


private byte[] encrypt(byte[] raw, byte[] clear) throws Exception { 
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 
     Cipher cipher = Cipher.getInstance("AES"); 
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 
    byte[] encrypted = cipher.update(clear); 
     return encrypted; 
} 

private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { 
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 
     Cipher cipher = Cipher.getInstance("AES"); 
    cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
    byte[] decrypted = cipher.update(encrypted); 
     return decrypted; 
} 
} 
+0

您的代碼對於您要做的事情而言非常複雜。也許從一個簡單的字節數組開始,學習如何加密/解密該數組,然後添加文件處理。目前很難確定你的代碼是否可以在沒有運行和調試的情況下進行翻轉,這很費勁 – CharlieS 2014-10-27 23:27:37

回答

0

你很難搞清楚你想做什麼。 從代碼看來,您正在創建一個包含目錄中所有文件的字節數組...

您認爲如何加密和解密並將其拆分迴文件?

daniel

+0

我經歷了幾組代碼塊並且跟着它們。也許我錯了。但是這種加密和解密仍然讓我感到困惑。你可以幫我嗎! @danysz – 2014-10-27 06:05:14

0

您正在爲加密和解密操作生成兩個不同的密鑰。您使用相同的種子,但使用不同的MODE。

AES是對稱的,您使用相同的密鑰進行解密以進行加密。

+0

我該怎麼做@charlieS? – 2014-10-27 06:11:34

+0

更改getRawKey,因此它只會生成一個新的,然後在隨後的調用中重新使用相同的密鑰。 – CharlieS 2014-10-27 06:35:58