?? smartcarddlg.cpp
字號:
// smartcardDlg.cpp : implementation file
//
#include "stdafx.h"
#include "smartcard.h"
#include "smartcardDlg.h"
#include "dcic32.h"
#include "HDSSSE32.h"
#include "SSSE32.h"
#include "cpu.h"
#include "des.h"
#include "SendCmd.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()
/////////////////////////////////////////////////////////////////////////////
// CSmartcardDlg dialog
CSmartcardDlg::CSmartcardDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSmartcardDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSmartcardDlg)
m_reader = _T("");
m_name = _T("");
m_sex = _T("");
m_date = _T("");
m_id = _T("");
m_cardno = _T("");
m_pcno = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSmartcardDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSmartcardDlg)
DDX_Control(pDX, IDC_COMBO2, m_readercnt);
DDX_CBString(pDX, IDC_COMBO2, m_reader);
DDX_Text(pDX, IDC_EDIT1, m_name);
DDX_Text(pDX, IDC_EDIT2, m_sex);
DDX_Text(pDX, IDC_EDIT6, m_date);
DDX_Text(pDX, IDC_EDIT5, m_id);
DDX_Text(pDX, IDC_EDIT3, m_cardno);
DDX_Text(pDX, IDC_EDIT4, m_pcno);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSmartcardDlg, CDialog)
//{{AFX_MSG_MAP(CSmartcardDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSmartcardDlg message handlers
BOOL CSmartcardDlg::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 CSmartcardDlg::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 CSmartcardDlg::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 CSmartcardDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSmartcardDlg::OnButton1()
{
// TODO: Add your control notification handler code here
char dev_Name[10] = {'A','U','T','O'};
int nIndex = m_readercnt.GetCurSel(); //GetCurSel() 函數:用以得到用戶選中下拉列表框中數據的索引值.返回的值是重0開始的,如果沒有選擇任何選項將會返回-1
m_readercnt.GetLBText(nIndex,m_reader); //其原型為void GetLBText( int nIndex, CString&rString ).nIndex為列表框中數據的索引值,rString 為字符串,其作用就是把索引號為nIndex的數據放到rString變量中.與其對應的GetLBTextLen(intnIndex)函數就是得到索引號為nIndex中數據的長度
//判斷讀卡器類型
if(m_reader=="德卡")
{
theApp.m_readerType=1;
}
else if(m_reader=="明華")
{
theApp.m_readerType=2;
}
else if(m_reader=="華大")
{
theApp.m_readerType=3;
}
//打開讀卡器
if(theApp.m_readerType == 1)
{
theApp.icdev = IC_InitComm(100);
if (int(theApp.icdev) >0)
{
AfxMessageBox("打開德卡讀卡器成功!");
}
else
{
AfxMessageBox("打開德卡讀卡器失敗!");
}
}
else if(theApp.m_readerType == 2)
{
theApp.HandleReader1 = ICC_Reader_Open(dev_Name);
if(theApp.HandleReader1 > 0)
{
AfxMessageBox("打開明華讀卡器成功!");
}
else
{
AfxMessageBox("打開明華讀卡器失敗!");
}
}
else if(theApp.m_readerType == 3)
{
theApp.HandleReader2 = icc_reader_Open(dev_Name);
if(theApp.HandleReader2 >= 0)
{
AfxMessageBox("打開華大讀卡器成功!");
}
else
{
AfxMessageBox("打開華大讀卡器失敗!");
}
}
else
{
AfxMessageBox("未找到讀卡器!");
}
}
void CSmartcardDlg::OnButton3()
{
// TODO: Add your control notification handler code here
int st;
if(theApp.m_readerType == 1)
{
st = IC_ExitComm(theApp.icdev);
if(st == 0)
{
AfxMessageBox("關閉德卡讀卡器成功!");
}
else
{
AfxMessageBox("關閉德卡讀卡器失敗!");
}
}
else if(theApp.m_readerType == 2)
{
if(theApp.HandleReader1 > 0)
{
ICC_Reader_Close(theApp.HandleReader1);
AfxMessageBox("關閉明華讀卡器成功!");
}
else
{
AfxMessageBox("關閉明華讀卡器失敗!");
}
}
else if(theApp.m_readerType == 3)
{
if(theApp.HandleReader2 >= 0)
{
icc_reader_Close(theApp.HandleReader2);
AfxMessageBox("關閉華大讀卡器成功!");
}
else
{
AfxMessageBox("關閉華大讀卡器失敗!");
}
}
}
void CSmartcardDlg::OnButton2()
{
// TODO: Add your control notification handler code here
int st;
unsigned char resp[512];
unsigned char resplen;
unsigned char cmd[260];
unsigned char name[20]; //姓名
unsigned char sex[5]; //性別
unsigned char id[20]; //身份證號
unsigned char date[10]; //發卡日期
unsigned char date_asc[20];
unsigned char card_num[20]; //醫療證號
unsigned char pc_num[20]; //電腦號
unsigned int cmdlen;
unsigned int sw1sw2;
WORD SW1SW2;
long ST;
long resplength;
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("卡復位失敗");
}
//============================讀數據====================================
//選文件MF (用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\x3f\x00", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件MF失敗");
}
//選文件DDF1(用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xdd\xf1", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件DDF1失敗");
}
//選文件EF05
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xEF\x05", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件EF06失敗");
}
//讀記錄 DDF1.EF05.05(用戶卡) 發卡日期
cmdlen = 0x05;
memcpy(cmd, "\x00\xB2\x05\x04\x06", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("讀取發卡日期失敗");
}
else
{
//獲得讀出的數據
memcpy(date,resp+2,resplen-2);
HexToAscii(date,date_asc,resplen-2);
date_asc[(resplen-2)*2] = 0x00;
m_date.Format("%s",date_asc);
}
//選文件MF (用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\x3f\x00", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件MF失敗");
}
//選文件DDF1(用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xdd\xf1", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件DDF1失敗");
}
//選文件EF06
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xEF\x06", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件EF06失敗");
}
//讀記錄 DDF1.EF06.02(用戶卡) 姓名
cmdlen = 0x05;
memcpy(cmd, "\x00\xB2\x02\x04\x20", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if(sw1sw2 != 0x9000)
{
AfxMessageBox("讀取姓名失敗");
}
else
{
memcpy(name,resp+2,resplen-2);
name[resplen-2] = 0x00;
m_name.Format("%s",name);
}
//讀記錄 DDF1.EF06.03(用戶卡) 性別
cmdlen = 0x05;
memcpy(cmd, "\x00\xB2\x03\x04\x03", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("讀取姓名失敗");
}
else
{
memcpy(sex,resp+2,resplen-2);
if(sex[0x00] == 0x31)
{
m_sex.Format("%s","男");
}
else if(sex[0x00] == 0x32)
{
m_sex.Format("%s","女");
}
}
//讀記錄 DDF1.EF06.01(用戶卡) 公民身份號碼
cmdlen = 0x05;
memcpy(cmd, "\x00\xB2\x01\x04\x14", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("讀取身份證號失敗");
}
else
{
//獲得讀出的數據
memcpy(id,resp+2,resplen-2);
id[resplen-2] = 0x00;
m_id.Format("%s",id);
}
//============================讀數據====================================
//選文件MF (用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\x3f\x00", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件MF失敗");
}
//選文件DDF1(用戶卡)
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xdd\xf1", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件DDF1失敗");
}
//用戶卡校驗密碼 (用戶卡)
cmdlen = 0x08;
memcpy(cmd, "\x00\x20\x00\x00\x03\x12\x34\x56", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
switch(sw1sw2)
{
case 0x63c3:
AfxMessageBox("還有三次重新輸入密碼機會");
break;
case 0x63c2:
AfxMessageBox("還有二次重新輸入密碼機會");
break;
case 0x63c1:
AfxMessageBox("還有一次重新輸入密碼機會");
break;
case 0x63c0:
AfxMessageBox("再輸入錯誤認證會導致鎖定");
break;
default:
AfxMessageBox("認證鎖定");
}
}
//選文件DF04
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xDF\x04", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件DF04失敗");
}
//選文件EF05
cmdlen = 0x07;
memcpy(cmd, "\x00\xA4\x00\x00\x02\xEF\x05", cmdlen);
sw1sw2 = SendCmd(theApp.icdev, CardSet_User, cmd,cmdlen, resp, &resplen);
if (sw1sw2 != 0x9000)
{
AfxMessageBox("選擇文件EF05失敗");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -