Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Odczytanie informacji z Elektronicznej Legitymacji Studenckiej
- * Autor: Lukasz Kulasek (http://lukaszkulasek.pl)
- *
- * Politechnika Gdanska
- * Wydzial Elektroniki, Telekomunikacji i Informatyki
- * Projekt Grupowy, sem. 9
- *
- * Wersja: 18.01.2009
- */
- package karta;
- import java.io.*;
- import java.util.*;
- import javax.smartcardio.*;
- import java.util.List;
- import java.awt.event.*;
- import java.sql.*;
- public class Karta {
- public static void main(String[] args) throws Exception {
- boolean success, running = true;
- boolean useDB = true; // czy ma zapisywac do bazy danych
- DB dataBase = null;
- String ip = "?";
- // pobiera IP komputera
- try {
- java.net.InetAddress i = java.net.InetAddress.getLocalHost();
- ip = i.getHostAddress();
- } catch(Exception e) {
- e.printStackTrace();
- }
- /*
- if (useDB == true) {
- dataBase = new DB();
- boolean succ = dataBase.connect("localhost", "root", "", "els_devel");
- if (!(succ)) {
- if (useDB) {
- useDB = false;
- System.out.println();
- System.out.println("Blad polaczenia z baza danych.");
- System.out.println();
- System.out.println();
- }
- }
- }
- */
- // odczytanie ELS
- ReadELS ELS = new ReadELS();
- do {
- success = ELS.readData(false); // odczytuje dane z karty i przechowuje je
- if (success) {
- String[] dane_karty;
- String[] etykiety = new String[9];
- etykiety[0] = "Numer seryjny ukladu";
- etykiety[1] = "Uczelnia ";
- etykiety[2] = "Nazwisko studenta ";
- etykiety[3] = "Imiona studenta ";
- etykiety[4] = "Numer indeksu ";
- etykiety[5] = "Numer edycji ";
- etykiety[6] = "PESEL ";
- etykiety[7] = "Data waznosci ELS ";
- etykiety[8] = "Obywatelstwo ";
- dane_karty = ELS.getPersonalData(); // odczytuje sformatowane dane studenta
- for (int i=0; i<9; i++) {
- System.out.println(etykiety[i]+" : "+dane_karty[i]);
- }
- /*
- if (useDB == true) {
- // zapis do bazy danych...
- String query = "INSERT INTO log (indeks, ip) VALUES ('"+dane_karty[4]+"','"+ip+"')";
- System.out.println();
- if (dataBase.updateQuery(query)) {
- System.out.println("Zapisano do bazy danych.");
- }
- }*/
- // oczekuje na wyciagniecie karty.
- // Nastepnie rozpocznie ponownie caly proces.
- CheckCardPresent isPresent;
- boolean isCardPresent = true;
- do {
- Thread.sleep(1000);
- try {
- isPresent = new CheckCardPresent();
- isCardPresent = isPresent.get();
- } catch (CardException e) {
- System.out.println("BLAD KRYTYCZNY!");
- System.out.println(e.toString());
- }
- } while (isCardPresent == true);
- System.out.println();
- System.out.println("Wyciagnieto karte...");
- System.out.println();
- } else {
- if (ELS.wasCardNotInserted()) {
- running = false;
- } else if (ELS.wasCardInsertedBad() == true) {
- // nieznany blad... konczenie programu.
- System.out.println("Wystapil blad odczytania danych z legitymacji...");
- running = false;
- } else if (ELS.wasCardInsertedBad() == false) {
- Thread.sleep(1000);
- }
- }
- } while (running);
- /*if (useDB == true) {
- dataBase.closeConnection();
- }*/
- System.out.println("Program zakonczony.");
- System.out.println();
- System.out.println();
- System.out.println();
- }
- }
- class DB {
- Connection con = null;
- DB() {
- }
- public boolean connect(String DB_HOST, String DB_USER, String DB_PASS, String DB_NAME) {
- boolean success = true;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://"+DB_HOST+"/"+DB_NAME, DB_USER, DB_PASS);
- if(!con.isClosed()) {
- /*
- */
- } else {
- success = false;
- }
- } catch(Exception e) {
- success = false;
- System.err.println("(DB.connect()) Exception: " + e.getMessage());
- }
- return success;
- }
- public boolean updateQuery(String query) {
- boolean success = true;
- try {
- Statement st = con.createStatement();
- ResultSet rs = null;
- st.executeUpdate(query);
- } catch (Exception e) {
- success = false;
- System.err.println("(DB.updateQuery()) Exception: " + e.getMessage());
- }
- return success;
- }
- public boolean closeConnection() {
- boolean success = true;
- if(con != null) {
- try {
- con.close();
- } catch (Exception e) {
- success = false;
- System.err.println("(DB.closeConnection()) Exception: " + e.getMessage());
- }
- } else {
- success = false;
- }
- return success;
- }
- }
- class ReadELS {
- private static CardTerminal terminal = null;
- private static CardChannel channel;
- private static Card card;
- private byte[] atr;
- private String atr_hex;
- private boolean verbose = false;
- private String[] result;
- private int[] result_len;
- private boolean[] result_is_string;
- private String[] dane_karty;
- private boolean cardInsertedBad = false;
- private boolean cardNotInserted = false;
- private int waitForCardTime = 10; // Czas oczekiwania na karte w sekundach.
- // Po jego uplywie nastepuje zakonczenie programu.
- // Komenda APDU wybierajaca wezel glowny MF
- private static final byte[] CMD_SELECT_MF = {(byte)0x00, // CLA
- (byte)0xA4, // INS
- (byte)0x00, // P1
- (byte)0x0C, // P2
- (byte)0x02, // Lc
- (byte)0x3F, // FID 1 (high)
- (byte)0x00 }; // FID 2 (low)
- // Komenda APDU wybierajaca katalog DF (po nazwie)
- private static final byte[] CMD_SELECT_DF = {(byte)0x00, // CLA
- (byte)0xA4, // INS
- (byte)0x04, // P1
- (byte)0x04, // P2
- (byte)0x07, // Lc
- (byte)0xD6, // AID 1 (high)
- (byte)0x16, // AID 2
- (byte)0x00, // AID 3
- (byte)0x00, // AID 4
- (byte)0x30, // AID 5 (low)
- (byte)0x01, // PIX 1 (high)
- (byte)0x01 }; // PIX 2 (low)
- // Komenda APDU wybierajaca plik EF o FID 0x0002 z katalogu DF
- private static final byte[] CMD_SELECT_EF0002 = {(byte)0x00, // CLA
- (byte)0xA4, // INS
- (byte)0x00, // P1
- (byte)0x0C, // P2
- (byte)0x02, // Lc
- (byte)0x00, // FID 1 (high)
- (byte)0x02 }; // FID 2 (low)
- ReadELS() throws Exception {
- }
- public boolean readData(boolean verbose) throws Exception {
- boolean success = true;
- this.verbose = verbose;
- success = initCard();
- if (success) {
- success = setEF0002();
- }
- if (success) {
- success = readEFData();
- }
- if (success) {
- success = disconnect();
- }
- return success;
- }
- private boolean initCard() throws Exception {
- boolean success = true;
- // wyswietla liste dostepnych terminali
- TerminalFactory factory = TerminalFactory.getDefault();
- List terminalList = null;
- try {
- terminalList = factory.terminals().list();
- } catch (CardException e) {
- System.out.println("Brak podlaczonych terminali...");
- success = false;
- }
- if (success) {
- if (verbose == true) {
- // Lista numerow czytnikow kart
- for (int i=0; i < terminalList.size(); i++){
- System.out.println("Czytnik kart nr " + i + " " + terminalList.get(i));
- }
- }
- // korzysta z pierwszego terminala z listy
- terminal = (CardTerminal) terminalList.get(0);
- boolean oczekiwano = false;
- if (terminal.isCardPresent() == false) {
- success = false; oczekiwano = true; boolean running = true;
- int loopCount = 0;
- System.out.print("Oczekiwanie na karte... ");
- do {
- try {
- terminal.waitForCardPresent(1000);
- } catch (CardNotPresentException e) {
- // ciagle nie ma karty...
- } catch (Exception e) {
- System.out.println("BLAD KRYTYCZNY... "+e.toString());
- }
- loopCount++;
- if (loopCount > waitForCardTime) {
- // czekano 10 sekund. Konczenie programu.
- System.out.println();
- System.out.println();
- System.out.println("Nie wlozono karty.");
- running = false;
- success = false;
- cardNotInserted = true;
- } else {
- System.out.print(".");
- }
- } while ((terminal.isCardPresent() == false) && (running == true));
- System.out.println();
- System.out.println();
- if (cardNotInserted == false) {
- success = true;
- }
- }
- if (cardNotInserted == false) {
- if ((success == true) && (oczekiwano == true)) {
- System.out.println("Karta wlozona.");
- System.out.println();
- }
- // ustanawia polaczenie z karta
- try {
- card = terminal.connect("T=1");
- } catch (CardException e) {
- System.out.println("Wloz poprawnie karte... ");
- System.out.println();
- success = false; cardInsertedBad = true;
- }
- }
- if (success) {
- if (verbose == true) {
- System.out.println("card: " + card);
- }
- channel = card.getBasicChannel();
- //resetuje karte; pobiera ATR;
- if (verbose == true) {
- System.out.print("ATR: ");
- }
- atr = card.getATR().getBytes();
- this.atr_hex = "";
- String s = new String(); int n, x;
- for (n = 0; n < atr.length; n++) {
- x = (int) (0x000000FF & atr[n]); // byte to int conversion
- s = Integer.toHexString(x).toUpperCase();
- if (s.length() == 1) s = "0" + s;
- if (verbose == true) {
- System.out.print(s + " ");
- }
- this.atr_hex += s + " ";
- } // for
- if (verbose == true) {
- System.out.println();
- System.out.println();
- }
- }
- }
- return success;
- }
- private boolean setEF0002() throws Exception {
- boolean success = false;
- CommandAPDU apdu_comm = new CommandAPDU(CMD_SELECT_MF);
- ResponseAPDU response = channel.transmit(apdu_comm);
- if (check9000(response)) {
- CommandAPDU apdu_comm5 = new CommandAPDU(CMD_SELECT_DF);
- ResponseAPDU response5 = channel.transmit(apdu_comm5);
- if (check9000(response5)) {
- CommandAPDU apdu_comm4 = new CommandAPDU(CMD_SELECT_EF0002);
- ResponseAPDU response4 = channel.transmit(apdu_comm4);
- if (check9000(response5)) {
- success = true;
- }
- }
- }
- if (!success) {
- System.out.println("BLAD KRYTYCZNY otworzenia pliku EF.ELS do odczytania !!!");
- }
- return success;
- }
- private boolean readEFData() throws Exception {
- boolean success = true;
- byte[] read_comm;
- CommandAPDU apdu_comm3;
- ResponseAPDU response3;
- byte[] dane = new byte[256];
- int offset = 0;
- String res = "";
- int lp = 0;
- read_comm = new byte[5]; int i;
- result = new String[50]; // parsuje rekordy do stringa
- result_len = new int[50]; // odcztuje dlugosci stringow
- result_is_string = new boolean[50]; // zapisuje, czy rekord to string (0x0C), czy int (0x13)
- int nr_stringu = 0;
- // odczytane 2x danych po 256 bajtow
- for (int offs = 0; offs<2; offs++) {
- read_comm[0] = (byte)0x00; // CLA
- read_comm[1] = (byte)0xB0; // INS
- read_comm[2] = (byte)(((offset) >> 8) & 0xFF); // P1
- read_comm[3] = (byte)((offset) & 0xFF); // P2
- read_comm[4] = (byte)0xFF; // Lc
- // wysylanie komendy czytania 256 bajtow...
- apdu_comm3 = new CommandAPDU(read_comm);
- response3 = channel.transmit(apdu_comm3);
- byte[] tmp_dane = response3.getBytes();
- boolean last_byte_control = false, hex_0x0C = false;
- for (i=0; i<tmp_dane.length-2; i++) {
- int code = (int)tmp_dane[i];
- if (code > 0) {
- lp++;
- char c = (char)tmp_dane[i];
- if (verbose == true)
- System.out.print(lp+": "+byteToHex(tmp_dane[i])+": ");
- if ((code>31) && (code < 256)) {
- if (verbose == true)
- System.out.print(c);
- if (hex_0x0C == false) {
- result[nr_stringu] += c;
- }
- }
- if (hex_0x0C == true) {
- result_len[nr_stringu] = code;
- hex_0x0C = false;
- }
- if (((code == (int)0x0C) || (code == (int)0x13) || (code == (int)0x18)) && (last_byte_control == false)) {
- last_byte_control = true; // zeby nie traktowal dlugosci danych w rekordzie jako bajtu kontrolnego
- nr_stringu++;
- if (code == (int)0x0C) {
- result_is_string[nr_stringu] = true;
- } else {
- result_is_string[nr_stringu] = false;
- }
- hex_0x0C = true;
- } else {
- last_byte_control = false;
- }
- if (verbose == true)
- System.out.println();
- }
- }
- offset += 64;
- }
- if (verbose) {
- System.out.println();
- }
- // formatowanie odczytanego strumienia bajtow
- int size, nr_danych=0;
- dane_karty = new String[9];
- String form_data;
- for (i = 0; i<result.length; i++) {
- if (result[i] != null) {
- size = 4+result_len[i];
- if (size > result[i].length()) {
- size = result[i].length();
- }
- form_data = result[i].substring(4,size);
- if ((i>0) && (nr_danych < 9)) {
- if (i != 5) {
- dane_karty[nr_danych] = form_data;
- nr_danych++;
- } else if ((i == 5) && (result_is_string[i] == true)) { // drugie imie
- dane_karty[nr_danych-1] += " "+form_data;
- } else {
- dane_karty[nr_danych] = form_data;
- nr_danych++;
- }
- }
- }
- }
- // sformatowanie daty waznosci karty na RRRR-MM-DD
- dane_karty[7] = dane_karty[7].substring(0,4)+"-"+dane_karty[7].substring(4,6)+"-"+dane_karty[7].substring(6,8);
- return success;
- }
- public String[] getPersonalData() {
- return dane_karty;
- }
- private boolean disconnect() throws Exception {
- boolean success = true;
- card.disconnect(false);
- return success;
- }
- public static boolean check9000(ResponseAPDU ra) {
- byte[] response = ra.getBytes();
- if (response[response.length - 2] == (byte) 0x90 && response[response.length - 1] == (byte) 0x00) {
- return (true);
- } else {
- return (false);
- }
- }
- private String byteToHex(byte b){
- int i = b & 0xFF;
- return Integer.toHexString(i);
- }
- public byte[] getATR() {
- return atr;
- }
- public String getHexATR() {
- return atr_hex;
- }
- public boolean wasCardInsertedBad() {
- return cardInsertedBad;
- }
- public boolean wasCardNotInserted() {
- return cardNotInserted;
- }
- }
- class CheckCardPresent {
- private static CardTerminal terminal = null;
- private static CardChannel channel;
- private static Card card;
- private boolean cardPresent;
- CheckCardPresent() throws Exception {
- boolean success = true;
- cardPresent = true;
- TerminalFactory factory = TerminalFactory.getDefault();
- List terminalList = null;
- try {
- terminalList = factory.terminals().list();
- } catch (CardException e) {
- cardPresent = false;
- success = false;
- }
- if (success) {
- // korzysta z pierwszego terminala z listy
- terminal = (CardTerminal) terminalList.get(0);
- cardPresent = terminal.isCardPresent();
- }
- }
- public boolean get() {
- return cardPresent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement