Click here to Skip to main content
15,920,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a security application in c#. For that, I am planning to encrypt the entire USB storage device.Now I can check if the USB is Pugged in or not and my next step is to encrypt the detected USB storage How can i do that ?

C#
using System;
using System.IO;
using System.Management;
using System.ServiceProcess;
using System.Timers;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Text;

namespace WinServiceSample
{
    public partial class ScheduledService : ServiceBase
    {
       
        



            protected override void OnStart(string [] strgs)
            {



                //add this line to text file during start of service
                TraceService("start service");

                //handle Elapsed event
                timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

                //This statement is used to set interval to 1 minute (= 60,000 milliseconds)

                timer.Interval = 10000;

                //enabling the timer
                timer.Enabled = true;
                ManagementEventWatcher mwe_creation;
                WqlEventQuery q_creation = new WqlEventQuery();
                q_creation.EventClassName = "__InstanceCreationEvent";
                q_creation.WithinInterval = new TimeSpan(0, 0, 2);    //How often do you want to check it? 2Sec.
                q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
                mwe_creation = new ManagementEventWatcher(q_creation);
                mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
                mwe_creation.Start();

                ManagementEventWatcher mwe_deletion;
                WqlEventQuery q_deletion = new WqlEventQuery();
                q_deletion.EventClassName = "__InstanceDeletionEvent";
                q_deletion.WithinInterval = new TimeSpan(0, 0, 2);    //How often do you want to check it? 2Sec.
                q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'  ";
                mwe_deletion = new ManagementEventWatcher(q_deletion);
                mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
                mwe_deletion.Start();

            }


            internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
            {
                TraceService("USB Added");
            }

            internal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
            {
                TraceService("USB Removed");
            }

 
        //This method is used to stop the service
        protected override void OnStop()
        {
        timer.Enabled = false;
        TraceService("stopping service");
        

        }






        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
        TraceService("Another entry at "+DateTime.Now);
        }

        private void TraceService(string content)
        {
            //set up a filestream
            FileStream fs = new FileStream(@"d:\ScheduledService.txt",FileMode.OpenOrCreate, FileAccess.Write);

            //set up a streamwriter for adding text
            StreamWriter sw = new StreamWriter(fs);

            //find the end of the underlying filestream
            sw.BaseStream.Seek(0, SeekOrigin.End);

            //add the text
            sw.WriteLine(content);
            //add the text to the underlying filestream

            sw.Flush();
            //close the writer
            sw.Close();
        }
        private void Filewriter(string message)
        {
            //set up a filestream
            FileStream fs = new FileStream(@"d:\keystrokes.txt", FileMode.OpenOrCreate, FileAccess.Write);

            //set up a streamwriter for adding text
            StreamWriter sw = new StreamWriter(fs);

            //find the end of the underlying filestream
            sw.BaseStream.Seek(0, SeekOrigin.End);

            //add the text
            sw.WriteLine(message);
            //add the text to the underlying filestream

            sw.Flush();
            //close the writer
            sw.Close();
        }
   }
}
Posted
Updated 7-Mar-16 3:30am
v2
Comments
Richard MacCutchan 7-Mar-16 10:14am    
You would need to iterate through all the files on the device encrypting them and rewriting them back to the device. This would be potentially quite difficult if the device were completely full. An alternative would be to have a filter driver which encrypted data at the device level.
Philippe Mori 9-Mar-16 12:37pm    
Use exisiting solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900