Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.43 KB | None | 0 0
  1. /*
  2.  
  3.  *  Odczytanie informacji z Elektronicznej Legitymacji Studenckiej
  4.  
  5.  *  Autor: Lukasz Kulasek (http://lukaszkulasek.pl)
  6.  
  7.  *
  8.  
  9.  *  Politechnika Gdanska
  10.  
  11.  *  Wydzial Elektroniki, Telekomunikacji i Informatyki
  12.  
  13.  *  Projekt Grupowy, sem. 9
  14.  
  15.  *
  16.  
  17.  *  Wersja: 18.01.2009
  18.  
  19.  */
  20.  
  21. package karta;
  22.  
  23. import java.io.*;
  24.  
  25. import java.util.*;
  26.  
  27. import javax.smartcardio.*;
  28.  
  29. import java.util.List;
  30.  
  31. import java.awt.event.*;
  32.  
  33. import java.sql.*;
  34.  
  35.  
  36.  
  37. public class Karta {
  38.  
  39.  
  40.  
  41.     public static void main(String[] args) throws Exception {  
  42.  
  43.         boolean success, running = true;
  44.  
  45.         boolean useDB = true; // czy ma zapisywac do bazy danych
  46.  
  47.         DB dataBase = null;
  48.  
  49.         String ip = "?";
  50.  
  51.        
  52.  
  53.         // pobiera IP komputera
  54.  
  55.         try {
  56.  
  57.             java.net.InetAddress i = java.net.InetAddress.getLocalHost();
  58.  
  59.             ip = i.getHostAddress();
  60.  
  61.         } catch(Exception e) {
  62.  
  63.             e.printStackTrace();
  64.  
  65.         }
  66.  
  67.  
  68.        
  69.         /*
  70.         if (useDB == true) {
  71.  
  72.                
  73.                 dataBase = new DB();
  74.  
  75.                 boolean succ = dataBase.connect("localhost", "root", "", "els_devel");
  76.  
  77.                 if (!(succ)) {
  78.  
  79.                     if (useDB) {
  80.  
  81.                         useDB = false;
  82.  
  83.                         System.out.println();
  84.  
  85.                         System.out.println("Blad polaczenia z baza danych.");
  86.  
  87.                         System.out.println();
  88.  
  89.                         System.out.println();
  90.  
  91.                     }
  92.  
  93.                 }
  94.  
  95.         }
  96. */
  97.        
  98.  
  99.         // odczytanie ELS
  100.  
  101.         ReadELS ELS = new ReadELS();
  102.  
  103.        
  104.  
  105.         do {
  106.  
  107.             success = ELS.readData(false); // odczytuje dane z karty i przechowuje je
  108.  
  109.            
  110.  
  111.             if (success) {
  112.  
  113.                 String[] dane_karty;
  114.  
  115.                 String[] etykiety = new String[9];
  116.  
  117.                
  118.  
  119.                 etykiety[0] = "Numer seryjny ukladu";
  120.  
  121.                 etykiety[1] = "Uczelnia            ";
  122.  
  123.                 etykiety[2] = "Nazwisko studenta   ";
  124.  
  125.                 etykiety[3] = "Imiona studenta     ";
  126.  
  127.                 etykiety[4] = "Numer indeksu       ";
  128.  
  129.                 etykiety[5] = "Numer edycji        ";
  130.  
  131.                 etykiety[6] = "PESEL               ";
  132.  
  133.                 etykiety[7] = "Data waznosci ELS   ";
  134.  
  135.                 etykiety[8] = "Obywatelstwo        ";
  136.  
  137.  
  138.  
  139.                 dane_karty = ELS.getPersonalData(); // odczytuje sformatowane dane studenta
  140.  
  141.                
  142.  
  143.                 for (int i=0; i<9; i++) {
  144.  
  145.                     System.out.println(etykiety[i]+" :  "+dane_karty[i]);
  146.  
  147.                 }
  148.  
  149.                 /*
  150.  
  151.                 if (useDB == true) {
  152.  
  153.                     // zapis do bazy danych...
  154.  
  155.                     String query = "INSERT INTO log (indeks, ip) VALUES ('"+dane_karty[4]+"','"+ip+"')";
  156.  
  157.                     System.out.println();
  158.  
  159.                     if (dataBase.updateQuery(query)) {
  160.  
  161.                         System.out.println("Zapisano do bazy danych.");
  162.  
  163.                     }
  164.  
  165.                 }*/
  166.  
  167.  
  168.  
  169.                 // oczekuje na wyciagniecie karty.
  170.  
  171.                 // Nastepnie rozpocznie ponownie caly proces.
  172.  
  173.                 CheckCardPresent isPresent;
  174.  
  175.                 boolean isCardPresent = true;
  176.  
  177.                
  178.  
  179.                 do {
  180.  
  181.                     Thread.sleep(1000);
  182.  
  183.                     try {
  184.  
  185.                         isPresent = new CheckCardPresent();
  186.  
  187.                         isCardPresent = isPresent.get();
  188.  
  189.                     } catch (CardException e) {
  190.  
  191.                         System.out.println("BLAD KRYTYCZNY!");
  192.  
  193.                         System.out.println(e.toString());
  194.  
  195.                     }
  196.  
  197.                 } while (isCardPresent == true);
  198.  
  199.  
  200.  
  201.                 System.out.println();
  202.  
  203.                 System.out.println("Wyciagnieto karte...");
  204.  
  205.                 System.out.println();
  206.  
  207.             } else {
  208.  
  209.                 if (ELS.wasCardNotInserted()) {
  210.  
  211.                     running = false;                   
  212.  
  213.                 } else if (ELS.wasCardInsertedBad() == true) {
  214.  
  215.                     // nieznany blad... konczenie programu.
  216.  
  217.                     System.out.println("Wystapil blad odczytania danych z legitymacji...");
  218.  
  219.                     running = false;
  220.  
  221.                 } else if (ELS.wasCardInsertedBad() == false) {
  222.  
  223.                     Thread.sleep(1000);
  224.  
  225.                 }
  226.  
  227.             }
  228.  
  229.  
  230.  
  231.         } while (running);
  232.  
  233.  
  234.  
  235.         /*if (useDB == true) {
  236.  
  237.             dataBase.closeConnection();
  238.  
  239.         }*/
  240.  
  241.        
  242.  
  243.         System.out.println("Program zakonczony.");
  244.  
  245.         System.out.println();
  246.  
  247.         System.out.println();
  248.  
  249.         System.out.println();
  250.  
  251.     }
  252.  
  253.  
  254.  
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263. class DB {
  264.  
  265.     Connection con = null;
  266.  
  267.  
  268.  
  269.     DB() {
  270.  
  271.     }
  272.  
  273.    
  274.  
  275.     public boolean connect(String DB_HOST, String DB_USER, String DB_PASS, String DB_NAME) {
  276.  
  277.         boolean success = true;
  278.  
  279.        
  280.  
  281.         try {
  282.  
  283.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  284.  
  285.             con = DriverManager.getConnection("jdbc:mysql://"+DB_HOST+"/"+DB_NAME, DB_USER, DB_PASS);
  286.  
  287.             if(!con.isClosed()) {  
  288.  
  289.  
  290.  
  291.                 /*             
  292.  
  293.                 */
  294.  
  295.                
  296.  
  297.             } else {
  298.  
  299.                 success = false;
  300.  
  301.             }
  302.  
  303.        
  304.  
  305.         } catch(Exception e) {
  306.  
  307.             success = false;
  308.  
  309.             System.err.println("(DB.connect()) Exception: " + e.getMessage());
  310.  
  311.         }      
  312.  
  313.  
  314.  
  315.         return success;
  316.  
  317.     }
  318.  
  319.    
  320.  
  321.     public boolean updateQuery(String query) {
  322.  
  323.         boolean success = true;
  324.  
  325.        
  326.  
  327.         try {
  328.  
  329.             Statement st = con.createStatement();
  330.  
  331.             ResultSet rs = null;
  332.  
  333.             st.executeUpdate(query);
  334.  
  335.         } catch (Exception e) {
  336.  
  337.             success = false;
  338.  
  339.             System.err.println("(DB.updateQuery()) Exception: " + e.getMessage());         
  340.  
  341.         }
  342.  
  343.        
  344.  
  345.         return success;
  346.  
  347.     }
  348.  
  349.    
  350.  
  351.     public boolean closeConnection() {
  352.  
  353.         boolean success = true;
  354.  
  355.  
  356.  
  357.         if(con != null) {
  358.  
  359.             try {
  360.  
  361.                 con.close();
  362.  
  363.             } catch (Exception e) {
  364.  
  365.                 success = false;
  366.  
  367.                 System.err.println("(DB.closeConnection()) Exception: " + e.getMessage());         
  368.  
  369.             }
  370.  
  371.         } else {
  372.  
  373.             success = false;
  374.  
  375.         }
  376.  
  377.  
  378.  
  379.         return success;
  380.  
  381.     }
  382.  
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391. class ReadELS {
  392.  
  393.     private static CardTerminal terminal = null;
  394.  
  395.     private static CardChannel channel;
  396.  
  397.     private static Card card;
  398.  
  399.     private byte[] atr;
  400.  
  401.     private String atr_hex;
  402.  
  403.    
  404.  
  405.     private boolean verbose = false;
  406.  
  407.     private String[] result;
  408.  
  409.     private int[] result_len;
  410.  
  411.     private boolean[] result_is_string;
  412.  
  413.     private String[] dane_karty;
  414.  
  415.    
  416.  
  417.     private boolean cardInsertedBad = false;
  418.  
  419.     private boolean cardNotInserted = false;
  420.  
  421.     private int waitForCardTime = 10;   // Czas oczekiwania na karte w sekundach.
  422.  
  423.                                         // Po jego uplywie nastepuje zakonczenie programu.
  424.  
  425.  
  426.  
  427.     // Komenda APDU wybierajaca wezel glowny MF
  428.  
  429.     private static final byte[] CMD_SELECT_MF = {(byte)0x00, // CLA
  430.  
  431.                                          (byte)0xA4, // INS
  432.  
  433.                                          (byte)0x00, // P1
  434.  
  435.                                          (byte)0x0C, // P2
  436.  
  437.                                          (byte)0x02, // Lc
  438.  
  439.                                          (byte)0x3F, // FID 1 (high)
  440.  
  441.                                          (byte)0x00 }; // FID 2 (low)
  442.  
  443.  
  444.  
  445.     // Komenda APDU wybierajaca katalog DF (po nazwie)
  446.  
  447.     private static final byte[] CMD_SELECT_DF = {(byte)0x00, // CLA
  448.  
  449.                                          (byte)0xA4, // INS
  450.  
  451.                                          (byte)0x04, // P1
  452.  
  453.                                          (byte)0x04, // P2
  454.  
  455.                                          (byte)0x07, // Lc
  456.  
  457.                                          (byte)0xD6, // AID 1 (high)
  458.  
  459.                                          (byte)0x16, // AID 2
  460.  
  461.                                          (byte)0x00, // AID 3
  462.  
  463.                                          (byte)0x00, // AID 4
  464.  
  465.                                          (byte)0x30, // AID 5 (low)
  466.  
  467.                                          (byte)0x01, // PIX 1 (high)
  468.  
  469.                                          (byte)0x01 }; // PIX 2 (low)
  470.  
  471.  
  472.  
  473.     // Komenda APDU wybierajaca plik EF o FID 0x0002 z katalogu DF
  474.  
  475.     private static final byte[] CMD_SELECT_EF0002 = {(byte)0x00, // CLA
  476.  
  477.                                              (byte)0xA4, // INS
  478.  
  479.                                              (byte)0x00, // P1
  480.  
  481.                                              (byte)0x0C, // P2
  482.  
  483.                                              (byte)0x02, // Lc
  484.  
  485.                                              (byte)0x00, // FID 1 (high)
  486.  
  487.                                              (byte)0x02 }; // FID 2 (low)
  488.  
  489.                                              
  490.  
  491.     ReadELS() throws Exception {
  492.  
  493.     }
  494.  
  495.  
  496.  
  497.     public boolean readData(boolean verbose) throws Exception {
  498.  
  499.         boolean success = true;
  500.  
  501.        
  502.  
  503.         this.verbose = verbose;
  504.  
  505.        
  506.  
  507.         success = initCard();
  508.  
  509.         if (success) {
  510.  
  511.             success = setEF0002();
  512.  
  513.         }
  514.  
  515.         if (success) {
  516.  
  517.             success = readEFData();
  518.  
  519.         }
  520.  
  521.         if (success) {
  522.  
  523.             success = disconnect();
  524.  
  525.         }
  526.  
  527.        
  528.  
  529.         return success;
  530.  
  531.     }
  532.  
  533.    
  534.  
  535.     private boolean initCard() throws Exception {
  536.  
  537.         boolean success = true;
  538.  
  539.         // wyswietla liste dostepnych terminali
  540.  
  541.         TerminalFactory factory = TerminalFactory.getDefault();
  542.  
  543.         List terminalList = null;
  544.  
  545.        
  546.  
  547.         try {
  548.  
  549.             terminalList = factory.terminals().list();
  550.  
  551.         } catch (CardException e) {
  552.  
  553.             System.out.println("Brak podlaczonych terminali...");
  554.  
  555.             success = false;
  556.  
  557.         }
  558.  
  559.        
  560.  
  561.         if (success) {
  562.  
  563.             if (verbose == true) {
  564.  
  565.                 // Lista numerow czytnikow kart
  566.  
  567.                 for (int i=0;  i < terminalList.size(); i++){
  568.  
  569.                     System.out.println("Czytnik kart nr " + i + " " + terminalList.get(i));
  570.  
  571.                 }
  572.  
  573.             }
  574.  
  575.          
  576.  
  577.             // korzysta z pierwszego terminala z listy
  578.  
  579.             terminal = (CardTerminal) terminalList.get(0);
  580.  
  581.        
  582.  
  583.             boolean oczekiwano = false;
  584.  
  585.             if (terminal.isCardPresent() == false) {
  586.  
  587.                 success = false; oczekiwano = true; boolean running = true;
  588.  
  589.                 int loopCount = 0;
  590.  
  591.                
  592.  
  593.                 System.out.print("Oczekiwanie na karte... ");
  594.  
  595.                
  596.  
  597.                 do {
  598.  
  599.                     try {          
  600.  
  601.                         terminal.waitForCardPresent(1000);
  602.  
  603.                     } catch (CardNotPresentException e) {
  604.  
  605.                         // ciagle nie ma karty...
  606.  
  607.                     } catch (Exception e) {
  608.  
  609.                         System.out.println("BLAD KRYTYCZNY... "+e.toString());
  610.  
  611.                     }
  612.  
  613.                     loopCount++;
  614.  
  615.                    
  616.  
  617.                     if (loopCount > waitForCardTime) {
  618.  
  619.                         // czekano 10 sekund. Konczenie programu.
  620.  
  621.                         System.out.println();
  622.  
  623.                         System.out.println();
  624.  
  625.                         System.out.println("Nie wlozono karty.");
  626.  
  627.                         running = false;
  628.  
  629.                         success = false;
  630.  
  631.                         cardNotInserted = true;
  632.  
  633.                     } else {
  634.  
  635.                         System.out.print(".");
  636.  
  637.                     }
  638.  
  639.                 } while ((terminal.isCardPresent() == false) && (running == true));
  640.  
  641.  
  642.  
  643.                 System.out.println();
  644.  
  645.                 System.out.println();
  646.  
  647.  
  648.  
  649.                 if (cardNotInserted == false) {
  650.  
  651.                     success = true;
  652.  
  653.                 }
  654.  
  655.             }
  656.  
  657.            
  658.  
  659.             if (cardNotInserted == false) {
  660.  
  661.                 if ((success == true) && (oczekiwano == true)) {
  662.  
  663.                     System.out.println("Karta wlozona.");
  664.  
  665.                     System.out.println();
  666.  
  667.                 }
  668.  
  669.            
  670.  
  671.                 // ustanawia polaczenie z karta
  672.  
  673.                 try {
  674.  
  675.                     card = terminal.connect("T=1");
  676.  
  677.                 } catch (CardException e) {
  678.  
  679.                     System.out.println("Wloz poprawnie karte... ");
  680.  
  681.                     System.out.println();
  682.  
  683.                     success = false; cardInsertedBad = true;
  684.  
  685.                 }
  686.  
  687.             }
  688.  
  689.            
  690.  
  691.             if (success) {
  692.  
  693.                 if (verbose == true) {
  694.  
  695.                     System.out.println("card: " + card);
  696.  
  697.                 }
  698.  
  699.                
  700.  
  701.                 channel = card.getBasicChannel();
  702.  
  703.              
  704.  
  705.                 //resetuje karte; pobiera ATR;
  706.  
  707.                 if (verbose == true) {
  708.  
  709.                     System.out.print("ATR:  ");
  710.  
  711.                 }
  712.  
  713.            
  714.  
  715.                 atr = card.getATR().getBytes();
  716.  
  717.            
  718.  
  719.                 this.atr_hex = "";
  720.  
  721.                 String s = new String(); int n, x;
  722.  
  723.                 for (n = 0; n < atr.length; n++) {
  724.  
  725.                     x = (int) (0x000000FF & atr[n]);  // byte to int conversion
  726.  
  727.                     s = Integer.toHexString(x).toUpperCase();
  728.  
  729.                     if (s.length() == 1) s = "0" + s;
  730.  
  731.                         if (verbose == true) {
  732.  
  733.                             System.out.print(s + " ");
  734.  
  735.                         }
  736.  
  737.                         this.atr_hex += s + " ";
  738.  
  739.                 } // for
  740.  
  741.                
  742.  
  743.                 if (verbose == true) {
  744.  
  745.                     System.out.println();
  746.  
  747.                     System.out.println();
  748.  
  749.                 }          
  750.  
  751.             }
  752.  
  753.         }
  754.  
  755.        
  756.  
  757.         return success;
  758.  
  759.     }
  760.  
  761.    
  762.  
  763.     private boolean setEF0002() throws Exception {
  764.  
  765.         boolean success = false;
  766.  
  767.        
  768.  
  769.         CommandAPDU apdu_comm = new CommandAPDU(CMD_SELECT_MF);
  770.  
  771.         ResponseAPDU response = channel.transmit(apdu_comm);
  772.  
  773.        
  774.  
  775.         if (check9000(response)) { 
  776.  
  777.             CommandAPDU apdu_comm5 = new CommandAPDU(CMD_SELECT_DF);
  778.  
  779.             ResponseAPDU response5 = channel.transmit(apdu_comm5);    
  780.  
  781.             if (check9000(response5)) {
  782.  
  783.                 CommandAPDU apdu_comm4 = new CommandAPDU(CMD_SELECT_EF0002);
  784.  
  785.                 ResponseAPDU response4 = channel.transmit(apdu_comm4);    
  786.  
  787.                 if (check9000(response5)) {
  788.  
  789.                     success = true;
  790.  
  791.                 }              
  792.  
  793.             }
  794.  
  795.         }
  796.  
  797.         if (!success) {
  798.  
  799.             System.out.println("BLAD KRYTYCZNY otworzenia pliku EF.ELS do odczytania !!!");
  800.  
  801.         }
  802.  
  803.         return success;
  804.  
  805.     }
  806.  
  807.    
  808.  
  809.     private boolean readEFData() throws Exception {
  810.  
  811.         boolean success = true;
  812.  
  813.  
  814.  
  815.         byte[] read_comm;
  816.  
  817.         CommandAPDU apdu_comm3;
  818.  
  819.         ResponseAPDU response3;
  820.  
  821.         byte[] dane = new byte[256];
  822.  
  823.         int offset = 0;
  824.  
  825.  
  826.  
  827.         String res = "";
  828.  
  829.        
  830.  
  831.         int lp = 0;
  832.  
  833.        
  834.  
  835.         read_comm = new byte[5]; int i;
  836.  
  837.        
  838.  
  839.         result = new String[50];  // parsuje rekordy do stringa
  840.  
  841.         result_len = new int[50];  // odcztuje dlugosci stringow
  842.  
  843.         result_is_string = new boolean[50];  // zapisuje, czy rekord to string (0x0C), czy int (0x13)
  844.  
  845.         int nr_stringu = 0;
  846.  
  847.        
  848.  
  849.         // odczytane 2x danych po 256 bajtow
  850.  
  851.         for (int offs = 0; offs<2; offs++) {
  852.  
  853.             read_comm[0] = (byte)0x00; // CLA
  854.  
  855.             read_comm[1] = (byte)0xB0; // INS
  856.  
  857.             read_comm[2] = (byte)(((offset) >> 8) & 0xFF); // P1
  858.  
  859.             read_comm[3] = (byte)((offset) & 0xFF); // P2
  860.  
  861.             read_comm[4] = (byte)0xFF; // Lc
  862.  
  863.  
  864.  
  865.             // wysylanie komendy czytania 256 bajtow...
  866.  
  867.             apdu_comm3 = new CommandAPDU(read_comm);
  868.  
  869.             response3 = channel.transmit(apdu_comm3);
  870.  
  871.        
  872.  
  873.             byte[] tmp_dane = response3.getBytes();
  874.  
  875.             boolean last_byte_control = false, hex_0x0C = false;
  876.  
  877.            
  878.  
  879.             for (i=0; i<tmp_dane.length-2; i++) {
  880.  
  881.  
  882.  
  883.                 int code = (int)tmp_dane[i];
  884.  
  885.                 if (code > 0) {
  886.  
  887.                     lp++;
  888.  
  889.                     char c = (char)tmp_dane[i];
  890.  
  891.                    
  892.  
  893.                     if (verbose == true)
  894.  
  895.                         System.out.print(lp+": "+byteToHex(tmp_dane[i])+":   ");
  896.  
  897.  
  898.  
  899.                     if ((code>31) && (code < 256)) {
  900.  
  901.                         if (verbose == true)
  902.  
  903.                             System.out.print(c);
  904.  
  905.                         if (hex_0x0C == false) {
  906.  
  907.                             result[nr_stringu] += c;
  908.  
  909.                         }
  910.  
  911.                     }
  912.  
  913.                    
  914.  
  915.                     if (hex_0x0C == true) {
  916.  
  917.                         result_len[nr_stringu] = code;
  918.  
  919.                         hex_0x0C = false;
  920.  
  921.                     }
  922.  
  923.                    
  924.  
  925.                     if (((code == (int)0x0C) || (code == (int)0x13) || (code == (int)0x18)) && (last_byte_control == false)) {
  926.  
  927.                         last_byte_control = true; // zeby nie traktowal dlugosci danych w rekordzie jako bajtu kontrolnego
  928.  
  929.                         nr_stringu++;
  930.  
  931.                         if (code == (int)0x0C) {
  932.  
  933.                             result_is_string[nr_stringu] = true;                       
  934.  
  935.                         } else {
  936.  
  937.                             result_is_string[nr_stringu] = false;
  938.  
  939.                         }
  940.  
  941.                         hex_0x0C = true;
  942.  
  943.                     } else {
  944.  
  945.                         last_byte_control = false;
  946.  
  947.                     }
  948.  
  949.  
  950.  
  951.                     if (verbose == true)
  952.  
  953.                         System.out.println();
  954.  
  955.                 }
  956.  
  957.                
  958.  
  959.             }
  960.  
  961.  
  962.  
  963.             offset += 64;
  964.  
  965.         }      
  966.  
  967.  
  968.  
  969.         if (verbose) {
  970.  
  971.             System.out.println();
  972.  
  973.         }
  974.  
  975.  
  976.  
  977.         // formatowanie odczytanego strumienia bajtow
  978.  
  979.         int size, nr_danych=0;
  980.  
  981.         dane_karty = new String[9];
  982.  
  983.         String form_data;
  984.  
  985.        
  986.  
  987.         for (i = 0; i<result.length; i++) {
  988.  
  989.             if (result[i] != null) {
  990.  
  991.                 size = 4+result_len[i];
  992.  
  993.                 if (size > result[i].length()) {
  994.  
  995.                     size = result[i].length();
  996.  
  997.                 }
  998.  
  999.                 form_data = result[i].substring(4,size);
  1000.  
  1001.                
  1002.  
  1003.                 if ((i>0) && (nr_danych < 9)) {
  1004.  
  1005.                     if (i != 5) {
  1006.  
  1007.                         dane_karty[nr_danych] = form_data;
  1008.  
  1009.                         nr_danych++;
  1010.  
  1011.                     } else if ((i == 5) && (result_is_string[i] == true)) { // drugie imie
  1012.  
  1013.                         dane_karty[nr_danych-1] += " "+form_data;
  1014.  
  1015.                     } else {
  1016.  
  1017.                         dane_karty[nr_danych] = form_data;
  1018.  
  1019.                         nr_danych++;
  1020.  
  1021.                     }
  1022.  
  1023.                 }
  1024.  
  1025.             }
  1026.  
  1027.         }
  1028.  
  1029.         // sformatowanie daty waznosci karty na RRRR-MM-DD
  1030.  
  1031.         dane_karty[7] = dane_karty[7].substring(0,4)+"-"+dane_karty[7].substring(4,6)+"-"+dane_karty[7].substring(6,8);
  1032.  
  1033.        
  1034.  
  1035.         return success;
  1036.  
  1037.     }
  1038.  
  1039.        
  1040.  
  1041.     public String[] getPersonalData() {
  1042.  
  1043.         return dane_karty;
  1044.  
  1045.     }
  1046.  
  1047.    
  1048.  
  1049.     private boolean disconnect() throws Exception {
  1050.  
  1051.         boolean success = true;
  1052.  
  1053.        
  1054.  
  1055.         card.disconnect(false);
  1056.  
  1057.        
  1058.  
  1059.         return success;
  1060.  
  1061.     }
  1062.  
  1063.    
  1064.  
  1065.     public static boolean check9000(ResponseAPDU ra) {
  1066.  
  1067.         byte[] response = ra.getBytes();
  1068.  
  1069.         if (response[response.length - 2] == (byte) 0x90 && response[response.length - 1] == (byte) 0x00) {
  1070.  
  1071.             return (true);
  1072.  
  1073.         } else {
  1074.  
  1075.             return (false);
  1076.  
  1077.         }
  1078.  
  1079.     }
  1080.  
  1081.  
  1082.  
  1083.     private String byteToHex(byte b){
  1084.  
  1085.         int i = b & 0xFF;
  1086.  
  1087.         return Integer.toHexString(i);
  1088.  
  1089.     }
  1090.  
  1091.    
  1092.  
  1093.     public byte[] getATR() {
  1094.  
  1095.         return atr;
  1096.  
  1097.     }
  1098.  
  1099.        
  1100.  
  1101.     public String getHexATR() {
  1102.  
  1103.         return atr_hex;
  1104.  
  1105.     }
  1106.  
  1107.    
  1108.  
  1109.     public boolean wasCardInsertedBad() {
  1110.  
  1111.         return cardInsertedBad;
  1112.  
  1113.     }
  1114.  
  1115.  
  1116.  
  1117.     public boolean wasCardNotInserted() {
  1118.  
  1119.         return cardNotInserted;
  1120.  
  1121.     }
  1122.  
  1123.    
  1124.  
  1125. }
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133. class CheckCardPresent {
  1134.  
  1135.     private static CardTerminal terminal = null;
  1136.  
  1137.     private static CardChannel channel;
  1138.  
  1139.     private static Card card;
  1140.  
  1141.    
  1142.  
  1143.     private boolean cardPresent;
  1144.  
  1145.  
  1146.  
  1147.     CheckCardPresent() throws Exception {
  1148.  
  1149.         boolean success = true;
  1150.  
  1151.         cardPresent = true;
  1152.  
  1153.  
  1154.  
  1155.         TerminalFactory factory = TerminalFactory.getDefault();
  1156.  
  1157.         List terminalList = null;
  1158.  
  1159.            
  1160.  
  1161.         try {
  1162.  
  1163.             terminalList = factory.terminals().list();
  1164.  
  1165.         } catch (CardException e) {
  1166.  
  1167.             cardPresent = false;
  1168.  
  1169.             success = false;
  1170.  
  1171.         }
  1172.  
  1173.        
  1174.  
  1175.         if (success) {
  1176.  
  1177.             // korzysta z pierwszego terminala z listy
  1178.  
  1179.             terminal = (CardTerminal) terminalList.get(0);
  1180.  
  1181.             cardPresent = terminal.isCardPresent();
  1182.  
  1183.         }
  1184.  
  1185.     }
  1186.  
  1187.    
  1188.  
  1189.     public boolean get() {
  1190.  
  1191.         return cardPresent;
  1192.  
  1193.     }
  1194.  
  1195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement