我正在尝试阅读爱沙尼亚身份证的个人文件。
我需要将以下数据发送到卡(从 这里),以便从个人文件中读取记录(即 ID 号、姓名等):
00 A4 00 0C # We choose root folder
00 A4 01 0C 02 EE EE # We choose folder EEEE
00 A4 02 04 02 50 44 # We choose file 5044, which contains personal data
00 B2 XX 04 # We read record XX (see table) from the personal data file
# The card responds 61 YY, where YY denotes the number of bytes waiting to be read
00 C0 00 00 YY # We read YY bytes from the card
# Card responds ... 90 00, where ... is the requested data and 90 00 means status OK
但是,原始字节在 T=0 协议中,并且卡在接受 T=0 之前卡在 T=1 的时间过长。事件顺序如下:
- 卡连接到读卡器
- 程序返回
SCardStatusChange
并开始处理卡 - 在尝试连接到卡(
SCardConnect
或SCardReconnect
)SCARD_E_SHARING_VIOLATION
时,一遍又一遍地收到错误,大约 5 秒 - 然后,在尝试连接时,
SCARD_E_PROTO_MISMATCH
会在 3 到 30 秒之间的任何时间收到错误,甚至更多。 - 之后,卡连接成功并读取数据。
我可以在 T=0 协议中以某种方式更快地连接到它吗?
我的源代码的简化版本如下:
// NOTE: this is approximately what I do.
// I haven't tested this code yet - it's almost 1 AM here.
#include <winscard.h>
void readSmartCard() {
LONG sCardErrorCode;
SCARDCONTEXT sCardContext;
DWORD sCardReaderStrLen = 1024;
wchar_t sCardReaderStr[1024];
SCARDHANDLE sCardHandle;
DWORD sCardActiveProtocol;
SCARD_READERSTATE readerState;
sCardErrorCode = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &sCardContext);
// error handling macro
ZeroMemory(&sCardReaderState, sizeof(sCardReaderState));
sCardReaderState.szReader = L"\\\\?PnP?\\Notification";
sCardReaderState.pvUserData = nullptr;
sCardReaderState.dwEventState = SCARD_STATE_PRESENT;
sCardErrorCode = SCardGetStatusChange(sCardContext, INFINITE, &readerState, 1);
// e.h.m
if (readerState.dwCurrentState == 65538) {
sCardErrorCode = SCardListReaders(sCardContext, NULL, sCardReaderStr, &sCardReaderStrLen);
// e.h.m
readerState.szReader = sCardReaderStr;
}
sCardErrorCode = SCardGetStatusChange(sCardContext, INFINITE, &readerState, 1);
// e.h.m
if (sCardReaderState.dwEventState & SCARD_STATE_PRESENT) {
while (true) {
sCardErrorCode = SCardConnect(sCardContext, readerState.szReader, SCARD_SHARE_EXCLUSIVE,
SCARD_PROTOCOL_T0, &sCardHandle, &sCardActiveProtocol);
// e.h.m
printf("%x", sCardErrorCode);
// this will print:
// 8010000b (for around 5s)
// 8010000f (for around 20s)
if (sCardErrorCode == SCARD_S_SUCCESS) {
break;
}
Sleep(1000);
}
// open personal file and read data, yay!
}
}