我正在使用 Ubuntu 18 并尝试编写代码以使用智能卡。
我正在使用 ACS APG8201-b2 智能卡读卡器,并且已安装 the official driver(PC/SC 驱动程序包 3.04 MB,版本 1.1.8,2020 年 1 月 10 日)和 libpcsclite-dev
。
作为起点,我正在尝试运行 Ludovic Rousseau 的示例 C 代码 here 。
我可以将我的代码 ApduTool.c
编译为:
#ifdef WIN32
#undef UNICODE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <winscard.h>
#ifdef WIN32
static char *pcsc_stringify_error(LONG rv)
{
static char out[20];
sprintf_s(out, sizeof(out), "0x%08X", rv);
return out;
}
#endif
#define CHECK(f, rv) \
if (SCARD_S_SUCCESS != rv) \
{ \
printf(f ": %s\n", pcsc_stringify_error(rv)); \
return -1; \
}
int main(void)
{
LONG rv;
SCARDCONTEXT hContext;
LPTSTR mszReaders;
SCARDHANDLE hCard;
DWORD dwReaders, dwActiveProtocol, dwRecvLength;
SCARD_IO_REQUEST pioSendPci;
BYTE pbRecvBuffer[258];
BYTE cmd1[] = { 0x00, 0xA4, 0x04, 0x00, 0x0A, 0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01 };
BYTE cmd2[] = { 0x00, 0x00, 0x00, 0x00 };
unsigned int i;
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
CHECK("SCardEstablishContext", rv)
#ifdef SCARD_AUTOALLOCATE
dwReaders = SCARD_AUTOALLOCATE;
rv = SCardListReaders(hContext, NULL, (LPTSTR)&mszReaders, &dwReaders);
CHECK("SCardListReaders", rv)
#else
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
CHECK("SCardListReaders", rv)
mszReaders = calloc(dwReaders, sizeof(char));
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
CHECK("SCardListReaders", rv)
#endif
printf("reader name: %s\n", mszReaders);
return 0;
}
使用 Makefile
:all: ApduTool.c
gcc -c ApduTool.c -lsqlite3 -lpcsclite -lcrypto -lssl -I/usr/local/include/PCSC/ -I/usr/lib/ -I.
gcc ApduTool.o -o ApduTool -lsqlite3 -lpcsclite -lcrypto -lssl -I/usr/local/include/PCSC/ -I/usr/lib/ -I.
clean:
rm -f ApduTool *.o
当我运行 ApduTool 二进制文件时,SCardListReaders
函数返回:SCardListReaders: Cannot find a smart card reader.
我猜不出问题出在哪里,因为我在运行 lsusb
时可以看到我的 USB 阅读器:Bus 001 Device 003: ID 072f:8206 Advanced Card Systems, Ltd
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
并且我在运行代码时运行了 PCSC 守护程序( pcscd
)。谁能帮我解决这个问题?我是否错过了安装特定驱动程序?
最佳答案
pcscd 应该记录正在发生的事情以及是否找到了读卡器驱动程序。
见 https://pcsclite.apdu.fr/#support了解如何生成正确的 pcscd 日志。
关于Ubuntu 无法识别我的智能卡读卡器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70054099/