尝试使用智能卡I / O API读取和写入智能卡的字符串

时间:2013-05-04 20:29:05

标签: java smartcard smartcard-reader

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

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

我不知道为此构建APDU命令。我尝试了READ BINARY,WRITE BINARY,ERASE BINARY,但我当然做错了。它总是返回0x6E和0x00作为响应的SW1和SW2字节,这意味着错误。这是我用一个小字符串向applet发送测试命令的部分的一小部分:

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完成此操作?

谢谢!! 爱德华

1 个答案:

答案 0 :(得分:3)

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

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

通过这些信息,您将了解只发送任何命令APDU - 包括ERASE BINARY(通常在新卡上不支持),READ BINARY或UPDATE BINARY APDU - 不是要走的路。您需要有关卡的更多信息才能继续,是的,如果您有Java Card实施,则可能需要上传Applet才能发送任何应用程序级APDU。

相关问题