0

我想使用 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);
        }           

应用程序(解密):

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");
    }
4

0 回答 0