javax.smartcardio读取所有智能卡

javax.smartcardio读取所有智能卡,java,smartcard,apdu,emv,Java,Smartcard,Apdu,Emv,我必须阅读万事达卡、VISA卡、VISA ELECTRON等的主要账号(PAN)。我写了这个代码,但它不起作用。有人能帮我吗 import java.util.List; import javax.smartcardio.ATR; import javax.smartcardio.Card; import javax.smartcardio.CardChannel; import javax.smartcardio.CardException; import javax.smartcardio

我必须阅读万事达卡、VISA卡、VISA ELECTRON等的主要账号(PAN)。我写了这个代码,但它不起作用。有人能帮我吗

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 LetturaSmartCard {
    public static void main(String[] args) throws CardException {

        // stampo lista lettori 
        TerminalFactory factory = TerminalFactory.getDefault();
        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Lista lettori smart card collegati: " + terminals);

        // prendo il primo lettore
        CardTerminal terminal = terminals.get(0);

        System.out.println("Lettore utilizzato: " + terminal);
        //stabilisco connessione con la carta

        Card card = terminal.connect("*");
        // System.out.println(card.getProtocol());
        System.out.println("\n\nCarta inserita: " + card);
        CardChannel channel = card.getBasicChannel();


        ATR atr = card.getATR();
        byte[] ATR = atr.getBytes();
        byte[] TuttaCarta;
        System.out.println("ATR della carta: " + LetturaSmartCard.bytesToHex(ATR));


        //------------------------------------lettura---------------------------

        byte[] selectVpay = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x20, (byte) 0x20, (byte) 0x00};
        byte[] selectMastercard = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x10, (byte) 0x10, (byte) 0x00};
        byte[] selectVisaElectron = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x20, (byte) 0x10, (byte) 0x00};
        byte[] bo = {(byte) 0x00, (byte) 0xB2, (byte) 0x00, (byte) 0x03, (byte) 0x60};
        byte[] selectMaestro = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x30, (byte) 0x60, (byte) 0x00};
        byte[] getProcessingOptions = {(byte) 0x80, (byte) 0xA8, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x83, (byte) 0x00, (byte) 0x00};
        byte[] readRecord = {(byte) 0x00, (byte) 0xB2, (byte) 0x01, (byte) 0x0C, (byte) 0x00};
        byte[] lettura = {(byte) 0x00, (byte) 0xB0, (byte) 0x60, (byte) 0x10, (byte) 0x00};
        ResponseAPDU r = null;


        CommandAPDU capdu = new CommandAPDU(selectMastercard);
        r = card.getBasicChannel().transmit(capdu);
        TuttaCarta = r.getBytes();
        System.out.println(LetturaSmartCard.bytesToHex(TuttaCarta));

        capdu = new CommandAPDU(getProcessingOptions);
        r = card.getBasicChannel().transmit(capdu);
        TuttaCarta = r.getBytes();
        System.out.println(LetturaSmartCard.bytesToHex(TuttaCarta));


        capdu = new CommandAPDU(readRecord);
        r = card.getBasicChannel().transmit(capdu);
        TuttaCarta = r.getBytes();
        System.out.println(LetturaSmartCard.bytesToHex(TuttaCarta));


        //----------------------------------------------------------------------


        // disconnect
        card.disconnect(false);


    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02x", bytes[i]));
        }

        return sb.toString();
    }

}
6985
是我在预期PAN时收到的错误。
我做错了什么?

此错误意味着卡上没有此类记录。使用READ RECORD命令

00 B2 01 0C 00
您正在尝试使用SFI 1读取文件的记录1。但是,您在响应GET PROCESSION OPTIONS命令时收到的应用程序文件定位器明确指出,卡上只有以下记录可用:

10 02 03 01: records 2 and 3 in the file with SFI 2
18 01 02 00: records 1 and 2 in the file with SFI 3
18 04 04 00: record 4 in the file with SFI 3
因此,您不能期望卡上存在SFI为1的文件中的记录1。对于该卡,您通常可以在SFI 2记录2中找到PAN:

00 B2 02 14 00
还请注意,您使用的静态编码GPO命令仅适用于PDOL为空的卡

00 B2 02 14 00