java操作智能卡 IC卡 连接pcsc读卡器 发送APDU指令 源代码 下载 jdk1.6满足要求 java语言 操作IC卡 操作智能卡

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

Package javax.smartcardio详细信息

http://docs.oracle.com/javase/6/docs/jre/api/security/smartcardio/spec/javax/smartcardio/package-summary.html

 

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


连接读卡器和卡片,发送APDU指令

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.");
   }
 }
}


 

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值