2

I have some videos that i encrypted to protect them with this code :

Encrypted Code :

private void EncryptFile(string inputFile, string outputFile)
    {
        try
        {

            string password = @"MyKey123"; // Your Key Here
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(inputFile, FileMode.Open);

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);


            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
        }
        catch
        {
            MessageBox.Show("Encryption failed!", "Error");
        }
    }

Now i want to play my encrypted videos from memory. I had researched a lot and know that i shoud use DirectShow to play video from memory and can to find this article(https://www.codeproject.com/Articles/2632/DirectShow-MediaPlayer-in-C).

Now i don`t figure out how can i decrypt file and play it.

Decrypted Code :

private void DecryptFile(string inputFile, string outputFile)
    {

        {
            string password = @"MyKey123"; // Your Key Here

            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateDecryptor(key, key),
                CryptoStreamMode.Read);

            FileStream fsOut = new FileStream(outputFile, FileMode.Create);

            int data;
            while ((data = cs.ReadByte()) != -1)
                fsOut.WriteByte((byte)data);

            fsOut.Close();
            cs.Close();
            fsCrypt.Close();

        }
    }

How can i play encrypted video from memory with DirectShow?

Roman R.
  • 68,205
  • 6
  • 94
  • 158
topcool
  • 2,498
  • 7
  • 28
  • 52
  • DirectShow is ancient. Are you sure you don't want to use a modern alternative? – zx485 Jan 25 '18 at 09:49
  • @zx485 Is there any modern or better way? – topcool Jan 25 '18 at 09:50
  • [Media Foundation](https://en.wikipedia.org/wiki/Media_Foundation) is a COM-based multimedia framework pipeline and infrastructure platform for digital media in Windows Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10. – zx485 Jan 25 '18 at 09:56
  • @zx485 thanks a lot, Is there any tutorial to use Media Foundation in c#? – topcool Jan 25 '18 at 10:07
  • A quick google search resulted in [Media Foundation .Net](http://mfnet.sourceforge.net/) and this [SO answer](https://stackoverflow.com/q/28261101/1305969). – zx485 Jan 25 '18 at 10:10
  • 2
    Before jumping on the Media Foundation bandwagon one should view [What is the status of Microsoft Media Foundation?](https://stackoverflow.com/a/33555619/585968). Sometimes _"ancient"_ technologies are better than upstarts. Not supporting Windows 7 is quite unforgivable –  Jan 25 '18 at 10:16

1 Answers1

2

Playback from decrypted in memory content is a much harder task than decrypting or encrypting media files at once as your code snippets suggest. Media files are often huge. Even if your file is small the APIs target scenario that files might be large and file data is streamed without loading it at once. Hence decryption and playback have to provide ways to access randomly located pieces of the file on demand.

You are typically supposed to develop one of the respective API primitives to be inserted into pipeline and handle decryption delivering pieces of original file on request.

With DirectShow API you typically want to develop and supply your custom DirectShow source filter. There was an Async Filter Sample which might be a good starting point where decryption can be added fairly easily, but the sample is native code C++ project in first place and building it might be somewhat confusing. I don't think you can develop a filter in C# even though you might want to try DirectShow.NET's \Samples\Misc\GSSF:

The Generic Sample Source Filter - A way to implement a source filter in c#

This thread addresses the question asked here and remains up to date: Playing encrypted movie from memory:

I have an encrypted movie in resources, size could be around or more than 1 GB, so I can not decrypt it all at once. So I am thinking of playing the movie by decrypting it chunk by chunk, whatever part is needed can be decrypted before it is played and can be supplied to player.

Playback with on-the-fly decryption in Media Foundation is somewhat easier to implement. In Media Foundation you might need to supply customized IMFByteStream implementation which handles read requests and applies the decryption in question. Or even IStream implementation applied to with MFCreateMFByteStreamOnStream function might do the trick. I think it's pretty much doable in C#. Another thing is that you will have to use Media Foundation as an API for playback and the task is subject to its own set of challenges.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • thanks a lot, i want to ask question. can i protect my video file by using Media Foundation to encrypt and decrypt video and play it in memory? – topcool Jan 25 '18 at 22:50
  • Yes, the way, for example, that I mentioned in the last paragraph of the answer. – Roman R. Jan 26 '18 at 07:04
  • Is there any sample code? I can not understand the media foundation docs because they are so obscure. I added Media foundation dll to project. Now how can i play encrypted video in memory? please help. thanks – topcool Jan 26 '18 at 10:43
  • No sample code, a rule of thumb with Media Foundation is that you are on your own with the API. – Roman R. Jan 26 '18 at 10:47