write Card Data - Java javax.smartcardio

Java examples for javax.smartcardio:CardChannel

Description

write Card Data

Demo Code


//package com.java2s;

import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;

public class Main {
    public static void writeData(Card c, byte block, byte[] data)
            throws CardException {
        byte cla = (byte) 0xFF;
        byte ins = (byte) 0xD6;
        byte p1 = (byte) 0x00;
        byte p2 = block;
        byte le = 0x10;
        byte[] params = new byte[21];
        for (int i = 0; i < 21; i++) {
            params[i] = 0x20;//from   w ww .  j  a va 2  s  .  c o m
        }
        params[0] = cla;
        params[1] = ins;
        params[2] = p1;
        params[3] = p2;
        params[4] = le;
        for (int i = 0; i < data.length; i++) {
            params[5 + i] = data[i];
        }

        CardChannel channel = c.getBasicChannel();
        CommandAPDU command = new CommandAPDU(params);

        ResponseAPDU response = channel.transmit(command);
        validateResponse(response);

    }

    private static void validateResponse(ResponseAPDU response)
            throws CardException {
        if (response.getSW1() != 144) {
            throw new CardException(
                    "No fue posible cargar la autenticaci?n");
        }
    }
}

Related Tutorials