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

核心代码如下:

#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 dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here 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); } } } 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 painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int 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 icon dc.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 here CMSSDlg::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 here unsigned 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 here unsigned 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(); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值