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

📄 usbkeyinittoolsdlg.cpp

📁 作为信息安全的一辅助工具,主要用于初始化USBKEY,从而使USBKEY存储用户的相关信息.
💻 CPP
字号:
// UsbKeyInitToolsDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "UsbKeyInitTools.h"
#include "UsbKeyInitToolsDlg.h"

#include "Pub/HKCommand.h"
#include "Pub/HTCSPAct.h"
#include "Pub/HKError.h"
#include "Pub/HTError.h"
#include ".\usbkeyinittoolsdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


LPVOID g_hContext = NULL;
LPCSTR g_szSOPin = "1111";
LPCSTR g_szUserPin = "1111";
LPCSTR g_ManagerKeyStr = "UsbManagerAdministrator";
LPCSTR g_CommanKeyStr = "UsbKey";
#define MAX_SIZE 1024

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

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

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CUsbKeyInitToolsDlg 对话框

int CUsbKeyInitToolsDlg::GetUsbKeyCount(void)
{
	DWORD dwI, dwReaderLen, dwReaderNum, dwState;
	char * pszReader;

	m_cb0.ResetContent();

    if(HKListReaders(NULL, &dwReaderLen, &dwReaderNum) != 0)
    {
        return -1;
    }

	if((pszReader = (char *)malloc(dwReaderLen)) == NULL)
	{
        return -1;
	}
	memset(pszReader, 0, dwReaderLen);

    if(HKListReaders(pszReader, &dwReaderLen, &dwReaderNum) != 0)
    {
        free(pszReader);
        return -1;
    }

	dwReaderNum = 0;
    dwI = 0;
    while(pszReader[dwI] != 0x00)
	{
		if(HKGetDevState(&pszReader[dwI], &dwState) != 0)
        {
            free(pszReader);
            return -1;
        }

        if(dwState == HGS_STATE_PRESENT)
        {
			m_cb0.AddString(&pszReader[dwI]);
			dwReaderNum++;
        }
		dwI += (strlen(&pszReader[dwI]) + 1);
	}
    free(pszReader);

    if(dwReaderNum > 0)
    {
        m_cb0.SetCurSel(0);
    }

	return dwReaderNum;
}

CUsbKeyInitToolsDlg::CUsbKeyInitToolsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUsbKeyInitToolsDlg::IDD, pParent)
	, m_bAdmin(FALSE)
	, m_sDWName(_T(""))
	, m_sGuid(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUsbKeyInitToolsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_cb0);
	DDX_Control(pDX, IDC_SHOWBIT, m_lb0);
	DDX_Control(pDX, IDC_CHK_LOGIN, m_bLoginKey);
	DDX_Check(pDX, IDC_CHK_LOGIN, m_bAdmin);
}

BEGIN_MESSAGE_MAP(CUsbKeyInitToolsDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
    ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_WM_DEVICECHANGE()
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_BN_CLICKED(IDC_CHK_LOGIN, OnBnClickedChkLogin)
	ON_BN_CLICKED(IDC_BTN_WRITE, OnBnClickedBtnWrite)
END_MESSAGE_MAP()


// CUsbKeyInitToolsDlg 消息处理程序

BOOL CUsbKeyInitToolsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

    m_keyNumber = GetUsbKeyCount();

	OnBnClickedChkLogin();
	
	return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CUsbKeyInitToolsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CUsbKeyInitToolsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CUsbKeyInitToolsDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

BOOL CUsbKeyInitToolsDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
    int keynb;

	if(nEventType != DBT_DEVNODES_CHANGED)
    {
		return TRUE;
    }
    
    ////::AfxMessageBox("事件触发。");
    keynb = GetUsbKeyCount();

    ///*CString str0;

    //str0.Format("发现UsbKey:%d个。", keynb);
    //::AfxMessageBox(str0);*/

    if(keynb > -1 && keynb != m_keyNumber)
    {
        m_keyNumber = keynb;
    }
	//CTime nTime;
	//nTime =CTime::GetCurrentTime();
	//CString Tmp;
	//Tmp.Format("%d:%d:%d",nTime.GetHour(),nTime.GetMinute(),nTime.GetSecond());
	//this->m_cLst.AddString(Tmp);
	return TRUE;
}



// 生成Guid
char * NewGuid(void)
{
	static char buf[64] = {0};
	GUID guid;
	if (S_OK == ::CoCreateGuid(&guid))
	{
		_snprintf(buf, sizeof(buf)
		, "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X"
		, guid.Data1
		, guid.Data2
		, guid.Data3
		, guid.Data4[0], guid.Data4[1]
		, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
		, guid.Data4[6], guid.Data4[7]
		);
	}
	return (char*)buf;
}


void CUsbKeyInitToolsDlg::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码

	UpdateData(TRUE);

	CString strReader;
    BYTE key_buf[1024+1];
	BYTE bRetryNum;

    if(m_cb0.GetCount() == 0)
    {
        return;
    }

	m_cb0.GetWindowText(strReader);

    m_lb0.SetWindowText(" 开始进行" + strReader + "的初始化,请稍候...");

    GetDlgItem(IDOK)->EnableWindow(FALSE);
    GetDlgItem(IDCANCEL)->EnableWindow(FALSE);

	if(HC_FormatHW(g_hContext, strReader, FALSE, FALSE, g_szSOPin, g_szUserPin, 
                   "Haitai HaiKey 0", HT_CSP_APP, TRUE) != 0)
    {
	    if(HC_EraseHW(g_hContext, strReader, g_szSOPin, TRUE, &bRetryNum) != 0)
        {
		    MessageBox("擦除和格式化HaiKey失败!");
            m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

            GetDlgItem(IDOK)->EnableWindow(TRUE);
            GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		    return;
	    }
	    if(HC_FormatHW(g_hContext, strReader, FALSE, FALSE, g_szSOPin, g_szUserPin, 
                    "Haitai HaiKey 0", HT_CSP_APP, TRUE) != 0)
        {
		    MessageBox("格式化HaiKey失败!");
            m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

            GetDlgItem(IDOK)->EnableWindow(TRUE);
            GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		    return;
	    }
	}

    if(HC_CreateFile(NULL, strReader, 10, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 40) != 0)
    {
		MessageBox("建立HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

    ::ZeroMemory(key_buf, 32 + 1);
    ::CopyMemory(key_buf, "12345678", 8);

    if(HC_WriteFile(NULL, strReader, 10, HT_NEEDNT_PIN, NULL, key_buf, 32 + 1, NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}

    if(HC_CreateFile(NULL, strReader, 20, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 600) != 0)
    {
		MessageBox("建立HaiKey文件失败!");  
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

    ::ZeroMemory(key_buf, 512 + 1);
    if(HC_WriteFile(NULL, strReader, 20, HT_NEEDNT_PIN, NULL, key_buf, 512 + 1, NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}


	//增加 的 文件用来保存  涉密介质信息***********
	if(HC_CreateFile(NULL, strReader, 30, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 1200) != 0)
    {
		MessageBox("建立HaiKey文件失败!");  
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

    ::ZeroMemory(key_buf, 1024+1);

    if(HC_WriteFile(NULL, strReader, 30, HT_NEEDNT_PIN, NULL, key_buf, 1024+1  , NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}






  //涉密介质的
    if(HC_CreateFile(NULL, strReader, 40, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 40) != 0)
    {
		MessageBox("建立HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

    ::ZeroMemory(key_buf, 32 + 1);
    ::CopyMemory(key_buf, "12345678", 8);

    if(HC_WriteFile(NULL, strReader, 40, HT_NEEDNT_PIN, NULL, key_buf, 32 + 1, NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}
   //
    if(HC_CreateFile(NULL, strReader, 50, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 600) != 0)
    {
		MessageBox("建立HaiKey文件失败!");  
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

    ::ZeroMemory(key_buf, 512 + 1);
	if (this->m_bAdmin)
		::CopyMemory(key_buf, g_ManagerKeyStr, strlen(g_ManagerKeyStr));
	else
		::CopyMemory(key_buf, g_CommanKeyStr, strlen(g_CommanKeyStr));

    if(HC_WriteFile(NULL, strReader, 50, HT_NEEDNT_PIN, NULL, key_buf, 512 + 1, NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}

	//增加对内网系统的支持   60# 70 #文件




	//增加对涉密2.0的支持  80 #文件

	if(HC_CreateFile(NULL, strReader, 80, HT_NEEDNT_PIN, HT_NEEDNT_PIN, 600) != 0)
    {
		MessageBox("建立HaiKey文件失败!");  
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
    }

	//存储格式:GUID ,企业名称,else...

		
	//UpdateData(TRUE);

	//char GuidBuf[50]=""; 
	//char CmpNyBuf[100]="";
	//

	//sprintf(GuidBuf,this->m_sGuid.GetBuffer());
	//m_sGuid.ReleaseBuffer();
	//sprintf(CmpNyBuf,this->m_sDWName.GetBuffer());
	//m_sDWName.ReleaseBuffer();


 //   ::ZeroMemory(key_buf, 512 + 1);
	//if (this->m_bAdmin)
	//{
	//	int nSumLen=0;
	//	int nLen = strlen(GuidBuf);
	//	::CopyMemory(key_buf, GuidBuf, nLen); nSumLen +=nLen;
	//	key_buf[nLen]=',';					  nSumLen ++;
	//	
	//	int nLen1 = strlen(CmpNyBuf);
	//	::CopyMemory(key_buf+nSumLen, CmpNyBuf, nLen1);
	//	
	//}

 //   if(HC_WriteFile(NULL, strReader, 80, HT_NEEDNT_PIN, NULL, key_buf, 512 + 1, NULL) != 0)
	//{
	//	MessageBox("写入HaiKey文件失败!");
 //       m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

 //       GetDlgItem(IDOK)->EnableWindow(TRUE);
 //       GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
	//	return;
	//}

















    m_lb0.SetWindowText(" " + strReader + "的初始化,执行完毕。");

    m_keyNumber = GetUsbKeyCount();

    GetDlgItem(IDOK)->EnableWindow(TRUE);
    GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
}




void CUsbKeyInitToolsDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	OnCancel();
}

void CUsbKeyInitToolsDlg::OnBnClickedChkLogin()
{
	//UpdateData();	
	//BOOL bCheck = m_bAdmin;

	//this->GetDlgItem(IDC_EDIT_DWName)->EnableWindow(bCheck);
	//this->GetDlgItem(IDC_EDIT_GUID)->EnableWindow(bCheck);
	//
	//if(bCheck)
	//{
	//	m_sGuid.Format("%s",NewGuid());
	//	UpdateData(FALSE);
	//	this->GetDlgItem(IDC_EDIT_DWName)->SetFocus();
	//}
	
}



void CUsbKeyInitToolsDlg::OnBnClickedBtnWrite()
{
	UpdateData(TRUE);

	CString strReader;
    BYTE key_buf[1024+1];
	BYTE bRetryNum;

    if(m_cb0.GetCount() == 0)
    {
        return;
    }

	m_cb0.GetWindowText(strReader);



	::ZeroMemory(key_buf, 32 + 1);
    ::CopyMemory(key_buf, "12345678", 8);
    if(HC_WriteFile(NULL, strReader, 10, HT_NEEDNT_PIN, NULL, key_buf, 32 + 1, NULL) != 0)
	{
		MessageBox("写入HaiKey文件失败!");
        m_lb0.SetWindowText(" " + strReader + "的初始化,执行错误。");

        GetDlgItem(IDOK)->EnableWindow(TRUE);
        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
		return;
	}
}

⌨️ 快捷键说明

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