Search code examples
c#testingencryptionpowershellaes

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. 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.

I have absolutely no experience with this stuff so feel free to treat me like a child.

Thanks


Solution

  • I eventually made a c# command line.

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