简体   繁体   中英

Android Java decryption - saving the decrypted file

So I'm working on an application where I need to decrypt encrypted files.The encryption is done with PHP and decryption with Java.I've tested,there is no problem with the encryption/decryption in Java program.But my problem is that I can't run the same application in Android.I need to find away how to get the encrypted data.For now i prefer to store it in assets folder manually, just for tests and get it like this :

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

After that I need to save the decrypted file somewhere so I can see if decryption is working on the device too.So basically I need to find a way how to get the encrypted file from assets folder as example and where to store the decrypted file (dowsn't matter where).Here is the code that I'm using for decryption :

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 any help or suggestions how to do this are welcomed.

It's unclear what you are asking.

If you are trying to read a file from the assets folder there are several examples on SO.

Android Assets No Value Read?

Why not just use the file system rather than the assets folder in the apk?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM