Guest User

Untitled

a guest
Jan 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import java.util.List;
  2. import java.io.FileWriter;
  3. import java.io.BufferedWriter;
  4. import java.nio.ByteBuffer;
  5. import javax.smartcardio.*;
  6.  
  7. class ProxNRollReader {
  8. byte[] GET_UID = {(byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00};
  9.  
  10. public static void main(String[] args) {
  11. if (args.length < 1) {
  12. System.out.println("usage: java ProxNRollReader <file_to_output>");
  13. System.exit(1);
  14. }
  15. new ProxNRollReader().start(args[0]);
  16. }
  17.  
  18. public void start(String fileToOutput) {
  19. FileWriter file;
  20. BufferedWriter out;
  21. try {
  22. file = new FileWriter(fileToOutput);
  23. out = new BufferedWriter(file);
  24. } catch (Exception e) {
  25. System.err.println("error opening file!");
  26. e.printStackTrace();
  27. return;
  28. }
  29.  
  30. String currentUID = "";
  31. while (true) {
  32. try {
  33. TerminalFactory factory = TerminalFactory.getDefault();
  34. List<CardTerminal> terminals = factory.terminals().list();
  35.  
  36. if (terminals.size() == 0) {
  37. System.err.println("still no readers.");
  38. }
  39.  
  40. for (CardTerminal terminal: terminals) {
  41. if (terminal.isCardPresent()) {
  42. Card card = terminal.connect("*");
  43. CardChannel channel = card.getBasicChannel();
  44.  
  45. String newUID = sendToChannel(GET_UID, channel);
  46. if (!newUID.equals("") && !newUID.equals(currentUID)) {
  47. System.out.println(newUID);
  48. out.write(newUID);
  49. out.newLine();
  50. out.flush();
  51. }
  52. currentUID = newUID;
  53. }
  54. }
  55.  
  56. Thread.sleep(500);
  57. } catch (Exception e) {
  58. System.err.println("E: "+e);
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. private String sendToChannel(byte[] cmd, CardChannel channel) {
  65. String res = "";
  66. byte[] baResp = new byte[258];
  67. ByteBuffer bufCmd = ByteBuffer.wrap(cmd);
  68. ByteBuffer bufResp = ByteBuffer.wrap(baResp);
  69.  
  70. int output = 0;
  71. try {
  72. output = channel.transmit(bufCmd, bufResp);
  73. } catch (CardException ex) {
  74. System.err.println("Caught exception on channel");
  75. }
  76.  
  77. for (int ii = 0; ii < output; ii++) {
  78. res += String.format("%02X", baResp[ii]);
  79. }
  80.  
  81. return res;
  82. }
  83. }
Add Comment
Please, Sign In to add comment