hi,
i want to lock a folder, contains an decrypted video file. Along with this i want to play this file using windows application. But no one can copy or cut this file while playing. Is there any method to develop this? Here actual scenario is need to play a video from a encrypted video file. what i have done is decrypt this video and saved to a folder 'a' and play that file in my windows application. but while playing any one can copy the decrypted file from 'a'. Is there any method to prevent this.?
please help me
thanks in advance.

Recommended Answers

All 3 Replies

Hi,

please try to use Directory.SetAccessControl (folderName, new DirectoryServices()) ... i hope it will solve the issue

But Directory Services is not found. is there any assembly for this?

I got curious about this and looked into it a bit. As mano said, there is a class under System.Security.DirectorySecurity that handles control to a folder, and allows you to restrict access to certain users with it. The problem with this is, the user (assuming with local admin priveleges) can simply override the program by changing the settings back.

I've devised a somewhat malicious way to circumvent this by constantly looking for and closing the properties form for a folder. It doesn't really play by the rules but it should work for the most part. (I bet there's a different form to change security settings somewhere, so you may need to add these to the kill form thread).

To make this work make a folder on your desktop called 'test' and put some secret data in it - a file called test.txt with some text in it. Run the program to lock it, hit enter to unlock it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Threading;
namespace ConsoleApplication2
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

        volatile static bool abort_token = false;
        static void Main(string[] args)
        {
            //create and start a thread that looks for the test Properties dialog every 250ms and closes it
            Thread killProps = new Thread(new ThreadStart(killPropertiesForm));
            killProps.IsBackground = true;
            killProps.Start();
            
            
            //Get the folder on the current user's desktop called test
            string myFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test\";
            System.IO.FileStream sr = new System.IO.FileStream(myFolderPath + "test.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
            Console.WriteLine("Locking folder" + myFolderPath);

            //create a directory security object constructed with 'test' folder's security attributes
            DirectorySecurity s = new DirectorySecurity(myFolderPath, AccessControlSections.Owner);
            //Create an access rule for the current user, deny all control
            FileSystemAccessRule fsar = new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny);
            //add the access rule to the directory security object
            s.AddAccessRule(fsar);
            //add the attributes to the folder
            System.IO.Directory.SetAccessControl(myFolderPath, s);
            //Read data from the locked folder, since the stream was opened before the permissions were set, we're ok
            byte[] data = new byte[sr.Length];
            sr.Read(data, 0, (int)sr.Length);
            Console.WriteLine("Reading data from locked folder: " + System.Text.Encoding.Default.GetString(data));
            data = System.Text.Encoding.Default.GetBytes(Environment.NewLine + "Test done!");
            sr.Write(data, 0, data.Length - 1); 
            Console.ReadLine();
            Console.WriteLine("Unlocking file");
            //remove the access rule we just set
            s.RemoveAccessRule(fsar);
            //apply it to the folder
            System.IO.Directory.SetAccessControl(myFolderPath, s);
            sr.Close();
            //flag the form closing thread here
            abort_token = true;
            Console.ReadLine();
        }
        private static void killPropertiesForm()
        {
            while (abort_token == false)
            {
                closeWindow("#32770", "test Properties");
                Thread.Sleep(250);
            }
        }
        private static void closeWindow(string sName, string sForm)
        {
            // retrieve the handler of the window  
            int iHandle = FindWindow(sName, sForm);
            if (iHandle > 0)
            {
                // close the window using API        
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }
        }
    }
}

The problem with using this is that even YOUR PROGRAM can't access the folder anymore (unless it's being run as a different user). That is why I needed to open the streamreader before I set the permissions.

A much better method would be to load in the file as a stream and decrypt it on the fly :p. But this might not be possible if your API for the video player doesn't support memory streams. Another option would be to save the chunks to a secret hidden location, somewhere deep inside a temp folder. This location, however, could easily be tracked by certain debugging tools.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.