4

我正在使用 ACS AET65 读卡器尝试将字符串存储到智能卡中,然后将其读回。我正在使用智能卡 IO API,并且能够获取终端并与卡连接。但是,我一直在阅读 ISO 7816 规范,我真的迷路了。

我需要做的就是将一个 3K 字符串写入卡,然后将其读回。而已。根据我的研究,似乎这些卡上应该安装了小程序,但我确信必须有一种方法可以将纯字节数组写入其中并将其取回。

我不知道如何为此构建 APDU 命令。我尝试了 READ BINARY、WRITE BINARY、ERASE BINARY,但我肯定做错了什么。它总是返回 0x6E 和 0x00 作为响应的 SW1 和 SW2 字节,这意味着错误。这是我使用小字符串向小程序发送测试命令的部分的片段:

Card card = cardTerminal.connect("*");
card.beginExclusive();
System.out.println("Card protocol: "+card.getProtocol());
CardChannel channel = card.getBasicChannel();

String jsonStr = "small test string";

byte[] totalData = new byte[256];

byte[] data = jsonStr.getBytes();

System.arraycopy(data, 0, totalData, 0, data.length);

CommandAPDU eraseCommand = new CommandAPDU(0x00, 0x0E, 0x00, 0x00, data, 0x00);
ResponseAPDU eraseCommandResponse = channel.transmit(eraseCommand);

int eSw1 = eraseCommandResponse.getSW1();
int eSw2 = eraseCommandResponse.getSW2();


// returns 6E00, error
System.out.println("Erase Response SW1: " + toHexString(eSw1) + " and SW2: " + toHexString(eSw2));


CommandAPDU writeCommand = new CommandAPDU(0x00, 0xD0, 0x00, 0x00, data, 0x00);
ResponseAPDU commandResponse = channel.transmit(writeCommand);

int sw1 = commandResponse.getSW1();
int sw2 = commandResponse.getSW2();

// returns 6E00, error    
System.out.println("Write Response SW1: " + toHexString(sw1) + " and SW2: " + toHexString(sw2));

byte[] totalReadData = new byte[255];
CommandAPDU readCommand = new CommandAPDU(0x00, 0xB0, 0x00, 0x00, totalReadData, 0);
ResponseAPDU readCommandResponse = channel.transmit(readCommand);

int rSw1 = readCommandResponse.getSW1();
int rSw2 = readCommandResponse.getSW2();

// returns 6E00, error
System.out.println("Read Response SW1: " + toHexString(rSw1) + " and SW2: " + toHexString(rSw2));

byte[] totalReadData2 = readCommandResponse.getData();

// always returns an empty array
System.out.println("Total data read: "+totalReadData2.length);

card.endExclusive();

如何使用智能卡 API 完成此操作?

谢谢!!爱德华多

4

1 回答 1

3

智能卡有多种形式。ISO 7816-4 规范为基于文件和记录的卡片指定了一个框架。许多卡片和小程序至少在一定程度上符合此规范。

智能卡基本上是片上系统,尽管它们通常在 I/O 功能和规格方面极为有限。这些智能卡运行操作系统。有时这些操作系统与应用层融合,提供基本的 ISO 7816-4 功能和文件系统。其他卡仅提供为应用程序提供 API 的操作系统,并为这些应用程序加载/执行功能。Java Card 就是一个例子;基本上,您发送的所有命令 APDU 都由 Java Card 小程序处理,除了 Global Platform 指定的那些(它负责大多数 Java Card 上的卡管理和应用程序上传)。

有了这些信息,您就会明白,仅仅发送任何命令 APDU——包括 ERASE BINARY(新卡通常不支持)、READ BINARY 或 UPDATE BINARY APDU——都不是可行的方法。您将需要有关您的卡的更多信息才能继续,是的,如果您有一个 Java Card 实现,您可能需要上传一个 Applet,然后才能发送任何应用程序级 APDU。

于 2013-05-05T09:51:12.427 回答