Я пытаюсь зашифровать несколько видеофайлов в папке с кодом ниже на 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();
}
}
}