我正在尝试将一个小程序加载到智能卡中,然后我只想使用以下代码进行一些测试:
import javax.smartcardio.*;
import java.util.*;
public class TestSmartCardIO {
public static String toString(byte[] bytes) {
StringBuffer sbTmp = new StringBuffer();
for(byte b : bytes){
sbTmp.append(String.format("%X", b));
}
return sbTmp.toString();
}
public static void main(String[] args) {
try {
TerminalFactory factory = TerminalFactory.getDefault();
List terminals = factory.terminals().list();
System.out.println("Terminals count: " + terminals.size());
System.out.println("Terminals: " + terminals);
// Get the first terminal in the list
CardTerminal terminal = (CardTerminal) terminals.get(0);
// Establish a connection with the card using
// "T=0", "T=1", "T=CL" or "*"
Card card = terminal.connect("*");
System.out.println("Card: " + card);
// Get ATR
byte[] baATR = card.getATR().getBytes();
System.out.println("ATR: " + TestSmartCardIO.toString(baATR) );
CardChannel channel = card.getBasicChannel();
/*SELECT Command
See GlobalPlatform Card Specification (e.g. 2.2, section 11.9)
CLA: 00
INS: A4
P1: 04 i.e. b3 is set to 1, means select by name
P2: 00 i.e. first or only occurence
Lc: 08 i.e. length of AID see below
Data: A0 00 00 00 03 00 00 00
AID of the card manager,
in the future should change to A0 00 00 01 51 00 00*/
byte[] baCommandAPDU = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x08, (byte) 0xA0, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00};
System.out.println("APDU >>>: " + TestSmartCardIO.toString(baCommandAPDU));
ResponseAPDU r = channel.transmit(new CommandAPDU(baCommandAPDU));
System.out.println("APDU <<<: " + TestSmartCardIO.toString(r.getBytes()));
// Disconnect
// true: reset the card after disconnecting card.
card.disconnect(true);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
所以我只想测试卡是否被识别以及是否可以正确发送 APDU。我试图通过 APDU 选择 AID 卡管理器,但我得到:
Terminals count: 1
Terminals: [PC/SC terminal OT MicroSD smartcard Reader 1]
Card: PC/SC card in OT MicroSD smartcard Reader 1, protocol T=1, state OK
ATR: 3BDB96081B1FE451F83031C0641A181019005D
APDU >>>: 0A4408A00003000
APDU <<<: 6A82
还有就是SW1 = 6A
卡SW2 = 82
没有找到AID卡管家……正常吗?我不太明白,我想知道它是否与卡使用协议的事实有关T = 1
?非常感谢您的帮助