我想使用DES实现一个用于mp4文件加密和解密的模块。
它成功并且解密文件可以在桌面应用程序中与原始音轨完全相同但在Android中却无法播放?为什么?
以下是我的代码 (模块)
private static final String key = "v@!#1SF5~6A5XZE3";
public static void encrypt(InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
}
public static void doCopy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[8192];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
应用程序(加密):
stopService(new Intent(AlaramScheduleActivity.this, RecordService.class));
File instanceRecordDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "Test");
if(instanceRecordDirectory.exists()){
FileInputStream fis = new FileInputStream(instanceRecordDirectory.getAbsolutePath() + File.separator + "test.mp4");
FileOutputStream fos = new FileOutputStream(instanceRecordDirectory.getAbsolutePath() + File.separator + "etest.mp4");
AESClientEncodeDecode.encrypt(fis, fos);
}
申请(Decrpytion):
File instanceRecordDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "Test");
File decryptF = null;
if(instanceRecordDirectory.exists()){
FileInputStream fis2 = new FileInputStream(instanceRecordDirectory.getAbsolutePath() + File.separator + "etest.mp4");
FileOutputStream fos2 = new FileOutputStream(instanceRecordDirectory.getAbsolutePath() + File.separator + "dtest.mp4");
AESClientEncodeDecode.decrypt(fis2, fos2);
}
decryptF = new File(instanceRecordDirectory.getAbsolutePath() + File.separator + "dtest.mp4");
if(decryptF.exists()){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(decryptF), "audio/*");
startActivity(intent);
}else{
toast("Recordings does not exists");
}