'java/smart card'에 해당되는 글 3건

  1. 2010.07.16 [wipi 2.0] - smart card i/o 하자
  2. 2010.06.29 [smart card] - smartcardio 사용하기(2) 3
  3. 2010.06.29 [smart card] - smartcardio 사용하기(1)

wipi 2.0 표준 api 를 보면 IODevice 라는 넘이 존재한다. 이넘을 사용해서 smart card(usim) 에 read/write 할 수 있다.

connect
- IODevice 클래스를 사용해서 chip에 connect 하고 ATR 값을 읽어온다.
/**
     * Connect 하면 ATR 값을 Chip에서 읽을 수 있다.
     *
     * @throws IOException
     */
    public static void connect() throws IOException {       
        byte bAtr[] = Util.byteSet(50, (byte)0x20);
       
        if(iod == null) {
            try {
                iod = new IODevice("1ChipCard", 0, bAtr);
            } catch (IOException e) {
                close();
                System.out.println("[OpCard Connect Exception] - " + e);   
                throw e;
            } catch (Exception e) {
                close();
                System.out.println("[OpCard Connect Exception] - " + e);   
                throw new IOException("Conect Error!");
            }
            atr = Util.byteArr2HexaStr(Util.byteTrim(bAtr));   
        }       
       
        System.out.println("[Debug] - OpCard Connect Success!"); 
        System.out.println("[Debug] ATR - " + atr); 
    }       

apdu send
- chip에 apdu 명령을 보낸다.
/**
     *
     * @param cmd (cls, ins, p1, p2, lc , data, le)
     * @return
     * @throws IOException
     */
    public static String send(byte[] cmd) throws IOException {       
        String szBuf = null;
        int ret = 0;
        byte buf[] = new byte[255];
       
        if(iod != null) {
            try {
                iod.write(cmd);
                ret = iod.read(buf);                       
            } catch (IOException e) {
                close();
                System.out.println("[OpCard send Exception] - " + e);   
                throw e;
            } catch (Exception e) {
                close();
                System.out.println("[OpCard send Exception] - " + e);   
                throw new IOException("Send Error!");
            }
        }
       
        szBuf = Util.toString(buf, 0, ret);  // byte -> Hex String 변환
        System.out.println("[Debug] ret - " + ret); 
        System.out.println("[Debug] buf - " + szBuf); 
       
        return szBuf.trim();
    }

close
- connection 종료
public static void close() {       
       
        if (iod != null) {
            try {
                iod.close();
                iod = null;
            } catch (IOException e) {
                iod = null;
                System.out.println("[OpCard Close Exception] - " + e);   
            } catch (Exception e) {
                iod = null;
                System.out.println("[OpCard close Exception] - " + e);   
            }
        }    
        System.out.println("[Debug] - OpCard Close Success!"); 
    }

흠... 위 코드는 SKT wipi 폰에서 테스트 됨.
KT는 뭐.. 공개하면 안될듯 ㅋ


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

[smart card] - smartcardio 사용하기(2)  (3) 2010.06.29
[smart card] - smartcardio 사용하기(1)  (0) 2010.06.29
Posted by 짱가쟁이

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 짱가쟁이
현재까지 windows에서 java로 Smart Card I/O 하는 방법을 찾은건 2가지가 있다.

1. OpenCard Framwork(http://www.openscdp.org/ocf/)
    - 사용하기 귀찮고, 따로 api를 추가 시켜줘야함.

2. smartcardio 사용
    - OCF보다 사용하기 편하다.
    - jdk 1.6 버전  에서는 기본 API 제공한다.
    - javax.smartcardio.*

목표
    - javax.smartcardio.* API를 사용해서 Smart Card APDU Test Tool을 제작해보자.


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

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