【问题标题】:Smartcardio.Terminal connection error in PCSC Gemplus JavacardPCSC Gemplus Javacard 中的 Smartcardio.Terminal 连接错误
【发布时间】:2014-02-02 13:47:33
【问题描述】:

我对 Java Card 编程非常陌生。在我的代码中使用 javax.Smartcardio 时, 我在尝试连接到 Gemalto PCSC Java 卡时遇到错误。

import java.util.List;
import javax.smartcardio.*;

public class App 
{
   public static void main(String[] args) {
  try {
   // Display the list of terminals
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   System.out.println("Terminals: " + terminals);

   // Use the first terminal
   CardTerminal terminal = terminals.get(0);

   if (terminal.isCardPresent()) {
            System.out.println("Card present!");
        }

   // Connect with the card

   Card card = terminal.connect("*");
   System.out.println("card: " + card);
   CardChannel channel = card.getBasicChannel();

   // Send Select Applet command
   byte[] aid = {(byte)0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01};
  ResponseAPDU answer = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, aid));
  System.out.println("answer: " + answer.toString());

  // Send test command
  answer = channel.transmit(new CommandAPDU(0x00, 0x00, 0x00, 0x00));
   System.out.println("answer: " + answer.toString());
   byte r[] = answer.getData();
   for (int i=0; i<r.length; i++)
    System.out.print((char)r[i]);
    System.out.println();

   // Disconnect the card
   card.disconnect(false);
  } catch(Exception e) {
   System.out.println("Ouch: " + e.toString());
  }
 }
}

输出如下:

Terminals: [PC/SC terminal Gemplus USB Smart Card Reader 0]
Gemplus USB Smart Card Reader 0
Card present!
Ouch: javax.smartcardio.CardException: connect() failed

我无法弄清楚我的卡的真正问题。来自供应商的 VB API 似乎工作正常。我暂时不允许发布图片。

根据 API,

ATR: A2 13 10 91   
APDU Send Data: 20 12 01 01 00  
APDU Reply Data: A2 13 10 91 90 00  

感谢任何帮助。

【问题讨论】:

  • 根据TerminalImpl source,CardException 的原因应该是 PCSCException。尝试打印 e.getCause() 以便我们知道给出的错误代码。此外,查看 PCSC 守护程序的调试日志(如果可用)可能会有所帮助。请参阅 Ludovic Rousseau 的 instructions for Linuxinstructions for OSX
  • 感谢您的提示。 getCause() 给出 - SCARD_W_UNRESPONSIVE_CARD 错误。我相信我的卡已经死了。我已要求更换。完成后我会更新。
  • 您解决了这个问题吗?不幸的是,我在您(很好)提供的信息中没有看到任何提示能够回答它。
  • 是的,这个问题在更换我的智能卡后得到了解决。谢谢。

标签: connection-string javacard smartcard-reader


【解决方案1】:

查看 junit 测试以获取更多示例

jnasmartcardio

例如我做了如下操作并设法检测到SMARTCART是否已连接。

public class Prueba {

public static void main(String[] args) throws Exception
{        
    TerminalFactory context;

    Security.addProvider(new Smartcardio());
    context = TerminalFactory.getInstance("PC/SC", null, Smartcardio.PROVIDER_NAME);

    // Display the list of terminals
    List<CardTerminal> terminals_list = context.terminals().list();

    if (terminals_list.isEmpty())
    {
        System.err.println("No existen dispositivos conectados...");
        return;
    }

    // Use the first terminal
    CardTerminal terminal = terminals_list.get(0);

    if (terminal.isCardPresent())
    {
        System.out.println("La tarjeta electronica esta presente!: "+terminal.getName());
    }
    else
    {
        System.out.println("La tarjeta electronica NO esta presente");
    }
}
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多