解密AES加密的.ts文件

时间:2013-01-12 02:51:13

标签: android http-streaming

我想在Android中使用AES 128位加密简单地解密加密的ts文件。 我知道如果我玩m3u8然后玩家可以照顾这个,但我想直接访问ts并想要单独播放,所以需要在播放之前解密它。

让我知道适用的Java类。

1 个答案:

答案 0 :(得分:1)

假设您知道用于加密文件的密钥,您可以使用以下内容:

public static void decrypt() {
    try {
        Log.d(C.TAG, "Decrypt Started");

        byte[] bytes = new BigInteger(<your key>, 16).toByteArray();

        FileInputStream fis = new FileInputStream(<location of encrypted file>);

        FileOutputStream fos = new FileOutputStream(<location of decrypted file>);
        SecretKeySpec sks = new SecretKeySpec(bytes, <encryption type>);
        Cipher cipher = Cipher.getInstance(<encryption type>);
        cipher.init(Cipher.DECRYPT_MODE, sks);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int b;
        byte[] d = new byte[8];
        while ((b = cis.read(d)) != -1) {
            fos.write(d, 0, b);
        }
        fos.flush();
        fos.close();
        cis.close();
        Log.d(C.TAG, "Decrypt Ended");
    } catch (NoSuchAlgorithmException e) {
        Log.d(C.TAG, "NoSuchAlgorithmException");
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        Log.d(C.TAG, "InvalidKeyException");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d(C.TAG, "IOException");
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        Log.d(C.TAG, "NoSuchPaddingException");
        e.printStackTrace();
    }
}

使用适合您文件的内容替换<>之间的所有内容,您就可以了。