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1NJRyJ9.vvYKPY9tyBnhGGnNtY-XLy_Hz5cyJx5Ma2APVv0g5dk
#EXTINF:12.000,
s-4-v1-a1.ts?wmsAuthSign=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbiI6IjdjY2I5ZTAxZGI1YzVkM2IzMWUzMzc1NzQ3MjZjNjYwIiwiZXhwIjoxNTEzMTU4ODM5LCJpc3MiOiJTYWJhIElkZWEgR1NJRyJ9.vvYKPY9tyBnhGGnNtY-XLy_Hz5cyJx5Ma2APVv0g5dk

we have both enc.key file and the server response to 'enc.key' that is DVQpuWrxZLd2nCTTxAysIg==

we are using these C# functions for decryption but we don't know how to find salt or block size ? or basically we are doing it in correct way ?

 public static void  DecryptFile(string inputFile, string output,string password)
        {

            byte[] bytesToBeDecrypted = File.ReadAllBytes(inputFile);
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
            byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);


            File.Delete(inputFile);


            File.WriteAllBytes(output, bytesDecrypted);
        }


        public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
        {
            byte[] decryptedBytes = null;

            // Set your salt here, change it to meet your flavor:
            // The salt bytes must be at least 8 bytes.
            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = 128;
                    AES.BlockSize = 128;

                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);

                    AES.Mode = CipherMode.CBC;
                    AES.Padding = PaddingMode.PKCS7;
                    using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }
                    decryptedBytes = ms.ToArray();
                }
            }

            return decryptedBytes;
        }
1
DVQpuWrxZLd2nCTTxAysIg== looks base-64 encoded to me, so you'll need to decode it rather than use UTF8.GetBytes(). As far as the salt/hash size goes, that's something specific to the format of m3u8 files that's presumably in the spec.Dylan Nicholson

1 Answers

3
votes

There was already a similar quesion here (unable to find it back)

how to find salt or block size

Look at your EXT-X-KEY header. It states AES-128, so you will have to use AES-128 (it's the key size, block size is always 128 bit).

According to the RFC by default CBC mode with Pkcs7 padding is used.

The section 4.3.2.4. EXT-X-KEY further states: the Initialization Vector (IV) attribute value or the Media Sequence Number as the IV

So the IV should be present in the EXT-X-KEY header. If not, the sequence number is used (which is terrible idea for CBC, but this is how it is).

or basically we are doing it in correct way

Basically it looks ok (except the salt and key). I am not sure what is the encryption key. The encoded response from the server has 16 bites (128 bits) so I'd assume that can be the key (that you will have to find out yourself)