private void HandleKey(SubStreamSegment segment, IEnumerable <ExtKeyTagInstance> extKeys, CancellationToken cancellationToken) { ExtKeyTagInstance[] extKeyTagInstanceArray = extKeys.ToArray(); if (extKeyTagInstanceArray.Length < 1) { return; } string url = null; byte[] iv = null; foreach (ExtKeyTagInstance extKeyTagInstance in extKeyTagInstanceArray) { var b = extKeyTagInstance.AttributeObject(ExtKeySupport.AttrMethod); if (string.Equals("NONE", b, StringComparison.OrdinalIgnoreCase)) { url = null; } else { if (!string.Equals("AES-128", b, StringComparison.OrdinalIgnoreCase)) { if (string.Equals("SAMPLE-AES", b, StringComparison.OrdinalIgnoreCase)) { throw new NotImplementedException("Method SAMPLE-AES decryption is not implemented"); } throw new NotSupportedException("Unknown decryption method type: " + b); } string str = extKeyTagInstance.AttributeObject(ExtKeySupport.AttrUri); if (null != str) { url = str; } byte[] numArray = extKeyTagInstance.AttributeObject(ExtKeySupport.AttrIv); if (null != numArray) { iv = numArray; } } } if (null == url) { return; } if (null == iv) { iv = new byte[16]; long num = segment.MediaSequence ?? _segmentIndex - 1; iv[15] = (byte)((ulong)num & byte.MaxValue); iv[14] = (byte)((ulong)(num >> 8) & byte.MaxValue); iv[13] = (byte)((ulong)(num >> 16) & byte.MaxValue); iv[12] = (byte)((ulong)(num >> 24) & byte.MaxValue); } Func <Stream, CancellationToken, Task <Stream> > filter = segment.AsyncStreamFilter; Uri uri = _parser.ResolveUrl(url); segment.AsyncStreamFilter = async(stream, ct) => { if (null != filter) { stream = await filter(stream, ct).ConfigureAwait(false); } byte[] key; if (!_keyCache.TryGetValue(uri, out key)) { key = await LoadKeyAsync(uri, cancellationToken).ConfigureAwait(false); if (16 != key.Length) { throw new FormatException("AES-128 key length mismatch: " + key.Length); } _keyCache[uri] = key; } Debug.WriteLine("Segment AES-128: key {0} iv {1}", BitConverter.ToString(key), BitConverter.ToString(iv)); return(_platformServices.Aes128DecryptionFilter(stream, key, iv)); }; }