下面这一段代码,不是在所有卡片上都能成功运行。
写出来的意义是,让大家对于智能卡操作的流程有个大致了解,大致熟悉一下相关函数怎么用。
具体API文档,微软介绍的很清楚,网上也很多,不赘述。
基本上流程就是:
安装的库基本上就那么几样东西:
libccid pcscd libdbus-1-dev libpcsclite-dev
差不多就这几个吧,不够的就再查查,哈哈哈哈
make命令大致如下:
gcc -g -o smartcard smartcard.cpp -lpcsclite -I /usr/include/PCSC/
/// File Name: smartcard.cpp
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "pcsclite.h"
#include "winscard.h"
#include "reader.h"
int main(void) {
SCARDCONTEXT m_hContext;
SCARDHANDLE m_hCard;
SCARD_IO_REQUEST io;
char pmszReaders[512];
// BYTE CAPDU[] = {0x00,0x84,0x00,0x00,0x08};
// BYTE CAPDU[] = {0xFF,0xA4,0x00,0x00,0x01,0x06};
BYTE CAPDU_0[] = {0x00,0xA4,0x00,0x00,0x02,0x3F,0x00}; //get main folder 0x3F00
BYTE CAPDU_1[] = {0x00,0x84,0x00,0x00,0x08}; //get random number,8 bytes
// BYTE CAPDU_1[] = {0xFF,0xA4,0x00,0x00,0x01,0x06}; //reset, power down and up
BYTE CAPDU_2[] = {0xFF,0xB0,0x00,0x00,0xFE}; //read card, 0x00 -> 0x08
BYTE RAPDU_0[1024];
BYTE RAPDU_1[1024];
BYTE RAPDU_2[1024];
DWORD rr0 = 1024, rr1 = 1024, rr2 = 1024;
DWORD cch = 100;
DWORD i = 0;
DWORD ret = 0;
//Create contex
printf("Create contex ");
// ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &m_hContext);
ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &m_hContext);
if(SCARD_S_SUCCESS != ret)
{
printf("error: 0x%lx = %ld\n"