java - 如何区分智能卡读卡器错误和智能卡错误

标签 java smartcard apdu smartcard-reader pcsc

我已经实现了一个 Andoid 应用程序 - 服务器端应用程序。服务器与智能卡读卡器通信。当用户触摸按钮时 在 Android 应用程序中,正在建立到服务器的连接以对用户进行身份验证。应用程序之间交换的消息 和服务器具有以下格式:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 <[data]>
  • 如果消息的类型值 06 表示智能卡读卡器出错。
  • 如果消息的类型值 07 表示智能卡中有错误。

我正在使用如下代码与智能卡读卡器通信:

// 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);

智能卡 IO API 具有用于异常的 CardException 类。我的问题是我不知道什么时候发送0607 类型的消息,因为我无法区分卡生成的错误和生成的错误当 CardException 被抛出时,由读者读取。我该如何管理?

最佳答案

transmit() 方法,用于

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

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

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

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)
}

相反,卡生成的错误表示为在响应状态字中编码的错误代码。这些错误不会生成 Java 异常。您可以通过检查状态字(ResponseAPDU 的方法 getSW())来测试这些错误:

if (r.getSW() == 0x09000) {
    // success indicated by the card
} else {
    // error or warning condition generated by the card
}

关于java - 如何区分智能卡读卡器错误和智能卡错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564224/

相关文章:

java - 无法将 Java 中提供程序的标签名称发送到 Pact Broker

java - 更改方向导致应用程序崩溃 android.support.v4.app.ListFragment

java - 删除文本文件中的空格

c# - 在 C# 中从 ATR 确定设施代码和卡号

Java 卡 检测到与智能卡的通信错误

java - 在Android中用手指旋转3D对象

testing - MTIP 非接触式测试 MP11 在 CVM 结果上失败

javascript - MVC 表单读取智能卡后自动发布

android - 使用 Android NFC 向 Java Card 发送超过 261 个字节

security - EMV 安全消息传递