MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

c#设计一个文件夹加密软件

2025-01-12 15:24 huorong 精选文章 5 ℃ 0 评论

最近想要把电脑上的一些文件夹隐藏掉,实现看不见、打不开就行了,并且速度还要快,看了一下网上现有的软件,实现起来基本上有如下几种:
1、真正的对文件夹里的所有内容进行加密,时间上花费太大,使用的时候还需要解密了,这不是咱需要的
2、对文件夹改图标或畸形文件夹来实现隐藏
3、通过文件acl来实现禁止对文件夹的访问
4、还有一些不知道原理的,基本上都还收费。
5、使用Bitlocker,但这个只能对分区,不能对文件夹,并且初始化时非常慢
综合以上,决定自己手写一个简单的,不需要对文件加密,只需要对一般水平的人隐藏,并且就算显示了隐藏,也禁止被访问到就可以了,那就使用文件acl来实现吧,acl本质上不能加密码访问,软件加密码只是为了防止别人知道你隐藏了哪些文件夹而已,所以就没有使用密码。

界面

软件界面很简单,如下图,选择文件夹、移除文件夹、加密、解密。

引用第三方库miniExcel来加载表格中的内容和保存用户选择的文件夹,如果正常使用的话应该用sqlite数据库比较好一些。

软件主要通过设置文件acl的用户访问权限来实现对访问的阻止,并通过api来禁止系统对隐藏文件夹的索引,不过并不能阻止everything这类搜索软件的搜索,另外为了速度,并没有把权限应用于文件夹内的所有文件,所以你要知道文件的具体名称还是可以访问的。

核心

API引用,用于禁止文件夹被索引

 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 [return: MarshalAs(UnmanagedType.Bool)]
 static extern bool SetFileAttributes([MarshalAs(UnmanagedType.LPTStr)] string lpFileName, FileAttributes dwFileAttributes);

 string ExcelPath = "folderpath.xlsx";
 string Title = "文件夹加解密提醒";

文件夹加密功能,主要是拒绝当前用户对文件夹进行所有的控制,使用后文件夹将无法被访问。

public void Lock(string FolderPath)
{
    string folderPath = FolderPath;
    FileAttributes attributes = File.GetAttributes(folderPath);

    // 添加属性以阻止文件夹被索引
    attributes |= FileAttributes.NotContentIndexed;
    SetFileAttributes(folderPath, attributes);

    File.SetAttributes(folderPath, File.GetAttributes(folderPath) | FileAttributes.Hidden|FileAttributes.System);
    DirectorySecurity directorySecurity = Directory.GetAccessControl(folderPath);
    FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny);
    directorySecurity.AddAccessRule(fileSystemAccessRule);
    Directory.SetAccessControl(folderPath, directorySecurity);
   
}

点击加密后,显示隐藏的系统文件后,还是可以看到文件平的,只是无法访问,如下

文件夹解密功能, 其实就是移除禁止访问的这条ACL就可以了

public void UnLock(string FolderPath)
{
    string folderPath = FolderPath;
    DirectorySecurity directorySecurity = Directory.GetAccessControl(folderPath);
    FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny);
    directorySecurity.RemoveAccessRule(fileSystemAccessRule);
    Directory.SetAccessControl(folderPath, directorySecurity);
    File.SetAttributes(folderPath, File.GetAttributes(folderPath) & ~FileAttributes.Hidden&~FileAttributes.System);
}

核心功能就完成了,接下来就是一些完善修补工作了

加密实现

 //对文件夹加密
 if (DG_show.Rows.Count < 1)
 {
     MessageBox.Show("没有需要加密的文件夹!", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
     return;
 }

 string path = DG_show.CurrentRow.Cells[0].Value.ToString();
 try
 {
     Lock(path);
     MessageBox.Show("文件夹已加密!", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
     DG_show.CurrentRow.Cells[1].Value = "已加锁";
 }
 catch (Exception ex)
 {
     MessageBox.Show("加密文件夹时出现错误,信息如下:"+ex.Message.ToString(), Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
 }

解密实现

if (DG_show.Rows.Count < 1)
{
    MessageBox.Show("没有需要解密的文件夹!", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
    return;
}
string path = DG_show.CurrentRow.Cells[0].Value.ToString();
try
{
    UnLock(path);
    MessageBox.Show("文件夹已解密!", Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
    DG_show.CurrentRow.Cells[1].Value = "已解锁";
}
catch (Exception ex)
{
    MessageBox.Show("解密文件夹时出现错误,信息如下:" + ex.Message.ToString(), Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

选择文件夹功能

string folderPath = "";
FolderBrowserDialog dialog = new FolderBrowserDialog();
var dr = dialog.ShowDialog();
if (dr == DialogResult.OK)
{
    folderPath = dialog.SelectedPath;
}
else
{
    return;
}
DG_show.Rows.Add(folderPath, "已解锁");

窗体事件

//load时加载保存的已选择目录
DG_show.AutoGenerateColumns = false;
var isFile = File.Exists(ExcelPath);
if (isFile)
{
    var DTable = MiniExcel.QueryAsDataTable(ExcelPath, true);
    if (DTable.Rows.Count > 0)
    {
        foreach (DataRow dr in DTable.Rows)
        {
            DG_show.Rows.Add(dr[0], dr[1]);
        }
    }
}
//窗体关闭时保存用户选择的文件夹目录,先删除后保存
var isFile = File.Exists(ExcelPath);
if (isFile)
{
    File.Delete(ExcelPath);
}


DataTable dt = new DataTable();
dt.Columns.Add("文件夹");
dt.Columns.Add("状态");
for (int i = 0; i < DG_show.Rows.Count; i++)
{
    dt.Rows.Add(DG_show.Rows[i].Cells[0].Value.ToString(), DG_show.Rows[i].Cells[1].Value.ToString());
}
MiniExcel.SaveAs(ExcelPath, dt);

最终的效果如下,感觉还是挺好用的

如果去掉隐藏受保护的系统文件对勾,那么加锁的文件也是会被显示出来的,只是无法访问

如果不在意时间上的花费,那么可以把ACL规则应用到文件夹下的所有文件,这样子就无法通过具体的文件名称来进行访问了。

Tags:show columns

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言