Package org.infotechservice.smartcard.smartcard

Source Code of org.infotechservice.smartcard.smartcard.SmartStoreService

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import org.infotechservice.smartcard.service.CardService;
import org.infotechservice.smartcard.service.SmartCard;
import org.infotechservice.smartcard.utils.BCD;
import org.infotechservice.smartcard.utils.ByteArrayHelper;

/**
*
* @author Finder
*/
public class SmartStoreService extends CardService {

    private static final byte[] COMMAND_HEAD = new byte[]{
        (byte) 0x43, (byte) 0x6C, (byte) 0x69, (byte) 0x70,
        (byte) 0x73, (byte) 0x43, (byte) 0x6C, (byte) 0x69,
        (byte) 0x65, (byte) 0x6E, (byte) 0x74, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x80
    };
    private static final CommandAPDU[] DEFAULT_SELECT_COMAND = new CommandAPDU[]{
        new CommandAPDU(0xA0, 0xA4, 0x00, 0x00, new byte[]{0x3F, 0x00}),
        new CommandAPDU(0xA0, 0xA4, 0x00, 0x00, new byte[]{0X7F, 0X10}),
        new CommandAPDU(0xA0, 0xA4, 0x00, 0x00, new byte[]{0X6F, 0X3A}),
    };
    private static final byte[] WRITE_COMMAND = new byte[] {
        (byte)0xA0, (byte)0xDC, (byte)0x96, (byte)0x04, (byte)0x1C
    };
    private static final byte[] READ_COMMAND = new byte[] {
        (byte)0xA0, (byte)0xB2, (byte)0x96, (byte)0x04, (byte)0x1C
    };

    public SmartStoreService(SmartCard card) {
        super(card);
    }

    private void commandSelect() throws CardException {
        execScript(DEFAULT_SELECT_COMAND, "Invalid card type");
    }

    public boolean isMyCard() throws CardException {
        try {
            commandSelect();
            return true;
        } catch (CardException ex) {
            return false;
        }
    }

    public void writeID(int id) throws CardException {
        commandSelect();
        byte[] command = ByteArrayHelper.concat(COMMAND_HEAD, new BCD(id).getValue());
        command[14] = (byte) (command.length - 15);
        command = ByteArrayHelper.expand(command, 28, (byte) 0xff);
        command = ByteArrayHelper.concat(WRITE_COMMAND, command);
        exec(new CommandAPDU(command), "Write id error");
    }

    public int readID() throws CardException {
        commandSelect();
        ResponseAPDU resp = exec(new CommandAPDU(READ_COMMAND), "Read id error");
        if (Arrays.equals(Arrays.copyOf(COMMAND_HEAD, 14), Arrays.copyOf(resp.getData(), 14))) {
            return new BCD(resp.getData()).toInteger(16);
        } else {
            return 0;
        }
    }

    public static void idTest(SmartCard card) throws Exception {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("Выберите тип операции");
            System.out.println("  1) прочитать ID");
            System.out.println("  2) записать ID");
            System.out.println("  0) Выход");
            String ln = input.readLine();
            if (ln == null) {
                break;
            }
            ln = ln.trim();
            if (ln.equals("0")) {
                break;
            } else if (ln.equals("1")) {
                System.out.println("Id: " + new SmartStoreService(card).readID());
            } else if (ln.equals("2")) {
                System.out.print("Input ID: ");
                String sid = input.readLine();
                int id = Integer.parseInt(sid);
                new SmartStoreService(card).writeID(id);
            } else {
                System.out.println("Incorrect input.");
            }
        }
    }
}
TOP

Related Classes of org.infotechservice.smartcard.smartcard.SmartStoreService

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.