I'm trying to read data from a tachograph card using javax.smartcardio
. The problem is I'm doing most of the things blindly because I can't find a simple tutorial on APDU commands
.
Currently I managed to print the card type (card: PC/SC card in HID Global OMNIKEY 3x21 Smart Card Reader 0, protocol T=0, state OK
) and read the card identification information (selected file 0520
).
How do I download the tachograph .ddd file?
Here's the code so far:
public static void main(String[] args) {
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
// get the first terminal
CardTerminal terminal = terminals.get(0);
// establish a connection with the card
Card card = terminal.connect("T=0");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
byte[] c1 = hexStringToByteArray(stripCommandSpace("00 A4 04 0C 06 FF544143484F"));
//byte[] c1 = hexStringToByteArray(stripCommandSpace("FF CA 00 00 00"));
ResponseAPDU r1 = channel.transmit(new CommandAPDU(c1));
System.out.println("response: " + byteArrayToHexString(r1.getBytes()));
byte[] c2 = hexStringToByteArray(stripCommandSpace("00 a4 02 0c 02 05 04"));
ResponseAPDU r2 = channel.transmit(new CommandAPDU(c2));
System.out.println("response: " + byteArrayToHexString(r2.getBytes()));
byte[] c3 = hexStringToByteArray(stripCommandSpace("00 B0 00 01 128"));
ResponseAPDU r3 = channel.transmit(new CommandAPDU(c3));
System.out.println("response: " + byteArrayToHexString(r3.getBytes()));
System.out.println("data: " + byteArrayToHexString(r3.getData()));
byte[] ceva = r3.getData();
String str = new String(ceva);
System.out.println("Date card: " + str);
String fileName = "D:\\fisier\\test.ddd";
/*BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(str);
writer.close();*/
FileOutputStream stream = new FileOutputStream(fileName);
try {
stream.write(ceva);
} finally {
stream.close();
}
// disconnect
card.disconnect(false);
} catch (Exception e) {
System.out.println("Exceptie " + e.getMessage());
}
}
I managed to identify and read the driver activity file - I had to change the identifier to 0504
like in the picture below:
Now my problem is how to convert the byte array I get into a .ddd file.
I tried like in the updated version of my code (see above) but the .ddd reader tells me the file is corrupt (I use a third party app which converts .ddd to .txt - called readesm).