[英]programmatically determine if *.ts video file is encrypted with aes 128
I want to write a simple automated test to determine if a .ts video file has been encrypted using AES 128 encryption.我想编写一个简单的自动化测试来确定 .ts 视频文件是否已使用 AES 128 加密进行加密。 I will have access to both the encrypted and unencrypted files.
我将可以访问加密和未加密的文件。 I will also have access to the key.
我也可以使用钥匙。 I will pretty much have access to everything because I am working with the developers doing the encryption :)
我几乎可以访问所有内容,因为我正在与进行加密的开发人员合作:)
I'd prefer to do a test more advanced than just checking to see if the file sizes are different.我更愿意进行更高级的测试,而不仅仅是检查文件大小是否不同。
Any thoughts on some simple test code I could write?关于我可以编写的一些简单测试代码的任何想法? I'd be writing the code with c# or powershell.
我会用 c# 或 powershell 编写代码。
I have absolutely no experience with this stuff so feel free to treat me like a child.我对这些东西完全没有经验,所以请把我当作孩子一样对待。
Thanks谢谢
What's the real reason behind this?这背后的真正原因是什么? So you don't encrypt a file twice, or decrypt twice?
所以你不加密一个文件两次,或者解密两次? Perhaps there's a better solution if we know the requirements.
如果我们知道需求,也许会有更好的解决方案。
However, based on what I see so far, it seems like you'll have to attempt to decrypt the file and if it fails, assume it's not encrypted … but this could be very time consuming.但是,根据我目前看到的情况,您似乎必须尝试解密文件,如果失败,则假设它没有加密……但这可能非常耗时。 I am not sure if there's any other way, other than opening the file for reading, read a line and see if there is any plaintext, assuming you know what plaintext to compare it against.
我不确定是否还有其他方法,除了打开文件进行阅读,阅读一行并查看是否有任何明文,假设您知道要与哪些明文进行比较。
If you are attempting to test if encryption/decryption is working properly, then you could take an input file with known plaintext, encrypt it with the correct key, then decrypt it twice - once with the correct key, the second time with an invalid key.如果您试图测试加密/解密是否正常工作,那么您可以使用已知明文的输入文件,使用正确的密钥对其进行加密,然后解密两次 - 一次使用正确的密钥,第二次使用无效的密钥. Compare the results 3 ways.
比较结果 3 种方式。
If the TS container is entirely encrypted then it would probably be more efficient to see if the file is a valid MPEG-TS file instead of trying to figure out if it's encrypted or not.如果 TS 容器完全加密,那么查看文件是否是有效的 MPEG-TS 文件可能会更有效,而不是试图确定它是否已加密。 If it's invalid, assume it's encrypted.
如果无效,则假定它已加密。 You can read the first couple of bytes of the file to validate the format.
您可以读取文件的前几个字节来验证格式。 The format (or "magic numbers") is documented here:
格式(或“幻数”)记录在此处:
http://en.wikipedia.org/wiki/MPEG_transport_stream#Packet http://en.wikipedia.org/wiki/MPEG_transport_stream#Packet
Hope this helps.希望这可以帮助。
I eventually made ac# command line.我最终制作了 ac# 命令行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("<key> <iv> <encryptedfile> <outputdecryptedfile>");
Environment.Exit(-1);
}
//Console.ReadLine();
byte[] encryptionKey = StringToByteArray(args[0]);
byte[] encryptionIV = StringToByteArray(args[1]);
try
{
using (FileStream outputFileStream = new FileStream(args[3], FileMode.CreateNew))
{
using (FileStream inputFileStream = new FileStream(args[2], FileMode.Open))
{
using (var aes = new AesManaged { Key = encryptionKey, IV = encryptionIV, Mode = CipherMode.CBC })
using (var encryptor = aes.CreateDecryptor())
using (var cryptoStream = new CryptoStream(inputFileStream, encryptor, CryptoStreamMode.Read))
{
cryptoStream.CopyTo(outputFileStream);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.