在 C# 中响应与文件的交互?
我想编写一个程序来加密整个文件夹及其子文件夹,这样做没有问题,但我想通过让用户双击它并打开它来使整个加密过程变得相当透明未加密,例如,如果它是图片或 Word 文档,并且会在各自的应用程序中打开。
我的正在运行的程序如何收到有关目标文件打开的通知,阻止文件打开,执行它需要执行的操作(解密),然后运行生成的解密文件。
如何在 C# 中查看文件并执行此操作?我可以监视其他交互,例如用户复制监视的文件(因为它不会位于监视的文件夹中,因此应该将其解密,即将其拖到 USB 设备),或者删除监视的文件(比如说,如果我想在删除之前粉碎文件)?
PS FileSystemWatcher 不太满足我的需求。 编辑:我的意思是,FileSystemWatcher 会告诉我文件何时被打开、删除以及所有这些事件,但它不会让我快速介入、解密文件并交给它返回到通常打开该文件的进程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以重命名文件,添加您自己的扩展名,例如 thepicture.jpg.encrypted。将您的程序设置为此扩展的默认程序并处理打开它们
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
这在 C# 中是不可能的。最低限度需要您在 NtCreateFile、NtOpenFile 等上使用用户模式挂钩。在 C# 中无法实现这一点。由于内核模式代码可能会尝试访问您的文件,这甚至无法正常工作。正确的方法是编写一个 I/O 微过滤器(当然是用 C 语言)。
编辑:如果您真的很绝望,请尝试 EasyHook - 它允许您从 C# 挂钩函数。不过我还没有尝试过,挂钩像 NtCreateFile 这样的重要函数似乎确实有风险。另外,您还需要相当多的 Native API 知识。
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.
您使用的是 Windows 吗?如果是这样,为什么不使用内置的 BitLocker?
请参阅此链接:
BitLocker 驱动器加密
如果您正在考虑竞争性产品应用程序到 BitLocker,添加注释,因为我也可以为您指出该方向。
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.
不要尝试重新发明轮子,而是使用 NTFS 文件加密。您可以加密单个文件或整个文件夹或驱动器。另外,它对用户完全透明,并且完全按照您的要求执行(例如,复制到 UBS 驱动器时自动解密等)。只需使用 System.IO.File.Encrypt(string) - 没有比这更简单的了。
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.您不能从用户模式执行此操作。
不幸的是,做到这一点的唯一方法是编写一个微过滤器驱动程序。 Minifilter 驱动程序允许您拦截对文件的 IO 请求,然后您可以动态加密/解密您关心的文件。
听起来很简单,但加密微过滤器驱动程序非常非常难以正确使用。您将不得不最终隐藏文件对象,这是一个真正的挑战。请访问 www.osr.com,他们有大量关于做你想做的事情的信息。
如果您选择走这条路,我建议您获取 VMWare Workstation 的副本并下载 VirtualKD。它可以让您以接近火线的速度调试虚拟机。我会从 x64 Win7 开始,首先让远程共享工作。
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.