jdk1.6 java语言 连接pcsc读卡器 发送APDU指令

java语言操作读卡器  jdk1.6 提供的Package javax.smartcardio可以直接连接pcsc读卡器、和读卡器进行指令交互

Package javax.smartcardio详细信息

 

java语言代码实例

 /**
  *connectpcsc.java

  */

import java.util.List;

import javax.smartcardio.CardTerminal;
import javax.smartcardio.TerminalFactory;


public class connectpcsc {
 /**
  * main function, listing all the PC/SC readers connected to your PC
  */
 public static void main(String[] args) {

  // show the list of available terminals
  TerminalFactory factory = TerminalFactory.getDefault();
  
  // list of readers (empty)
  List<CardTerminal> terminals;
  try {
   // get list of readers form the terminal
   terminals = factory.terminals().list();
   
   // print list of readers to the console.
   System.out.println(terminals.toString());

  } catch (Exception e) {
   // Print Stack-Trace in case of an error
   e.printStackTrace();
  }
 }
}

 

import java.util.List;

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 GetStatus {
 public static void main(String[] args) {
 TerminalFactory factory = TerminalFactory.getDefault();
   try {
    List<CardTerminal> terminal = factory.terminals().list();
    System.out.println("terminal:" + terminal);
  
   CardTerminal ter = terminal.get(0);
  
         // establish a connection with the card
         Card card = ter.connect("T=0");
         System.out.println("card: " + card);
         CardChannel channel = card.getBasicChannel();
        
         ResponseAPDU r = channel.transmit(new CommandAPDU(160, 242, 0, 0, 22)); //A0 F2 00 00 16    
         System.out.println("response: " + r.toString());         
         for(int j=0; j<8; j++) {
          System.out.print(Integer.toHexString( (int)((r.getData()[j]+256) % 256)) + " ");
         }

         System.out.print("\n");

         for(int j=0; j<card.getATR().getBytes().length; j++) {
          System.out.print(Integer.toHexString( (int)((card.getATR().getBytes()[j]+256) % 256)) + " ");
         }

         // disconnect
         card.disconnect(false);
   } catch (CardException e) {
    // TODO Auto-generated catch block
    System.out.println("connection erro,or card not inserted.");
   }
 }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。