⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mssdlg.cpp

📁 U盘数据加密程序 用C语言编写的 非常经典的哟
💻 CPP
字号:
///////////////////////////////////////////////////////////////////
/*
 * 项目名称:移动存储安全(演示原型)
			 Mobile Storage Security
 *
 * 作    者:南京邮电大学计算机学院 吴登荣
 *
 * 摘    要:对U盘数据加密,保护信息隐蔽,防止隐私泄露。
 *
 * 版    本:Alpha
 *
 * 完成日期:2007年2月
 *
 * E - mail:Jeffrey.nupt@gmail.com
*/
///////////////////////////////////////////////////////////////////

// MSSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MSS.h"
#include "MSSDlg.h"
#include "rc4.h"
#include "Function.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

unsigned char Key[]="MobileStorageSecurity";

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

//int id[7]={0};

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMSSDlg dialog

CMSSDlg::CMSSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMSSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMSSDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMSSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMSSDlg)
	DDX_Control(pDX, IDC_SELECT, m_select);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMSSDlg, CDialog)
	//{{AFX_MSG_MAP(CMSSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_ADD_PASSWORD, OnAddPassword)
	ON_BN_CLICKED(IDC_REMOVE_PASSWORD, OnRemovePassword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMSSDlg message handlers

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -