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

📄 xkeyapiexampledlg.cpp

📁 vc++编写的eky算法源码。 1、 用户确认eKey被锁定(使用“eKey测试”工具测试
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// XKeyAPIExampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XKeyAPIExample.h"
#include "XKeyAPIExampleDlg.h"
#include "MwUsbD.h"
#include "XCAPI.h"
#include "dbt.h"

#define MAXRESPLEN 255
#define RESUME_SIGN_MODE 0
#define MAXDATALEN 1024

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

/////////////////////////////////////////////////////////////////////////////
// CXKeyAPIExampleDlg dialog

CXKeyAPIExampleDlg::CXKeyAPIExampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CXKeyAPIExampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CXKeyAPIExampleDlg)
	//}}AFX_DATA_INIT
	for(i=0;i<8;i++)
		icdev[i]=NULL;
	g_port=0;
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXKeyAPIExampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CXKeyAPIExampleDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_TITLE, m_title);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXKeyAPIExampleDlg, CDialog)
	//{{AFX_MSG_MAP(CXKeyAPIExampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CREATEFILESTRUCT, OnCreatefilestruct)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_READWRITEFILE, OnReadwritefile)
	ON_BN_CLICKED(IDC_RSAGENERATEKEYPAIR, OnRsageneratekeypair)
	ON_BN_CLICKED(IDC_SIGNVERIFY, OnSignverify)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	//}}AFX_MSG_MAP
	ON_WM_DEVICECHANGE()

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXKeyAPIExampleDlg message handlers

BOOL CXKeyAPIExampleDlg::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
	LOGFONT lf;
	memset(&lf,0,sizeof(LOGFONT));
    lf.lfHeight=30;
	lf.lfWidth=14;
	lf.lfWeight=FW_NORMAL;
	strcpy(lf.lfFaceName ,"宋体");
    g_font.CreateFontIndirect(&lf);
    m_title.SetFont(&g_font);

	//get ekey state
	st=Mw_GetDevState(pbRespData);
    if (st!=0)  
    {
        strPrompt.Format("GetDevState Error:  0x%02x",st);
		m_list.AddString(strPrompt);
		return TRUE;
	}
    for(i=0;i<8;i++)
	{
		if(pbRespData[i]==0x31) 
		{		
			//Connect(share mode)
			st=mw_init((long)i,1,icdev[i]);
			if (icdev[i]>0)
			{
				strPrompt.Format("Current Connect: MWDEV%d",i);
    			m_list.AddString (strPrompt);
    			g_port=i;
    			break;
			}
 		}
	}
	if(i==8)
	{
	    strPrompt.Format("Please Input Device!");
	    m_list.ResetContent();
	    m_list.AddString (strPrompt);
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BOOL CXKeyAPIExampleDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
    BOOL HaveDevice=FALSE;
	switch( nEventType)
	{
	case DBT_DEVICEARRIVAL:
		break;
	case DBT_DEVICEREMOVECOMPLETE:
		break; 
	case DBT_DEVNODES_CHANGED:
		unsigned char State[8];
		m_list.ResetContent();
		st=Mw_GetDevState(State);
    	if (st)
    		return FALSE;
	    mw_close(icdev[g_port]);
		for(i=0;i<8;i++)
		{
			if(State[i]==0x31)//get data 
			{		
				//Connect(share)
				st=mw_init((long)i,1,icdev[i]);
				if ((long)icdev[i]<0)
				{
					continue;
				}
				strPrompt.Format("Current Connect: MWDEV%d",i);
				m_list.AddString (strPrompt);
				HaveDevice=TRUE;
				g_port=i;
				break;
			}
		}
        if(HaveDevice==FALSE)
		{
			strPrompt.Format("Please Input Device!");
	        m_list.ResetContent();
	        m_list.AddString (strPrompt);
        }
		break;
	default:
		return TRUE;
	}
	return TRUE;

}

void CXKeyAPIExampleDlg::OnCreatefilestruct() 
{
	// TODO: Add your control notification handler code here
	m_list.ResetContent();
	if(icdev[g_port]==NULL)
	{
		strPrompt.Format("Please Input Device!");
		m_list.AddString(strPrompt);
		return;
	}
	
	//eKey Reset 
	dwRespLen=MAXRESPLEN;
	st=mw_reset(icdev[g_port],pbRespData,dwRespLen);
	if (st!=0)
    {
        strPrompt.Format("Reset Error:  0x%02x",st);
		m_list.AddString(strPrompt);
		return ;
	}
	hex_asc(pbRespData,(UCHAR*)szTempStr,dwRespLen);
	strPrompt.Format("Reset ATR: %s",szTempStr);
	m_list.AddString(strPrompt);
	m_list.AddString("");

	/////////////////////////////////////CreateFileStruct/////////////////////////////////////
	//Delete MF
	memcpy(pbCommData,"\x00\xE4\x00\x00\x02\x3F\x00",7);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Delete MF:");
	st=mw_protocol(icdev[g_port],7,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");

    //Verify initial PIN
	memcpy(pbCommData,"\x00\x20\xff\x00\x08\x11\x22\x33\x44\x55\x66\x77\x88",13);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Verify Initial PIN:");
	st=mw_protocol(icdev[g_port],13,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");


	//Create MF
	memcpy(pbCommData,"\x00\xe0\x00\x00\x1E\x62\x1C\x83\x02\x3f\x00\x81\x02\xff\xff\x82\x01\x38\x85\x01\x00\x84\x01\xAA\xab\x09\x80\x01\x54\x90\x00\x90\x00\x90\x00",35);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Create MF:");
	st=mw_protocol(icdev[g_port],35,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");
	
	//Create DF
	memcpy(pbCommData,"\x00\xe0\x00\x00\x1E\x62\x1C\x83\x02\xDf\x01\x81\x02\xff\xff\x82\x01\x38\x85\x01\x00\x84\x01\xBB\xab\x09\x80\x01\x54\x90\x00\x90\x00\x90\x00",35);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Create DF:");
	st=mw_protocol(icdev[g_port],35,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");

    //Create KeyFile
	memcpy(pbCommData,"\x00\xe0\x00\x00\x23\x62\x21\x83\x02\x00\x1C\x81\x02\x10\x00\x82\x01\x0A\x85\x01\x05\xab\x11\x80\x01\x7F\x97\x00\x97\x00\x90\x00\x97\x00\x90\x00\x97\x00\x97\x00",40);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Create KeyFile:");
	st=mw_protocol(icdev[g_port],40,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");
     
	//Add PIN
	//KeyID=1,KeyValue="1234"
	memcpy(pbCommData,"\x00\xE2\x00\xE0\x0a\x01\x80\x00\x00\x00\x33\x31\x32\x33\x34",15);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Add PIN: ");
	st=mw_protocol(icdev[g_port],15,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");

	//Active KeyFile
	memcpy(pbCommData,"\x00\x44\x02\x00\x02\x00\x1C",7);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Active KeyFile:");
	st=mw_protocol(icdev[g_port],7,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");

	//Create PublicFile
	memcpy(pbCommData,"\x00\xe0\x00\x00\x23\x62\x21\x83\x02\x00\x11\x81\x02\x00\x87\x82\x01\x01\x85\x01\x06\xab\x11\x80\x01\x7F\x90\x00\x90\x00\x90\x00\x90\x00\x90\x00\x90\x00\x90\x00",40);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Create PublicFile:");
	st=mw_protocol(icdev[g_port],40,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;
	}
	strPrompt.Format("Return:  %02x%02x",pbRespData[dwRespLen-2],pbRespData[dwRespLen-2+1]);
	m_list.AddString(strPrompt);
	m_list.AddString("");

	//Active PublicFile
	memcpy(pbCommData,"\x00\x44\x02\x00\x02\x00\x11",7);
	dwRespLen=MAXRESPLEN;
	m_list.AddString("Active PublicFile:");
	st=mw_protocol(icdev[g_port],7,pbCommData,dwRespLen,pbRespData);
	if (st!=0)
    {
        strPrompt.Format("Transfer Error:  %d",st);
		m_list.AddString(strPrompt);
		return ;

⌨️ 快捷键说明

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