Package org.infotechservice.smartcard.test

Source Code of org.infotechservice.smartcard.test.TestMain

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.infotechservice.smartcard.test;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.infotechservice.smartcard.smartcard.NewSocialCardListener;
import org.infotechservice.smartcard.smartcard.NewSocialCardService.Excemption;
import org.infotechservice.smartcard.smartcard.OldSocialCardService;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
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;
import org.infotechservice.smartcard.service.SmartCard;
import org.infotechservice.smartcard.service.SmartCardServer;
import org.infotechservice.smartcard.smartcard.NewSocialCardService;
import org.infotechservice.smartcard.smartcard.OldSocialCardListener;
import org.infotechservice.smartcard.utils.Converter;
import org.infotechservice.smartcard.utils.Messages;

/**
*
* @author Finder
*/
public class TestMain {

    //static final CommandAPDU              createFile;
    static {
        /*byte[]        FCP = ba(
        0x62
        , 0
        , 0x82 // TAG file descriptor
        , 0x02 // len
        , 0x78 // file descriptor
        , 0x21 // data coding byte h/z
        , 0x83 // TAG File id
        , 0x02 // len
        , 0x7F
        , 0x84
        , 0x8A // TAG Life cicle status information - h/z
        , 0x01
        , 0x00 // activated
        , 0x8c //  Tag Security Attributes
        , 0x02
        , 0x01
        , 0x00
        , 0x81 // Tag: Total File Size
        , 0x02 // len
        , 0x00
        , 0x80
        //, 0xC6

        );
        FCP[1] = (byte) (FCP.length - 2);
        createFile = new CommandAPDU(
        0xA0  // class
        , 0xE0  // command
        , 0x00  // op1
        , 0x00  // op2
        , FCP
        );*/
    }

    private static int fi(int fi) {
        return (fi << 3) | 4;
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {

            Messages.init();

            // show the list of available terminals
            TerminalFactory factory = TerminalFactory.getDefault();
            System.out.println("TerminalFactory: " + factory);

            CardTerminals terminals = factory.terminals();

            List<CardTerminal> terminalList = terminals.list();
            if(terminalList.size() == 0) {
                System.out.println("No terminals found");
                return;
            }
            System.out.println("Terminal list: " + terminalList);
           
            // get the first terminal
            CardTerminal terminal = terminalList.get(0);
            System.out.println("present: " + terminal.isCardPresent());

            //seviceTest();

            // establish a connection with the card
            if(!terminal.waitForCardPresent(500)) {
                System.out.println("no card inserted");
                return;
            }
            Card card = terminal.connect("*");
          
            try {

                System.out.println("card: " + card);
                System.out.println("proto: " + card.getProtocol());
                System.out.println("ATR-h: " + Converter.hexDump(card.getATR().getHistoricalBytes()));
                System.out.println("ATR: " + Converter.hexDump(card.getATR().getBytes()));
                CardChannel channel = card.getBasicChannel();
                System.out.println("Channel: " + channel.toString());
                System.out.println("Channel number: " + channel.getChannelNumber());

                 execTestScript(channel);

            } finally {
                card.disconnect(false);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void execTestScript(CardChannel channel) throws Exception {

        CommandAPDU select = new CommandAPDU(
            0x00// class
            0xA4// command
            0x04// op1
            0x00// op2
            new byte[] {
                //AID of applet
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x07 //SOC
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x01 //MED
                //(byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x02 //DLO
                //(byte)0x32, 0x50, 0x41, 0x59, 0x2E, (byte)0x53, (byte)0x59, (byte)0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31 //PSE
                (byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x10, (byte)0x10 //MASTERCARD
            }
        );
        ResponseAPDU r = channel.transmit(select);
        Messages msg = Messages.findMessage(r);
        if (!msg.isOk()) {
            System.out.println("Cannot select applet");
            return;
        }

        for(int file = 0; file<16; file++) {
            System.out.println("READ FILE:" + file);
            for(int rec = 0; rec<16; rec++) {
                int EF = getFile(file);
                CommandAPDU command = new CommandAPDU(0x00, 0xB2, rec, EF, 256);
                r = channel.transmit(command);
                msg = Messages.findMessage(r);
                if (msg.isOk()) {
                    System.out.println("\nFOUND RECORD OK: file:" + file + "rec:" + rec);
                    //System.out.println("REQUEST: " + Converter.hexDump(command.getBytes()));
                    System.out.println("response: " + r.toString());
                    System.out.println("raw: " + Converter.hexDump(r.getData()));
                    System.out.println("cp1251: " + new String(r.getData(), "windows-1251"));
                    //System.out.println("result: " + msg);
                    int hash = 0, xor = 0;
                    for(int i=2; i<r.getData().length; i++) {
                        hash += r.getData()[i];
                        xor ^= r.getData()[i];
                    }
                    System.out.println("hash: " + (hash & 0xFF) + ", xor=" + xor +" bytes:" + r.getData().length);
                }
            }
        }

    }

    private static int getFile(int file) {
        return (byte)((file << 3) | 4);
    }

    public static void seviceTest() throws Exception {
        SmartCardServer service = new SmartCardServer();
        service.addCardListener(new NewSocialCardListener() {
            @Override
            public void onCardInsert(SmartCard smartcard) {
                NewSocialCardService service;
                try {
                    service = new NewSocialCardService(smartcard);
                    System.out.println("New social cart inserted :"
                            + "\n F.I.O.: "  + service.getFIO()
                            + "\n Sex: "  + service.getSex()
                            + "\n Address: "  + service.getAddress()
                            + "\n Birthday: "  + service.getBirthDay()
                            + "\n Serial: "  + service.getSerial()
                            + "\n SNILS: "  + service.getSnils()
                            + "\n INN: "  + service.getInn());
                    for(int i=0; i<12; i++) {
                        Excemption exc = service.getExcemption(i);
                        if(exc == null) {
                            continue;
                        }
                        System.out.println(" excemption " + i
                                + ": type:" + exc.type
                                + ", begin:" + exc.begin
                                + ", end:" + exc.end);
                    }
                    System.out.println("card end ");
                } catch (CardException ex) {
                    System.out.println("New Social read error :"  + ex.getMessage());
                }
            }
        });

        service.addCardListener(new OldSocialCardListener() {
            @Override
            public void onCardInsert(SmartCard smartcard) {
                OldSocialCardService service = new OldSocialCardService(smartcard);
                try {
                    System.out.println("Old Social cart inserted :"  + service.readID());
                } catch (CardException ex) {
                    System.out.println("Old Social read error :"  + ex.getMessage());
                }
            }
        });

        service.start();
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("Нажмите 0 для выхода");
            String ln = input.readLine();
            if (ln == null || ln.equals("0")) {
                break;
            }
        }
        service.stop();
    }
}
TOP

Related Classes of org.infotechservice.smartcard.test.TestMain

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.