簡體   English   中英

Android Java解密-保存解密的文件

[英]Android Java decryption - saving the decrypted file

所以我在一個需要解密加密文件的應用程序上工作。加密是通過PHP完成的,然后使用Java解密。我已經測試過,Java程序中的加密/解密沒有問題。但是我的問題是我不能在Android中運行相同的應用程序。我需要找到如何獲取加密數據的方法。現在我更喜歡手動將其存儲在Assets文件夾中,僅用於測試並按如下方式獲取它:

AssetManager am = this.getAssets();
InputStream is = am.open("AUDIOVISUALFOTO02.pdf"); //or image file

之后,我需要將解密的文件保存在某個地方,以便查看解密是否也在設備上正常工作。不管在哪里)。以下是我用於解密的代碼:

package com.android.decrypt;

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

    public class DecryptPDFActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        //FileInputStream fis   = new FileInputStream(new File("encrypted.pdf"));
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        FileOutputStream fos  = new FileOutputStream(new File("decrypted.pdf"));

        byte[] b = new byte[8];
        int i;

        while ((i = cis.read(b)) != -1) {
          fos.write(b, 0, i);
        }
        fos.flush(); fos.close();
        cis.close(); fis.close();

        }
        catch(Exception e)
        {
            e.fillInStackTrace();
            Log.e("Error", "Damned ! : "+e);
        }
      }
}

因此,歡迎您提供任何幫助或建議。

不清楚你在問什么。

如果您嘗試從資產文件夾中讀取文件,那么有幾個關於SO的示例。

Android資產沒有價值讀取?

為什么不只使用文件系統,而不要使用apk中的資產文件夾?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM