-3

I want to simply decrypt the encrypted ts files using AES 128 bit encryption in Android. I know that If I play m3u8 then Player can take care of this but I want direct access of ts and want to play those individually so need to decrypt it before playing.

Let me know suitable Java classes available for same.

Cœur
  • 37,241
  • 25
  • 195
  • 267
PGU
  • 495
  • 6
  • 21
  • So this means I cant Ask questions in future because of my Past mistakes? – PGU Jan 12 '13 at 02:58
  • NO, it means people will not be encouraged to help you. – Mitch Wheat Jan 12 '13 at 02:58
  • Here is some sort of similar question http://stackoverflow.com/questions/6557909/aes-encrypt-decrypt-the-video-audio-file-and-play-the-decrypted-file?rq=1 So how that is accepted? Its just mentality of Peoples whether to accept it or not.. – PGU Jan 12 '13 at 03:03
  • @PGU: **First:** It already has 3 close votes. And plus, it has not been answered so far. **Second:** Just because other questions went unnoticed doesn't make them right. – Siddharth Lele Jan 12 '13 at 04:04

1 Answers1

1

Assuming you know the key that was used to encrypt the file, you can use the following:

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();
    }
}

Replace everything between < and > with the appropriate things for your file, and you're good to go.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks for the snippet.I do have IV for AES, so that can be used as Key? I am new to AES so please let me know. – PGU Jan 12 '13 at 03:05