2014-05-02 3 views
2

Je dois lire le numéro de compte principal (PAN) de MASTERCARD, VISA, VISA ELECTRON etc. Je worte ce code mais cela ne fonctionne pas quelqu'un peut m'aider?javax.smartcardio lire toutes les cartes à puce

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

} 

Je ne sais pas pourquoi cela ne fonctionne pas mes réponses sont

Lista lettori smart card collegati: [PC/SC terminal OMNIKEY CardMan 3x21 0] 
Lettore utilizzato: PC/SC terminal OMNIKEY CardMan 3x21 0 

Carta inserita: PC/SC card in OMNIKEY CardMan 3x21 0, protocol T=0, state OK 
ATR della carta: 3b6800000073c84013009000 
6f2d8407a0000000041010a522500a4d4153544552434152448701015f2d086974656e66726573bf0c059f4d020b0a9000 
771282023900940c1002030118010200180404009000 
**6985** 

6985 est une erreur que je reçois quand j'attendre le PAN. Qu'est-ce que je fais de mal?

Répondre

1

Cette erreur signifie qu'il n'existe aucun enregistrement de ce type sur la carte. Avec votre commande READ RECORD

00 B2 01 0C 00 

vous essayez de lire l'enregistrement 1 du fichier avec le SFI 1. Toutefois, le localisateur de fichier d'application que vous avez reçu en réponse à la commande GET OPTIONS DE TRAITEMENT indique clairement que seuls les documents suivants sont disponibles sur la carte:

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 

vous ne pouvez donc pas attendre un record 1 dans un fichier avec 1 à être SFI présent sur la carte. Pour cette carte, vous pouvez vous attendre généralement à trouver le PAN dans 2 SFI fiche 2:

00 B2 02 14 00 

Notez également que la commande GPO statiquement code que vous utilisez ne fonctionne que pour de la carte avec un PDOL vide.

Questions connexes