tags:

views:

82

answers:

5

I want to write a program that will encrypt an entire folder and it's sub-folders, I have no problem doing this but I would like to make the entire encryption process rather transparent by letting a user double click it and have it open as if it weren't encrypted, say if it were a picture or a word document and it'd open in it's respective application.

How can a running program of mine become notified about the opening of a target file, stop the file from opening, do what it needs to do (decrypt), followed by running the resulting decrypted file.

How can I watch a file and do this in C#? Can I watch for other interactions like the user copying a watched file (since it won't be in a watched folder, it should be decrypted i.e. it's dragged to a USB device), or for deleting a watched file (say if I want to shred a file before deletion)?

P.S. The FileSystemWatcher doesn't quite meet my needs. EDIT: What I mean is that FileSystemWatcher will tell me when a file is being opened, deleted and all those events, but it won't let me step in real quick, decrypt the file, and hand it back to the process that normally opens that file.

A: 

Are you using Windows? If so, why not use the built-in BitLocker?

See this link:

BitLocker drive encryption

If you are thinking about a competitive application to BitLocker, add a comment, as I can point you in that direction as well.

GalacticJello
BitLocker works on drives, volumes, has hardware and software (OS) requirements; it is not an appropriate tool for encrypting folders/files.
BillW
Either way, I want to use my encryption and write my own features around the security suite. I'm not writing this because I believe it hasn't been done before.
Corey Ogburn
A: 

Instead of trying to reinvent the wheel, use NTFS file encryption. You can encrypt single files or entire folders or drives. Plus it's completely transparent to the user and does exactly what you asks (e.g. automatically decrypt when copying to a UBS drive, etc). Just use System.IO.File.Encrypt(string) - there couldn't be anything easier.

Allon Guralnek
That won't allow me to use my own encryption. I've been working for 3 years on this encryption and I'm working on a security suite for it. I'm also working on publishing a paper for peer review.
Corey Ogburn
@Corey Ogburn You should mention in your main post that your goal is to do transparent file encryption with your own algorithm instead of the system default.
Scott Chamberlain
+1  A: 

It's impossible in C#. the bare minimum would need you to use user-mode hooks on NtCreateFile, NtOpenFile, etc. You can't achieve that in C#. That wouldn't even work properly due to kernel-mode code which may try to access your files. The proper way of doing this would be to write a I/O minifilter (in C of course).

EDIT: If you're really desperate, try EasyHook - it allows you to hook functions from C#. I haven't tried it though, and it does seem risky hooking vital functions like NtCreateFile. Plus you need a fair bit of Native API knowledge.

wj32
Or he could even write a driver to expose a virtual encrypted drive, like TrueCrypt does (http://www.truecrypt.org).
Allon Guralnek
+2  A: 

You can rename files, add them your own extension, like thepicture.jpg.encrypted. Set your program as a default program for this extension and handle opening them

valya
This is probably what I'm going to have to do.
Corey Ogburn
A: 

You can't do this from usermode.

Unfortunately the only way to do this is to write a minifilter driver. Minifilter drivers allow you to intercept IO requests to files, you can then encrypt/decrypt the files you care about on the fly.

It sounds simple, but encryption minifilter drivers are very, very, difficult to get right. You will have to end up shadowing file objects which is a real challenge. Check with www.osr.com, they have a ton of information on doing exactly what you want to do.

If you choose to go this route I would recommend getting a copy of VMWare Workstation and download VirtualKD. It will let you debug at near fire-wire speeds into a VM. I would start with x64 Win7 and get remote shares working first.

Karl Strings