本文介绍了Java智能卡 - 读Scosta智能卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Java读取印度政府标准Scosta智能卡 smartcardio
在code我现在用的就是

I am trying to read Indian Governments Standard 'Scosta' smart card through java smartcardiothe code I am using is

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

}

我使用在Mac机开发Eclipse IDE中。当我运行这个code,它给了我例外,因为它不能够读取终端。我有一个USB读卡器和也插入智能卡插入它。
你能指出确切的地方我是走错了。
提前致谢。

I am using eclipse IDE for development in Mac machine. When I run this code, it gives me exception as it is not able to read Terminals. I have got a USB Card Reader and have also inserted smart card into it.Could you please point out where exactly am I going wrong.Thanks in advance.

推荐答案

这可能是无关的问题,但包javax.smartcardio似乎被严重损坏在Mac OS X与java7目录的64位版本。你可以找到在这个更多的信息,这个 bug报告。您还可以看看开源项目试图解决javax.smartcardio包的问题。

It may be unrelated to your problem but package javax.smartcardio seems to be seriously broken on Mac OS X with 64-bit version of java7. You can find more information in this blog post and this bug report. You can also take a look at open source project jnasmartcardio that tries to solve the issues of javax.smartcardio package.

这篇关于Java智能卡 - 读Scosta智能卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:02