C# 解密m3u8 ts视频文件(AES-128)为mp4

 

文件需要:

1、m3u8文件  32225.m3u8

2、key文件 88d73ccaf175e3ff.ts  

3、加密后无法观看的ts视频文件  322251.ts 

代码1: 

 

 try
 {
     //1 获取m3u8中的iv信息
     string m3u8encodeKeyContent = File.ReadAllText("C:\\Users\\admin\\Downloads\\32225.m3u8");
     //2 读取m3u8头信息指向的加密key文件
     byte[] encodeBufferKey = File.ReadAllBytes("C:\\Users\\admin\\Downloads\\88d73ccaf175e3ff.ts");
     //3 加密ts视频的路径
     byte[] encodeBuffer = File.ReadAllBytes("C:\\Users\\admin\\Downloads\\322251.ts");
     //4 解密ts视频为mp4保存视频路径
     string saveFileName = "D:\\322251解密后的视频.mp4";

     string pattern = @"IV=0x([A-Fa-f0-9]+)";
     Match match = Regex.Match(m3u8encodeKeyContent, pattern);
     string ivValue = "";
     if (match.Success)
     {
         ivValue = match.Groups[1].Value; // 捕获组的内容
     }
     else
     {
         throw new ArgumentException("m3u8 文件异常");
     }
     string key = Convert.ToHexString(encodeBufferKey);
     byte[] decodeBuffer = Decrypt(encodeBuffer, key, ivValue);
     File.WriteAllBytes(saveFileName, decodeBuffer);
 }
 catch (Exception ex)
 {
     Debug.WriteLine(ex.Message);
     Console.WriteLine(ex.Message);
 }

 

代码2

 

        public static byte[] Decrypt(byte[] cipherTextBytes, string key16, string iv16)
        {   // 确保字符串长度是偶数
            if (key16.Length % 2 == 1)
            {
                throw new ArgumentException("The hex string length must be even.");
            }
            if (iv16.Length % 2 == 1)
            {
                throw new ArgumentException("The hex string length must be even.");
            }

            byte[] buffkey = new byte[key16.Length / 2];
            for (int i = 0; i < key16.Length; i += 2)
            {
                var tempkey = key16.Substring(i, 2);
                buffkey[i / 2] = Convert.ToByte(tempkey, 16);
            }
            byte[] buffiv = new byte[key16.Length / 2];
            for (int i = 0; i < iv16.Length; i += 2)
            {
                var tempkey = iv16.Substring(i, 2);
                buffiv[i / 2] = Convert.ToByte(tempkey, 16);
            }
            // 确保密钥和IV长度正确(128位,即16字节)
            byte[] keyBytes = buffkey.ToArray();
            byte[] ivBytes = buffiv.ToArray();

            // 创建RijndaelManaged对象用于解密
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                aes.Key = keyBytes;
                aes.IV = ivBytes;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                // 创建解密器
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                // 创建MemoryStream以用于解密
                using (MemoryStream msDecrypt = new MemoryStream(cipherTextBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        // 读取解密后的字节
                        byte[] decryptedBytes = new byte[cipherTextBytes.Length];
                        csDecrypt.Read(decryptedBytes, 0, decryptedBytes.Length);

                        return decryptedBytes;
                    }
                }
            }
        }

 查看

D:\\322251解密后的视频.mp4 

====

写个C#控制台,粘贴程序,修改参数就可以跑了 

 ===

附录一张图片

 

基于.net avaloniaui写了个跨平台的解码客户端,只打包了win64版本,下载链接地址:

 

https://github.com/xyyhqq/m3u8_aes128_decode/releases/tag/1.0

 思路参考来源:

https://www.52pojie.cn/thread-1194097-1-1.html

 

posted @ 2024-04-20 14:54  JohnnyLei  阅读(505)  评论(0)    收藏  举报