Skip to content

Instantly share code, notes, and snippets.

Created September 21, 2011 07:46
Show Gist options
  • Save anonymous/1231492 to your computer and use it in GitHub Desktop.
Save anonymous/1231492 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();
if (terminals.size() == 0) {
System.err.println("still no readers.");
}
for (CardTerminal terminal: terminals) {
if (terminal.isCardPresent()) {
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
String newUID = sendToChannel(GET_UID, channel);
if (!newUID.equals("") && !newUID.equals(currentUID)) {
System.out.println(newUID);
out.write(newUID);
out.newLine();
out.flush();
}
currentUID = newUID;
}
}
Thread.sleep(500);
} catch (Exception e) {
System.err.println("E: "+e);
e.printStackTrace();
}
}
}
private String sendToChannel(byte[] cmd, CardChannel channel) {
String res = "";
byte[] baResp = new byte[258];
ByteBuffer bufCmd = ByteBuffer.wrap(cmd);
ByteBuffer bufResp = ByteBuffer.wrap(baResp);
int output = 0;
try {
output = channel.transmit(bufCmd, bufResp);
} catch (CardException ex) {
System.err.println("Caught exception on channel");
}
for (int ii = 0; ii < output; ii++) {
res += String.format("%02X", baResp[ii]);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment