对多个文件执行功能

时间:2018-08-27 10:44:11

标签: java android encryption

我正在尝试在Android上使用以下代码对文件夹中的多个视频文件进行加密。但是我当前的代码无法正常工作。该应用程序将加密一个文件并停止。从本质上讲,该代码应该能够浏览给定文件夹中的文件列表,并对每个文件进行加密。非常感谢我在下面提供的帮助。

package in.org.connected.hercules;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button encryptbutton = (Button) findViewById(R.id.button1);

    encryptbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new EncryptTask().execute();

        }
    });
}


static void encrypt() throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    File dir = new File("/mnt/usbhost0");
    if (dir.isDirectory()){
        for(File file : dir.listFiles())
        {
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            String fileName = file.getName();// This stream write the encrypted text. This stream will be wrapped by
            // another stream.
            FileOutputStream fos = new FileOutputStream("/mnt/usbhost0/(enc)" + fileName);

            // Length is 16 byte
            SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
                    "AES");
            // Create cipher
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            // Wrap the output stream
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            // Write bytes
            int b;
            byte[] d = new byte[16384];
            while ((b = fis.read(d)) != -1) {
                cos.write(d, 0, b);
            }
            // Flush and close streams.
            cos.flush();
            cos.close();
            fis.close();
            }
        }
    }
    public class EncryptTask extends AsyncTask<String, String, String> {
        ProgressDialog pd;

        @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Encrypting your video");
        pd.show();

    }


    @Override
    protected String doInBackground(String... params) {
        try {
            encrypt();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.dismiss();

    }
}
}

1 个答案:

答案 0 :(得分:0)

很明显,您仅在一个线程(EncryptTask)上对视频进行顺序加密。 对于每个文件,您需要一个离散线程进行加密。像这样更改代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button encryptbutton = (Button) findViewById(R.id.button1);

    encryptbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            encrypt();
        }
    });
}


static void encrypt() throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    File dir = new File("/mnt/usbhost0");
    if (dir.isDirectory()){
        for(File file : dir.listFiles())
        {
            new EncryptTask(file).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }
    public class EncryptTask extends AsyncTask<String, String, String> {

        @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


    @Override
    protected String doInBackground(String... params) {
        //Do encrypt file
        return null;
    }

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