1. 연결된 리더기 목록 가져오기
// TerminalFactory 인스턴스 얻기.
TerminalFactory terminalFactory = TerminalFactory.getInstance("PC/SC", null);

// 사용 가능한 CardTerminal  가져오기
List<CardTerminal> list = terminalFactory.terminals().list();

public String[] getReaderList() throws CardException {
 
    List<CardTerminal> list = terminalFactory.terminals().list();
    String[] readerList    = new String[list.size()];
   
    for(int i = 0;i<list.size();i++) {
     readerList[i] = ((CardTerminal)list.get(i)).getName();
    }
   
    return readerList;
}

2. 리더기에 Connect 하기
// 연결할 카드 터미널(스마트카드 리더) 가져오기
CardTerminal cardTerminal = terminalFactory.terminals().getTerminal("리더기 이름을 넣는다.");

// 프로토콜 설정하기
Card card = cardTerminal.connect("*");

// 채널 가져오기
CardChannel cardChannel = card.getBasicChannel();

public boolean connect(String readerName) {
 
    try {
        CardTerminal cardTerminal = terminalFactory.terminals().getTerminal(readerName);
       
        card = cardTerminal.connect("*");
        cardChannel = card.getBasicChannel();
       
        return true;
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
}

3. 연결 끊기
public void disConnect() {
    try { 
        card.disconnect(true);       
        cardChannel = null;
        card   = null;
    } catch(Exception e) {
        e.printStackTrace();
    }
}

4. ATR 가져오기
// 접속된 카드 객체에서 ATR을 가져옴
card.getATR();

5. APDU 전송하기
public ResponseAPDU apdu(CommandAPDU cmd) {
    try {
        return cardChannel.transmit(cmd);
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

SCard.java
package com.scard;

import java.security.NoSuchAlgorithmException;
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.CardTerminals;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;

public class SCard {

 private TerminalFactory terminalFactory;
 private Card    card   = null;
 private CardChannel  cardChannel = null;
 
 public SCard() throws NullPointerException, NoSuchAlgorithmException {
  terminalFactory = TerminalFactory.getInstance("PC/SC", null);
 }
 
 public String[] getReaderList() throws CardException {
 
  List<CardTerminal> list = terminalFactory.terminals().list();
 
  String[] readerList    = new String[list.size()];
 
  for(int i = 0;i<list.size();i++) {
   readerList[i] = ((CardTerminal)list.get(i)).getName();
  }

  return readerList;
 }
 
 public boolean connect(String readerName) {
 
  try {
   CardTerminal cardTerminal = terminalFactory.terminals().getTerminal(readerName);

   card = cardTerminal.connect("*");
   cardChannel = card.getBasicChannel();
  
   return true;
  } catch(Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 public ATR getATR() {
  return card.getATR();
 }
 
 public void disConnect() {
  try { 
   card.disconnect(true);
  
   cardChannel = null;
   card   = null;
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
 
 public ResponseAPDU apdu(CommandAPDU cmd) {
 
  try {
   return cardChannel.transmit(cmd);
  } catch(Exception e) {
   e.printStackTrace();
   return null;
  }
 }
}

테스트
SCard sCard = new SCard();
 
// 리더기 목록 가져오기
String[] readers = sCard.getReaderList();

// 리더기에 연결하기
sCard.connect(readers[0]);

// 명령 APDU 생성하기
CommandAPDU commandApdu = new CommandAPDU((byte)0x00, (byte)0xA4, (byte)0x04, (byte)0x00, StringUtils.unHex("A0000000030000"), 0, (byte)0x07);

// APDU 전송하기
ResponseAPDU responseApdu = sCard.apdu(commandApdu);

// responseApdu : 잘 찾아보면 응닶값이랑.. sw가 있을거임..

'java > smart card' 카테고리의 다른 글

[wipi 2.0] - smart card i/o 하자  (0) 2010.07.16
[smart card] - smartcardio 사용하기(1)  (0) 2010.06.29
Posted by 짱가쟁이