[ 用户登陆 ] [ 免费注册 

Class >> 学习资料 > 程序技术 > VC >
vc++实现U盘介质加密解密保障存储安全
文 . 2011/10/9 12:40:25
核心程序:
#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();  
}  


最近后的内容
VS2003 中配置Microsoft PlatformSDK2003
最近前的内容
error C2440: “static_cast”: 无法从“UINT (__thiscall CCoolControlBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)”
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
warning C4996: 'sprintf': This function or variable may be unsafe
BOOL和bool的区别
wsprintf用法
C++命名书写规范/MFC、句柄、控件及结构的命名规范
C语言类型范围
error C2440: "static_cast": 无法从"void (__thiscall CChat::* )(WPARAM,LPARAM)"转换为"LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)"
最受欢迎
C语言类型范围
error C2440: "static_cast": 无法从"void (__thiscall CChat::* )(WPARAM,LPARAM)"转换为"LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)"
C++命名书写规范/MFC、句柄、控件及结构的命名规范
VS2003 中配置Microsoft PlatformSDK2003
error C2440: “static_cast”: 无法从“UINT (__thiscall CCoolControlBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)”
warning C4996: 'sprintf': This function or variable may be unsafe
BOOL和bool的区别
vc++实现U盘介质加密解密保障存储安全
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
wsprintf用法
 ©2005 - 2008 粤ICP备12037054号-1 用户登陆  |  互联说明  |  联系我们  |  网站地图