核心代码如下:

#include "StdAfx.h"
#include "Function.h"CString GetDiskNumber(CString name)
{HKEY hkey;char sz[256];DWORD dwtype,sl = 256;int number=0;// 确定选择的磁盘for(int i=1;i<8;i++){if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS){ CString id;id.Format("%d",i);if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS){CString str=(CString)sz;if(str.Compare(name)==0){number=i;break;}}}}CString driver=".//PHYSICALDRIVE";CString num;num.Format("%d",number);driver+=num;return driver;
}int ReadDisk(CString driver,unsigned char *Buf,long addr)
{HANDLE hDevice;BOOL bResult;DWORD bytesread;hDevice=CreateFile(driver,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);if(hDevice==INVALID_HANDLE_VALUE){   AfxMessageBox("Error!");return 0;}if(addr!=0){SetFilePointer(hDevice,512*addr,NULL,NULL);}bResult=ReadFile(hDevice,Buf,512,&bytesread,NULL);if((bResult==FALSE)||(bytesread<512))   {   AfxMessageBox("Error!");return 0;}CloseHandle(hDevice);return 1;
}int WriteDisk(CString driver,unsigned char *Buf,long addr)
{HANDLE hDevice;BOOL bResult;DWORD bytesread;hDevice=CreateFile(driver,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);if(hDevice==INVALID_HANDLE_VALUE){   AfxMessageBox("Error!");return 0;}if(addr!=0){SetFilePointer(hDevice,512*addr,NULL,NULL);}bResult=WriteFile(hDevice,Buf,512,&bytesread,NULL);if((bResult==FALSE)||(bytesread<512))   {   AfxMessageBox("Error!");return 0;}CloseHandle(hDevice);return 1;
}void PopupUSBDevice()
{char strSystemDirectory[256];GetSystemDirectory( strSystemDirectory, 256 );CString strTemp = strSystemDirectory;strTemp += "//rundll32.exe shell32.dll,Control_RunDLL hotplug.dll";WinExec( strTemp, SW_SHOW );
}

调用代码如下

BOOL CMSSDlg::OnInitDialog()
{CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog.  The framework does this automatically//  when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);            // Set big iconSetIcon(m_hIcon, FALSE);     // Set small icon// TODO: Add extra initialization hereHKEY hkey;char sz[256];DWORD dwtype,sl = 256;for(int i=1;i<8;i++){if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/NULL, KEY_ALL_ACCESS, &hkey)==ERROR_SUCCESS){CString id;id.Format("%d",i);if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS){CString str=(CString)sz;m_select.AddString(sz);}}}RegCloseKey(hkey);m_select.SetCurSel(0);if(m_select.GetCount()==0){AfxMessageBox("没有发现移动设备!");}return TRUE;  // return TRUE  unless you set the focus to a control
}void CMSSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}
}// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.void CMSSDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}
}// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMSSDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CMSSDlg::OnExit()
{// TODO: Add your control notification handler code hereCMSSDlg::OnCancel();
}LRESULT CMSSDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{if (WM_DEVICECHANGE == message){m_select.ResetContent();HKEY hkey;char sz[256];DWORD dwtype,sl = 256;for(int i=1;i<8;i++){if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS){CString id;id.Format("%d",i);if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS){CString str=(CString)sz;m_select.AddString(sz);}}}m_select.SetCurSel(0);RegCloseKey(hkey);}return CDialog::WindowProc(message, wParam, lParam);
}void CMSSDlg::OnAddPassword()
{// TODO: Add your control notification handler code hereunsigned char MBRBuf[512];CString name;CString driver;int id;              // 选择移动设备的编号if(m_select.GetCurSel()==CB_ERR){AfxMessageBox("请选择要加密的设备!");return;}id=m_select.GetCurSel();m_select.GetLBText(id,name);// 确定选择的磁盘driver=GetDiskNumber(name);// 读磁盘的MBR区if(ReadDisk(driver,MBRBuf,0)==0)return;/* RC4加密,KEY是密钥,此处Key[]="MobileStorageSecurity",后期可以用户输入的密码作为密钥 */RC4_KEY rc4_key;build_rc4_key(Key,strlen((char*)Key),&rc4_key);rc4_handler(MBRBuf,strlen((char*)MBRBuf),&rc4_key);// 将加密后的MBR写入磁盘if(WriteDisk(driver,MBRBuf,0)==0)return;AfxMessageBox("加密成功!");// 弹出USB存储设备PopupUSBDevice();
}void CMSSDlg::OnRemovePassword()
{// TODO: Add your control notification handler code hereunsigned char MBRBuf[512];CString name;CString driver;int id;              // 选择移动设备的编号if(m_select.GetCurSel()==CB_ERR){AfxMessageBox("请选择要加密的设备!");return;}id=m_select.GetCurSel();m_select.GetLBText(id,name);// 确定选择的磁盘driver=GetDiskNumber(name);// 读磁盘的MBR区if(ReadDisk(driver,MBRBuf,0)==0)return;/* RC4加密,KEY是密钥,此处Key[]="MobileStorageSecurity",后期可以用户输入的密码作为密钥 */RC4_KEY rc4_key;build_rc4_key(Key,strlen((char*)Key),&rc4_key);rc4_handler(MBRBuf,strlen((char*)MBRBuf),&rc4_key);// 将解密后的MBR写入磁盘if(WriteDisk(driver,MBRBuf,0)==0)return;AfxMessageBox("解密成功!");// 弹出USB存储设备PopupUSBDevice();
}

原文链接: http://blog.csdn.net/yincheng01/article/details/4668226

转载于:https://my.oschina.net/junwong/blog/47903

vc++实现U盘介质加密解密保障存储安全相关推荐

  1. (留念)第一个视频教程 U盘超级加密软件的原理详解与解密

    --------------------------------------------------------------------------- 爱国者安全网 www.3800hk.com 中国 ...

  2. VC实现数据的加密和解密(MD5加密/DES/RSA加密解密)

    VC实现数据的加密和解密 由于生产实习的时间有限,加上自己这段时间致力于考研,因此,仅仅是实现了通过MD5/DES/RSA的简单的字符串的加密解密,希望有兴趣的兄弟姐妹能够完善它. 主要的程序如下: ...

  3. VC++2010基于windows Sdk for windows7 开发CrytoAPI应用--加密解密文件

    windows Sdk for windows7 开发CrytoAPI是微软推出的安全应用调用函数,用很小的代码就可以实现复杂的安全信息加密.下面演示文件加密解密,详情请见代码. #include & ...

  4. 云服务器 ECS > 块存储 > 加密云盘 > 加密概述

    加密概述 更新时间:2020-10-23 09:50:32 编辑我的收藏 本页目录 功能介绍 密钥 计费 使用限制 数据加密适用于数据安全或法规合规等场景,帮助您加密保护存储在阿里云ECS上的数据.无 ...

  5. java md5加密解密_技术09期:数据安全之加密与实现

    前言:大数据时代,每个人的生活中都不存在所谓的绝对"秘密",通过网络上的数据信息可以分析出一个人生活的各种痕迹.因此,保障大数据信息安全至关重要. 本文主要介绍了散列算法.对称加密 ...

  6. 【C#文件锁】C#加密解密文件小工具

    前面一篇博文中,分享了一个文件夹加密小工具,该工具是操作文件夹名称的方法实现文件夹的一般加密,文件夹中的文件(视频.图片等)都原封不动的保存在那里. DebugLZQ在网上搜索相关文件加密的程序,发现 ...

  7. pyDes 实现 Python 版的 DES 对称加密/解密--转

    https://my.oschina.net/leejun2005/blog/586451 手头有个 Java 版的 DES 加密/解密程序,最近想着将其 Python 重构下,方便后续脚本解析,捣鼓 ...

  8. 关于文件透明加密解密

    1.windows文件系统概述 硬盘文档加密系统驱动原理一般有两种:通过HOOK一些底层的系统内核调用对文件目录的访问权限进行控制,在文件系统输入输出驱动上层直接建立一个驱动对文件目录的数据进行加密. ...

  9. 兽语狗语文字在线加密解密PHP网站源码

    介绍: 可以将文字.字母.数字.代码.标点符号等内容转换成新的文字形式,通过简单的文字以不同的排列顺序来表达不同的内容! 无需数据库 PHP源码 支持在线加密解密 有多种加密展示方式. 网盘下载地址: ...

最新文章

  1. 第二阶段第三次站立会议
  2. mac 安装淘宝镜像报错之坑
  3. 互联网趋势关键词:交流,为价值付费,资源整合
  4. 小马哥spring编程核心思想_Spring源码高级笔记之——Spring核心思想
  5. html怎么自动设为底部,让底部永远在页面最底部显示的css方法
  6. Nginx反向代理的系统优化
  7. 管理好你的愿望,人生将另一个样
  8. Kubernetes-Pod(三)
  9. DNF单机从服务器获取信息,dnf单机云服务器
  10. 分仓软件是什么?资管分仓的作用
  11. 基于开源方案构建统一的文件在线预览与office协同编辑平台的架构与实现历程
  12. 迪乐尼童鞋防骗子—常见网络骗子骗术防御要点
  13. 什么牌子的蓝牙耳机性价比高质量好?高性价比降噪蓝牙耳机推荐
  14. 园区网典型组网架构及案例实践
  15. 警告 1 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead
  16. python怎么加逗号_python – 什么是最简单的方法添加逗号到一个整数?
  17. 揭秘国漫《武动乾坤》制作幕后 动作戏竟然真人演
  18. RestartOnCrash-自动重启崩溃或挂起的应用程序
  19. 新装电脑系统,会自动安装乱七八糟的软件问题处理法
  20. grafanaprometheus 快速配置报警规则及报警接收

热门文章

  1. Python MySQL创建数据库
  2. 休闲乐系统服务器更改,《休闲乐》智能业务管理系统
  3. js隐藏手机号码中间
  4. selenium,unittest——驾照科目一网上答题自动化
  5. AE和Mocha结合做视频后期制作
  6. U3D 2020 AssetStort(资源商店)项目导入
  7. 推荐系统笔记(一):BPR Loss个性化推荐
  8. 全球顶尖CE0最怕竞争对手读到的22条商业规则!
  9. 电话销售如何避免打电话被封呢?
  10. 微信公众号开发教程(一) 验证接入