Skip to content

Instantly share code, notes, and snippets.

@scottmac
Forked from anonymous/gist:1231492
Created September 21, 2011 08:13
Show Gist options
  • Save scottmac/1231539 to your computer and use it in GitHub Desktop.
Save scottmac/1231539 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.nio.ByteBuffer;
import javax.smartcardio.*;
class ProxNRollReader {
byte[] GET_UID = {(byte)0xFF, (byte)0xCA, (byte)0x00,
(byte)0x00, (byte)0x00};
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("usage: java ProxNRollReader <file_to_output>");
System.exit(1);
}
new ProxNRollReader().start(args[0]);
}
public void start(String fileToOutput) {
FileWriter file;
BufferedWriter out;
try {
file = new FileWriter(fileToOutput);
out = new BufferedWriter(file);
} catch (Exception e) {
System.err.println("error opening file!");
e.printStackTrace();
return;
}
String currentUID = "";
while (true) {
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
CardTerminal reader = null;
for (CardTerminal terminal: terminals) {
if (terminal.getName().contains("SpringCard")) {
reader = terminal;
break;
}
}
if (reader != null) {
while (reader.waitForCardPresent(0)) {
Card card = reader.connect("*");
CardChannel channel = card.getBasicChannel();
String newUID = sendToChannel(GET_UID, channel);
card.disconnect(false);
if (!newUID.equals("") && !newUID.equals(currentUID)) {
System.out.println(newUID);
out.write(newUID);
out.newLine();
out.flush();
}
currentUID = newUID;
reader.waitForCardAbsent(0);
}
} else {
System.out.println("no readers.");
Thread.sleep(500);
}
} catch (Exception e) {
System.err.println("E: "+e);
e.printStackTrace();
}
}
}
private String sendToChannel(byte[] cmd, CardChannel channel) {
String res = "";
try {
ResponseAPDU output = channel.transmit(new CommandAPDU(cmd));
if (0x9000 != output.getSW()) {
System.err.println("SELECT ERROR");
return "";
}
byte[] received = output.getData();
for (int ii = 0; ii < 8 - received.length; ii++) {
res += "00";
}
for (int ii = 0; ii < received.length; ii++) {
res += String.format("%02X", received[ii]);
}
} catch (CardException ex) {
System.err.println("Caught exception on channel: " + ex);
ex.printStackTrace();
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment