Java智能卡 - 阅读Scosta智能卡

时间:2014-03-05 07:42:32

标签: java android macos smartcard scosta

我试图通过java smartcardio 阅读印度政府标准'Scosta'智能卡 我正在使用的代码是

package com.example.smartcardreader;

import java.util.List;

import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;

public class SmartCardReader {

 public static void main(String[] args) {

        try{

            // 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("*");
            System.out.println("card: " + card);

            // get the ATR
            ATR atr = card.getATR();
            byte[] baAtr = atr.getBytes();

            System.out.print("ATR = 0x");
            for(int i = 0; i < baAtr.length; i++ ){
                System.out.printf("%02X ",baAtr[i]);
            }

            CardChannel channel = card.getBasicChannel();
            byte[] cmdApduGetCardUid = new byte[]{
                        (byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00};

            ResponseAPDU respApdu = channel.transmit(
                                                new CommandAPDU(cmdApduGetCardUid));

            if(respApdu.getSW1() == 0x90 && respApdu.getSW2() == 0x00){

                byte[] baCardUid = respApdu.getData();

                System.out.print("Card UID = 0x");
                for(int i = 0; i < baCardUid.length; i++ ){
                    System.out.printf("%02X ", baCardUid [i]);
                }
            }

        card.disconnect(false);

        } catch (CardException e) {
            e.printStackTrace();
        }
    }

}

我正在使用eclipse IDE在Mac机器上进行开发。当我运行此代码时,它给了我异常,因为它无法读取终端。我有一个USB读卡器,还插入了智能卡。 你能指出我哪里错了。 提前致谢。

1 个答案:

答案 0 :(得分:2)

它可能与您的问题无关,但是在使用64位版本的java7的Mac OS X上,软件包javax.smartcardio似乎被严重破坏了。您可以在此blog post和此bug report中找到更多相关信息。您还可以查看试图解决javax.smartcardio包问题的开源项目jnasmartcardio