天天看點

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();

}