How to differentiate between smart card reader error and smart card error

时间:2017-06-15 09:45:05

标签: java smartcard apdu smartcard-reader pcsc

I have implemented an Andoid app - server side application. The server communicates with the smart card reader. When the user touches the button in the Android app, a connenction is being built to the server to get the user authenticated. The exchanged messages between the app and server have the following format:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 <[data]>
  • If the message has the type value 06 that indicates an error in the smart card reader.
  • If the message has the type value 07 that indicates an error in the smart card.

I am using code like below for the communication with the smart card reader:

// show the list of available terminals
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
// get the first terminal
CardTerminal terminal = terminals.get(0);
// establish a connection with the card
Card card = terminal.connect("T=0");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
ResponseAPDU r = channel.transmit(new CommandAPDU(c1));
System.out.println("response: " + toString(r.getBytes()));
// disconnect
card.disconnect(false);

The Smart Card IO API has the CardException class for exceptions. My problem is that I do not know when to send the message of type 06 or 07 because I can not differentiate between errors that are generated by the card and errors that are generated by the reader when the CardException is thrown. How can I manage that?

1 个答案:

答案 0 :(得分:1)

中使用的System.out.println(new BigDecimal("1.2846202978398e+19").toBigInteger()); 方法
transmit()

只会在与智能卡读卡器错误相关的情况下以及读卡器和智能卡之间的通信问题中抛出异常。当卡本身指示错误时,它不会抛出异常。

因此,您可以通过捕获异常来捕获所有与读者相关的错误:

ResponseAPDU r = channel.transmit(new CommandAPDU(c1));

相反,卡生成的错误表示为响应状态字中编码的错误代码。这些错误不会生成Java异常。您可以通过检查状态字(try { ResponseAPDU r = channel.transmit(new CommandAPDU(c1)); } catch (IllegalStateException e) { // channel has been closed or if the corresponding card has been disconnected } catch (CardException e) { // errors occured during communication with the smartcard stack or the card itself (e.g. no card present) } 的方法getSW())来测试这些错误:

ResponseAPDU