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

📄 testdpapidlg.cpp

📁 windows2000以上自带的加密api-dpapi的使用例子.
💻 CPP
字号:
// TestDPAPIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestDPAPI.h"
#include "TestDPAPIDlg.h"

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTestDPAPIDlg dialog

CTestDPAPIDlg::CTestDPAPIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDPAPIDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDPAPIDlg)
	m_strSource = _T("this");
	m_strTarget = _T("");
	m_strDesc = _T("");
	m_strPassword = _T("");
	m_bShowUI = FALSE;
	m_bMachine = FALSE;
	m_bAudit = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDPAPIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDPAPIDlg)
	DDX_Text(pDX, IDC_EDT_SOURCE, m_strSource);
	DDX_Text(pDX, IDC_EDT_TARGET, m_strTarget);
	DDX_Text(pDX, IDC_EDTDESCRIPTION, m_strDesc);
	DDX_Text(pDX, IDC_EDTPASSWORD, m_strPassword);
	DDX_Check(pDX, IDC_CHKSHOWUI, m_bShowUI);
	DDX_Check(pDX, IDC_CHKMACHINE, m_bMachine);
	DDX_Check(pDX, IDC_CHKAUDIT, m_bAudit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDPAPIDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDPAPIDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNENCRYPT, OnBtnencrypt)
	ON_BN_CLICKED(IDC_BTNDECRYPT, OnBtndecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDPAPIDlg message handlers
BOOL CTestDPAPIDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDPAPIDlg::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 CTestDPAPIDlg::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 CTestDPAPIDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

#include <io.h>
#include "FCNTL.H"
//---------------------------------------------------------------------------
const static TCHAR chHexTable[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//---------------------------------------------------------------------------
#define TOHEX(a, b)	{*b++ = chHexTable[a >> 4]; *b++ = chHexTable[a&0xf];}

void CTestDPAPIDlg::OnBtnencrypt() 
{
	// get source editbox data
	UpdateData();

	// clear target editbox content
	// m_strTarget.Empty();
	// UpdateData(FALSE);

	CString	l_strMsg;

	// don't show box
	CProtectedData pd(!m_bShowUI, m_bMachine, m_bAudit);

	if(m_bShowUI)
		pd.SetUI(NULL, _T("CProtectedData Sample Application Test1"));

	pd.SetData((LPBYTE)(LPCTSTR)m_strSource, (DWORD)_tcslen(m_strSource)+1);
	
	const DATA_BLOB* r = pd.ProtectData(m_strDesc, m_strPassword);		// simple, no description, no UI, no 2nd entropy, no auditing

	if(r->cbData == 0)
	{
		l_strMsg.Format(_T("Failed, error: 0x%08x\n"), GetLastError());
		MessageBox(l_strMsg);
	}
	else
	{
		// if this file already exist, then delete it(but use _topen fun to create a file, file's attributes is read only, why?)
		::SetFileAttributes("c:\\jzh.dat", FILE_ATTRIBUTE_NORMAL);
		DeleteFile("c:\\jzh.dat");

		int fh=_topen( "c:\\jzh.dat",_O_CREAT|_O_BINARY|_O_RDWR);
		if(fh != -1)
		{
			//_lseek(fh, 0L, SEEK_END);
			_write(fh, r->pbData, r->cbData);
			_close(fh);
		}
		else
		{
			MessageBox("open file error!");
		}

		LPTSTR p1 = new TCHAR[(r->cbData+1) * 2];
		LPTSTR p = p1;
		for(DWORD i = 0; i < r->cbData; ++i)
		{
			TOHEX(r->pbData[i], p);
		}
		*p=0;

		m_strTarget.Format(_T("%s"), p1);
		UpdateData(FALSE);
		delete[] p1;
	}

	pd.FreeProtectedData();
	
}

void CTestDPAPIDlg::OnBtndecrypt() 
{
	// get target editbox data
	UpdateData();

	char buffer[1000];
	int fh=_topen("c:\\jzh.dat", _O_RDONLY|_O_BINARY);
	int count = _read(fh, buffer, 1000);
	_close( fh );

	CString l_strMsg;
	CProtectedData upd(!m_bShowUI, m_bMachine, m_bAudit);

	if(m_bShowUI)
		upd.SetUI(NULL, _T("CProtectedData Sample Application Test1"));

	upd.SetData((LPBYTE)buffer, count);

	LPTSTR l_pDesc = 0;
	DATA_BLOB* pdata = upd.UnprotectData(&l_pDesc, m_strPassword);
	if(pdata->cbData == 0)
	{
		l_strMsg.Format(_T("Failed, error: 0x%08x\n"), GetLastError());
		MessageBox(l_strMsg);
	}
	else
	{
		//m_strSource = pdata->pbData;
		l_strMsg.Format(_T("Source Data: %s. Desc: %s"), pdata->pbData, l_pDesc);
		upd.FreeUnprotectedData();

		MessageBox(l_strMsg);
	}

	/*
	// clear source editbox content
	//m_strSource.Empty();

	CString l_strMsg;
	CProtectedData upd(TRUE);

	upd.SetData((LPBYTE)(LPCTSTR)m_strTarget, (DWORD)_tcslen(m_strSource)+1);

	DATA_BLOB* pdata = upd.UnprotectData();
	if(pdata->cbData == 0)
	{
		l_strMsg.Format(_T("Failed, error: 0x%08x\n"), GetLastError());
		MessageBox(l_strMsg);
	}
	else
	{
		m_strSource = pdata->pbData;
		upd.FreeUnprotectedData();
	}

  */
}

⌨️ 快捷键说明

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