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

📄 cpu.cpp

📁 智能卡密钥加密
💻 CPP
字号:
// cpu.cpp : implementation file
//

#include "stdafx.h"
#include "smartcard.h"
#include "cpu.h"
#include "smartcardDlg.h"
#include "dcic32.h"
#include "HDSSSE32.h"
#include "SSSE32.h"
#include "des.h"
#include "SendCmd.h"


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

CCpu::CCpu(CWnd* pParent /*=NULL*/)
	: CDialog(CCpu::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCpu)
	m_ATR = _T("");
	m_CAPDU = _T("");
	m_RAPDU = _T("");
	//}}AFX_DATA_INIT
}


void CCpu::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCpu)
	DDX_Text(pDX, IDC_EDIT1, m_ATR);
	DDX_Text(pDX, IDC_EDIT2, m_CAPDU);
	DDX_Text(pDX, IDC_EDIT3, m_RAPDU);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCpu, CDialog)
	//{{AFX_MSG_MAP(CCpu)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCpu message handlers


void CCpu::OnButton1() 
{
	// TODO: Add your control notification handler code here
	unsigned char ATR[20];
	unsigned char ATR_asc[40];
	unsigned char resp[512];
	unsigned char resplen;
	long ST;
	long resplength;
	int st;

	UpdateData(TRUE);
	
	if(theApp.m_readerType == 1)                       //选择的是德卡读卡器
	{
		//选择主卡座(用户卡)
		st = IC_InitType(theApp.icdev, CardSet_User);
		if (st < 0)
		{
			AfxMessageBox("选择卡座类型失败");
		}
		
		//卡片复位(用户卡)
		st=IC_CpuReset(theApp.icdev, &resplen, resp);
		if (st < 0)
		{
			AfxMessageBox("卡复位失败");
		} 
		else
		{
		    memcpy(ATR,resp,resplen);

			HexToAscii(ATR,ATR_asc,resplen);

			ATR_asc[resplen*2] = 0x00;

			m_ATR.Format("%s",ATR_asc);
		}
	}

	else if(theApp.m_readerType == 2)
	{
	     //用户卡复位
		ST = ICC_Reader_PowerOn(theApp.HandleReader1, cardset_user, resp);
		
		if (ST < 0)
		{
			AfxMessageBox("卡片复位失败");
		}
		else
		{
		    memcpy(ATR,resp,ST);

			HexToAscii(ATR,ATR_asc,ST);

			ATR_asc[ST*2] = 0x00;

			m_ATR.Format("%s",ATR_asc);
		}
	}

	else if(theApp.m_readerType == 3)
	{
	     //用户卡复位
		ST = icc_reader_PowerOn(theApp.HandleReader2, cardset_User, resp);
		
		if (ST < 0)
		{
			AfxMessageBox("卡片复位失败");
		}

		else
		{
		    memcpy(ATR,resp,ST);

			HexToAscii(ATR,ATR_asc,ST);

			ATR_asc[ST*2] = 0x00;

			m_ATR.Format("%s",ATR_asc);
		}
	}

	UpdateData(FALSE);

}

void CCpu::OnButton2() 
{
	// TODO: Add your control notification handler code here
	unsigned char cmd[300];
	unsigned char cmd_asc[600];
	WORD cmd_asc_len;
	WORD sw1sw2;
	unsigned char resp[512];
	unsigned char resp_asc[1024];
	unsigned char resplen;
	long resplength;
	CString tmp;

	UpdateData(TRUE);

	tmp = m_CAPDU;
	tmp.Remove(' ');
	strcpy((char *)cmd_asc,tmp);
	cmd_asc_len = strlen((char *)cmd_asc);
	AsciiToHex(cmd_asc,cmd,cmd_asc_len);

	if(theApp.m_readerType == 1)                       //选择的是德卡读卡器
	{
		sw1sw2 = SendCmd(theApp.icdev,CardSet_User,cmd,cmd_asc_len/2,resp,&resplen);

		HexToAscii(resp,resp_asc,resplen);
	    resp_asc[2 * resplen] = 0x00;

		if(sw1sw2 == 0x9000)
		{
		   m_RAPDU.Format("%s %.4X",resp_asc, sw1sw2);
		}
		else
        m_RAPDU.Format("错误码:%.4X",sw1sw2);
			
	}

	else if(theApp.m_readerType == 2)
	{
	     sw1sw2 = SENDCMD1(theApp.HandleReader1,cardset_user,cmd, cmd_asc_len/2, resp, &resplength);
		 
		 HexToAscii(resp,resp_asc,resplength);
	     resp_asc[2 * resplength] = 0x00;

		 if(sw1sw2 == 0x9000)
		 {
		    m_RAPDU.Format("%s %.4X",resp_asc, sw1sw2);
		 }
		 else
         m_RAPDU.Format("错误码:%.4X",sw1sw2);


	}

	else if(theApp.m_readerType == 3)
	{
	     sw1sw2 = SENDCMD2(theApp.HandleReader2,cardset_User,cmd, cmd_asc_len/2, resp, &resplength);
		 
		 HexToAscii(resp,resp_asc,resplength);
	     resp_asc[2 * resplength] = 0x00;

		 if(sw1sw2 == 0x9000)
		 {
		    m_RAPDU.Format("%s %.4X",resp_asc, sw1sw2);
		 }
		 else
         m_RAPDU.Format("错误码:%.4X",sw1sw2);
	}

	UpdateData(FALSE);
}

⌨️ 快捷键说明

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