vc++實現隨身碟介質加密解密保障儲存安全

來源:互聯網
上載者:User

核心代碼如下:

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

調用代碼如下

BOOL CMSSDlg::OnInitDialog()<br />{<br />CDialog::OnInitDialog();</p><p>// Add "About..." menu item to system menu.</p><p>// IDM_ABOUTBOX must be in the system command range.<br />ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);<br />ASSERT(IDM_ABOUTBOX < 0xF000);</p><p>CMenu* pSysMenu = GetSystemMenu(FALSE);<br />if (pSysMenu != NULL)<br />{<br />CString strAboutMenu;<br />strAboutMenu.LoadString(IDS_ABOUTBOX);<br />if (!strAboutMenu.IsEmpty())<br />{<br />pSysMenu->AppendMenu(MF_SEPARATOR);<br />pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);<br />}<br />}</p><p>// Set the icon for this dialog. The framework does this automatically<br />// when the application's main window is not a dialog<br />SetIcon(m_hIcon, TRUE);// Set big icon<br />SetIcon(m_hIcon, FALSE);// Set small icon</p><p>// TODO: Add extra initialization here<br />HKEY hkey;<br /> char sz[256];<br /> DWORD dwtype,sl = 256;</p><p>for(int i=1;i<8;i++)<br />{<br />if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/<br />NULL, KEY_ALL_ACCESS, &hkey)==ERROR_SUCCESS)<br />{<br />CString id;<br />id.Format("%d",i);<br />if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS)<br />{<br />CString str=(CString)sz;<br />m_select.AddString(sz);<br />}<br />}<br />}<br />RegCloseKey(hkey);</p><p>m_select.SetCurSel(0);</p><p>if(m_select.GetCount()==0)<br />{<br />AfxMessageBox("沒有發現行動裝置!");<br />}<br />return TRUE; // return TRUE unless you set the focus to a control<br />}</p><p>void CMSSDlg::OnSysCommand(UINT nID, LPARAM lParam)<br />{<br />if ((nID & 0xFFF0) == IDM_ABOUTBOX)<br />{<br />CAboutDlg dlgAbout;<br />dlgAbout.DoModal();<br />}<br />else<br />{<br />CDialog::OnSysCommand(nID, lParam);<br />}<br />}</p><p>// If you add a minimize button to your dialog, you will need the code below<br />// to draw the icon. For MFC applications using the document/view model,<br />// this is automatically done for you by the framework.</p><p>void CMSSDlg::OnPaint()<br />{<br />if (IsIconic())<br />{<br />CPaintDC dc(this); // device context for painting</p><p>SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);</p><p>// Center icon in client rectangle<br />int cxIcon = GetSystemMetrics(SM_CXICON);<br />int cyIcon = GetSystemMetrics(SM_CYICON);<br />CRect rect;<br />GetClientRect(&rect);<br />int x = (rect.Width() - cxIcon + 1) / 2;<br />int y = (rect.Height() - cyIcon + 1) / 2;</p><p>// Draw the icon<br />dc.DrawIcon(x, y, m_hIcon);<br />}<br />else<br />{<br />CDialog::OnPaint();<br />}<br />}</p><p>// The system calls this to obtain the cursor to display while the user drags<br />// the minimized window.<br />HCURSOR CMSSDlg::OnQueryDragIcon()<br />{<br />return (HCURSOR) m_hIcon;<br />}</p><p>void CMSSDlg::OnExit()<br />{<br />// TODO: Add your control notification handler code here<br />CMSSDlg::OnCancel();<br />}</p><p>LRESULT CMSSDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)<br />{<br />if (WM_DEVICECHANGE == message)<br />{<br />m_select.ResetContent();</p><p>HKEY hkey;<br /> char sz[256];<br /> DWORD dwtype,sl = 256;</p><p>for(int i=1;i<8;i++)<br />{<br />if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/<br />NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS)<br />{<br />CString id;<br />id.Format("%d",i);<br />if(RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS)<br />{<br />CString str=(CString)sz;<br />m_select.AddString(sz);<br />}<br />}<br />}</p><p>m_select.SetCurSel(0);</p><p>RegCloseKey(hkey);<br />}</p><p>return CDialog::WindowProc(message, wParam, lParam);<br />}</p><p>void CMSSDlg::OnAddPassword()<br />{<br />// TODO: Add your control notification handler code here<br />unsigned char MBRBuf[512];</p><p>CString name;<br />CString driver;</p><p>int id;// 選擇行動裝置的編號</p><p>if(m_select.GetCurSel()==CB_ERR)<br />{<br />AfxMessageBox("請選擇要加密的裝置!");<br />return;<br />}</p><p>id=m_select.GetCurSel();<br />m_select.GetLBText(id,name);</p><p>// 確定選擇的磁碟<br />driver=GetDiskNumber(name);</p><p>// 讀磁碟的MBR區<br />if(ReadDisk(driver,MBRBuf,0)==0)<br />return;</p><p>/* RC4加密,KEY是密鑰,此處Key[]="MobileStorageSecurity",後期可以<br /> 使用者輸入的密碼作為密鑰 */<br />RC4_KEY rc4_key;<br />build_rc4_key(Key,strlen((char*)Key),&rc4_key);<br />rc4_handler(MBRBuf,strlen((char*)MBRBuf),&rc4_key);</p><p>// 將加密後的MBR寫入磁碟<br />if(WriteDisk(driver,MBRBuf,0)==0)<br />return;</p><p>AfxMessageBox("加密成功!");</p><p>// 彈出USB存放裝置<br />PopupUSBDevice();<br />}</p><p>void CMSSDlg::OnRemovePassword()<br />{<br />// TODO: Add your control notification handler code here<br />unsigned char MBRBuf[512];</p><p>CString name;<br />CString driver;</p><p>int id;// 選擇行動裝置的編號</p><p>if(m_select.GetCurSel()==CB_ERR)<br />{<br />AfxMessageBox("請選擇要加密的裝置!");<br />return;<br />}</p><p>id=m_select.GetCurSel();<br />m_select.GetLBText(id,name);</p><p>// 確定選擇的磁碟<br />driver=GetDiskNumber(name);</p><p>// 讀磁碟的MBR區<br />if(ReadDisk(driver,MBRBuf,0)==0)<br />return;</p><p>/* RC4加密,KEY是密鑰,此處Key[]="MobileStorageSecurity",後期可以<br /> 使用者輸入的密碼作為密鑰 */<br />RC4_KEY rc4_key;<br />build_rc4_key(Key,strlen((char*)Key),&rc4_key);<br />rc4_handler(MBRBuf,strlen((char*)MBRBuf),&rc4_key);</p><p>// 將解密後的MBR寫入磁碟<br />if(WriteDisk(driver,MBRBuf,0)==0)<br />return;</p><p>AfxMessageBox("解密成功!");</p><p>// 彈出USB存放裝置<br />PopupUSBDevice();<br />}</p><p>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.