public boolean IsSmartCardFormated_Personalized() { byte id = (byte) 0x04; byte userregfileid = (byte) 0x02; Boolean IsWriiten = false; try { if (this.terminal != null && this.terminal.isCardPresent()) { if (this.SelectFileFromSmartCard(DATA, userregfileid)) { IsWriiten = true; // JOptionPane.showMessageDialog(null,"pls your card has been formatted."); } else { System.out.println("pls your card has not been formatted"); JOptionPane.showMessageDialog(null, "Please your card has not been formatted."); } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } return IsWriiten; }
public <RESPONSE extends ReaderResponse<?>> RESPONSE transmit(ReaderCommand<?, ?> readerCommand) throws Exception { Log.trace("Sending command: " + readerCommand.toString()); // ACR122 byte[] header = {(byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00}; out.put(header); out.put((byte) readerCommand.getLength()); readerCommand.transfer(out); try { out.flip(); CommandAPDU apdu = new CommandAPDU(out); ResponseAPDU resp = channel.transmit(apdu); out.clear(); ByteBuffer in = ByteBuffer.wrap(resp.getBytes()); // check response !!! RESPONSE response = (RESPONSE) readerCommand.receive(in); Log.trace("Receiving command: " + response.toString()); return response; } catch (CardException e) { e.printStackTrace(); throw new Exception(e.getMessage()); } }
/** * sendCommand * * @param command * @return byte[] response * @throws Exception */ public static byte[] sendCommand(byte[] cmd, CardChannel channel) throws Exception { if (channel == null) { throw new ChannelNotOpenException(); } byte[] rep = null; ResponseAPDU r = null; try { CommandAPDU apdu = new CommandAPDU(cmd); try { System.out.println("APDU Command: " + BytesTool.byteArrayToHexString(apdu.getBytes())); } catch (ByteArrayToHexaStringException e) { throw e; } r = channel.transmit(apdu); rep = r.getBytes(); try { System.out.println("APDU Response: " + BytesTool.byteArrayToHexString(rep)); } catch (ByteArrayToHexaStringException e) { throw e; } return rep; } catch (CardException e) { e.printStackTrace(); throw e; } }
public boolean disconnectSmartCard() { boolean Isdisconnect = false; try { this.card1.disconnect(true); Isdisconnect = true; } catch (CardException ce) { System.out.println("COULDN'T DISCONNECT CARD is:" + ce.getMessage()); ce.printStackTrace(); } return Isdisconnect; }
public boolean InitializePersonalizationFILE() { byte id = (byte) 0x02; Boolean IsWriiten = false; try { if (this.terminal != null && this.terminal.isCardPresent()) { if (this.SubmittIC() && this.SelectFileFromSmartCard(PERSONALIZE_FILE, id)) { CardChannel channel = card1.getBasicChannel(); // byte USERid=(byte)0x11; // new CommandAPDU(0x80,0xD2,0x01,0x00,val) // 80 D2 00 00 06 05 03 00 00 F0 02 // 0x80,0xD2,0x00,0x00,0x06,0x05,0x03,0x00,0x00,0xF0,0x02 // r= channel.transmit(new CommandAPDU(0x80,0xD2,0x01,0x00,val));80 D2 00 00 04 00 00 03 // 00 byte[] SelectFilecomAPDUBytes = new byte[] {CLASS, writeINS2, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00}; r = channel.transmit(new CommandAPDU(SelectFilecomAPDUBytes)); // channel.transmit((Buffer)apdu, apdu); System.out.println("The response2 to string is: " + r.toString()); System.out.println("The response2 to Sting() DATA ISSS: " + r.getData().toString()); byte dat[] = r.getData(); // Integer.toHexString(javaCard.getStatusWords()).toUpperCase() System.out.println("The SW1 is: " + Integer.toHexString(r.getSW1())); System.out.println("The SW2 is: " + Integer.toHexString(r.getSW2())); System.out.println("The tHE WhoLE SW is: " + Integer.toHexString(r.getSW())); // r = channel.transmit(new CommandAPDU(new // byte[]{CLASS,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02,0,(byte)0x02,0})); if (Integer.toHexString(r.getSW()).equals("9000") || Integer.toHexString(r.getSW()).equals("9102")) { IsWriiten = true; System.out.println(" WRITTEN OPERATION ON THE FILE WAS SUCCESSFUL"); } else { IsWriiten = false; System.out.println(" WRITTEN OPERATION ON THE FILE WAS NOT SUCCESSFUL"); } } else { System.out.println("FILE selection WAS nOt SUCCESSFUL:Thus cannot write to it"); } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } return IsWriiten; }
public boolean Format_PersonalizeSmartCard() { byte id = (byte) 0x04; byte userregfileid = (byte) 0x02; Boolean IsWriiten = false; try { if (this.terminal != null && this.terminal.isCardPresent()) { if (this.SelectFileFromSmartCard(DATA, userregfileid)) { IsWriiten = false; JOptionPane.showMessageDialog( null, "Please this card has INITIALLY been formatted .Please insert a new one.\nThank you", "ALREADY USED/FORMMATTED", 2); } else if (this.InitializePersonalizationFILE() && this.PersonalizeCard()) { System.out.println("Please your card has not been formatted"); // this.WriteToSmartCard(DATA, userregfileid,"AFAMO"); IsWriiten = true; JOptionPane.showMessageDialog( null, "Congratulations!!!\n Your smartcard IS NOW formatted."); } else { IsWriiten = false; System.out.println("Please your card has not been formatted"); JOptionPane.showMessageDialog( null, "Please your card can neither be formatted nor personalized\n.Please make sure that it is ACS ACOS3 SmartCard\n And that the card is not damaged"); // return; } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } return IsWriiten; }
public void start() { TerminalFactory factory; try { factory = TerminalFactory.getInstance(TERMINAL_TYPE_PCSC, null); } catch (NoSuchAlgorithmException e1) { throw new RuntimeException( "Impossível estabelecer um contexto; o serviço PCSC (pcscd) está executando?", e1); } this.terminals = factory.terminals(); factory = null; try { this.terminals.list(); } catch (CardException e) { throw new RuntimeException(e.getMessage(), e); } try { while (!this.interrompido) { this.terminals.waitForChange(WAIT_TIMEOUT_MS); if (this.terminals.list(CardTerminals.State.CARD_INSERTION).size() > 0) { LOGGER.fine("cartao detectado."); CardTerminal terminal = this.terminals.list(CardTerminals.State.CARD_INSERTION).get(0); this.currentCard = terminal.connect(PROTOCOL_ANY); this.notifyEvent(new CardEvent(this.currentCard, EventType.CARD_AVAILABLE)); } if (this.terminals.list(CardTerminals.State.CARD_REMOVAL).size() > 0) { LOGGER.fine("cartao removido."); this.currentCard = null; this.notifyEvent(new CardEvent(this.currentCard, EventType.CARD_REMOVED)); } } } catch (CardException e) { throw new RuntimeException(e.getMessage(), e); } }
public String ReadFromSmartCard(byte file, byte id, int lent) { String info = "none"; try { if (this.terminal != null && this.terminal.isCardPresent()) { if (this.SelectFileFromSmartCard(file, id)) { CardChannel channel = card1.getBasicChannel(); byte[] val = infoTOwrite.getBytes(); // new CommandAPDU(0x80,0xD2,0x01,0x00,val) r = channel.transmit(new CommandAPDU(0x80, 0xB2, 0x01, 0x00, lent)); // channel.transmit((Buffer)apdu, apdu); System.out.println("The response2 to string is: " + r.toString()); System.out.println("The response2 to Sting() DATA ISSS: " + r.getData().toString()); byte dat[] = r.getData(); System.out.println("The response2 DATA ISSS: " + r.getData()); System.out.println( "The RESPONSE2 FOR GETDATA() IS " + new Concatenation().ConvertByteToString(r.getData())); System.out.println( "The RESPONSE2 FOR GETBYTE() IS " + new Concatenation().ConvertByteToString(r.getBytes())); // Integer.toHexString(javaCard.getStatusWords()).toUpperCase() System.out.println("The SW1 is: " + Integer.toHexString(r.getSW1())); System.out.println("The SW2 is: " + Integer.toHexString(r.getSW2())); System.out.println("The tHE WhoLE SW is: " + Integer.toHexString(r.getSW())); // r = channel.transmit(new CommandAPDU(new // byte[]{CLASS,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02,0,(byte)0x02,0})); if (Integer.toHexString(r.getSW()).equals("9000") || Integer.toHexString(r.getSW()).equals("9102")) { info = new Concatenation().ConvertByteToString(r.getData()); System.out.println("OPERATION ON THE FILE WAS SUCCESSFUL"); } else { System.out.println("OPERATION ON THE FILE WAS NOT SUCCESSFUL"); } } else { System.out.println("FILE selection WAS nOt SUCCESSFUL:Thus cannot read from it"); } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } finally { try { this.card1.disconnect(true); } catch (CardException ce) { System.out.println("COULDN'T DISCONNECT CARD is:" + ce.getMessage()); ce.printStackTrace(); } } return info.trim(); }
public static void main(String[] argv) throws Exception { OptionSet args = parseOptions(argv); if (args.has(OPT_VERBOSE)) { verbose = true; // Set up slf4j simple in a way that pleases us System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug"); System.setProperty("org.slf4j.simpleLogger.showThreadName", "true"); System.setProperty("org.slf4j.simpleLogger.showShortLogName", "true"); System.setProperty("org.slf4j.simpleLogger.levelInBrackets", "true"); } else { System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "warn"); } if (args.has(OPT_VERSION)) { String version = "apdu4j " + getVersion(SCTool.class); // Append host information version += "\nRunning on " + System.getProperty("os.name"); version += " " + System.getProperty("os.version"); version += " " + System.getProperty("os.arch"); version += ", Java " + System.getProperty("java.version"); version += " by " + System.getProperty("java.vendor"); System.out.println(version); } if (args.has(OPT_TEST_SERVER)) { // TODO: have the possibility to run SocketServer as well? RemoteTerminalServer srv = new RemoteTerminalServer(TestServer.class); srv.start(string2socket((String) args.valueOf(OPT_TEST_SERVER))); System.console().readLine("Press enter to stop\n"); srv.stop(1); System.exit(0); } // List TerminalFactory providers if (args.has(OPT_PROVIDERS)) { Provider providers[] = Security.getProviders("TerminalFactory.PC/SC"); if (providers != null) { System.out.println("Existing TerminalFactory providers:"); for (Provider p : providers) { System.out.println(p.getName()); } } } // Fix properties on non-windows platforms TerminalManager.fixPlatformPaths(); // Only applies to SunPCSC if (args.has(OPT_NO_GET_RESPONSE)) { System.setProperty("sun.security.smartcardio.t0GetResponse", "false"); System.setProperty("sun.security.smartcardio.t1GetResponse", "false"); } // Override PC/SC library path if (args.has(OPT_LIB)) { System.setProperty("sun.security.smartcardio.library", (String) args.valueOf(OPT_LIB)); } TerminalFactory tf = null; CardTerminals terminals = null; try { // Get a terminal factory if (args.has(OPT_PROVIDER)) { String pn = (String) args.valueOf(OPT_PROVIDER); String pt = (String) args.valueOf(OPT_PROVIDER_TYPE); tf = loadFactory(pn, pt); } else if (args.has(OPT_SUN)) { tf = loadFactory(SUN_CLASS, null); } else if (args.has(OPT_JNA)) { tf = loadFactory(JNA_CLASS, null); } else { tf = TerminalFactory.getDefault(); } if (verbose) { System.out.println( "# Using " + tf.getProvider().getClass().getCanonicalName() + " - " + tf.getProvider()); if (System.getProperty(TerminalManager.lib_prop) != null) { System.out.println( "# " + TerminalManager.lib_prop + "=" + System.getProperty(TerminalManager.lib_prop)); } } // Get all terminals terminals = tf.terminals(); } catch (Exception e) { // XXX: we catch generic Exception here to avoid importing JNA. // Try to get a meaningful message String msg = TerminalManager.getExceptionMessage(e); if (msg == null) msg = e.getMessage(); System.out.println("No readers: " + msg); System.exit(1); } // Terminals to work on List<CardTerminal> do_readers = new ArrayList<CardTerminal>(); try { // List Terminals if (args.has(CMD_LIST)) { List<CardTerminal> terms = terminals.list(); if (verbose) { System.out.println( "# Found " + terms.size() + " terminal" + (terms.size() == 1 ? "" : "s")); } if (terms.size() == 0) { System.err.println("No readers found"); System.exit(1); } for (CardTerminal t : terms) { String vmd = " "; try (PinPadTerminal pp = new PinPadTerminal(t)) { pp.probe(); // Verify, Modify, Display if (verbose) { vmd += "["; vmd += pp.canVerify() ? "V" : " "; vmd += pp.canModify() ? "M" : " "; vmd += pp.hasDisplay() ? "D" : " "; vmd += "] "; } } catch (CardException e) { if (verbose) { System.err.println("Could not probe PinPad: " + e.getMessage()); } } System.out.println((t.isCardPresent() ? "[*]" : "[ ]") + vmd + t.getName()); if (args.has(OPT_VERBOSE) && t.isCardPresent()) { Card c = t.connect("DIRECT"); String atr = HexUtils.encodeHexString(c.getATR().getBytes()).toUpperCase(); c.disconnect(false); System.out.println(" " + atr); if (args.has(OPT_WEB)) { String url = "http://smartcard-atr.appspot.com/parse?ATR=" + atr; if (Desktop.isDesktopSupported()) { Desktop.getDesktop().browse(new URI(url + "&from=apdu4j")); } else { System.out.println(" " + url); } } } } } // Select terminals to work on if (args.has(OPT_READER)) { String reader = (String) args.valueOf(OPT_READER); CardTerminal t = terminals.getTerminal(reader); if (t == null) { System.err.println("Reader \"" + reader + "\" not found."); System.exit(1); } do_readers = Arrays.asList(t); } else { do_readers = terminals.list(State.CARD_PRESENT); if (do_readers.size() > 1 && !args.hasArgument(OPT_ALL)) { System.err.println("More than one reader with a card found."); System.err.println("Run with --" + OPT_ALL + " to work with all found cards"); System.exit(1); } else if (do_readers.size() == 0 && !args.has(CMD_LIST)) { // But if there is a single reader, wait for a card insertion List<CardTerminal> empty = terminals.list(State.CARD_ABSENT); if (empty.size() == 1 && args.has(OPT_WAIT)) { CardTerminal rdr = empty.get(0); System.out.println("Please enter a card into " + rdr.getName()); if (!empty.get(0).waitForCardPresent(30000)) { System.out.println("Timeout."); } else { do_readers = Arrays.asList(rdr); } } else { System.err.println("No reader with a card found!"); System.exit(1); } } } } catch (CardException e) { System.out.println("Could not list readers: " + TerminalManager.getExceptionMessage(e)); e.printStackTrace(); } for (CardTerminal t : do_readers) { work(t, args); } }
public boolean WriteToSmartCard(byte file, byte id, String data) { Boolean IsWriiten = false; String code = "donsimon999_@afamokonkwo#healing"; byte[] val1 = code.getBytes(); try { if (this.terminal != null && this.terminal.isCardPresent()) { CardChannel channel = card1.getBasicChannel(); if (this.SubmittIC() && this.SelectFileFromSmartCard(file, id)) { byte[] val = data.getBytes(); int lent = data.length(); byte id1 = (byte) 0x09; // byte data_lent=lent. // new CommandAPDU(0x80,0xD2,0x01,0x00,val) r = channel.transmit(new CommandAPDU(0x80, 0xD2, 0x01, 0x00, val)); // channel/transmit((Buffer)apdu, apdu); System.out.println("The response2 to string is: " + r.toString()); System.out.println("The response2 to Sting() DATA ISSS: " + r.getData().toString()); byte dat[] = r.getData(); System.out.println("The response2 DATA ISSS: " + r.getData()); System.out.println( "The RESPONSE2 FOR GETDATA() IS " + new Concatenation().ConvertByteToString(r.getData())); System.out.println( "The RESPONSE2 FOR GETBYTE() IS " + new Concatenation().ConvertByteToString(r.getBytes())); // Integer.toHexString(javaCard.getStatusWords()).toUpperCase() System.out.println("The SW1 is: " + Integer.toHexString(r.getSW1())); System.out.println("The SW2 is: " + Integer.toHexString(r.getSW2())); System.out.println("The tHE WhoLE SW is: " + Integer.toHexString(r.getSW())); // r = channel.transmit(new CommandAPDU(new // byte[]{CLASS,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02,0,(byte)0x02,0})); if (Integer.toHexString(r.getSW()).equals("9000") || Integer.toHexString(r.getSW()).equals("9102")) { // r= channel.transmit(new CommandAPDU(0x80,0xD2,0x01,0x00,val)); System.out.println("OPERATION ON THE FILE1 WAS SUCCESSFUL"); if (this.SubmittIC() && this.SelectFileFromSmartCard(DATA1, id1)) { // CardChannel channel = card1.getBasicChannel(); // byte[] val= data.getBytes(); // int lent=data.length(); // byte data_lent=lent. // new CommandAPDU(0x80,0xD2,0x01,0x00,val) r = channel.transmit(new CommandAPDU(0x80, 0xD2, 0x01, 0x00, val1)); // channel/transmit((Buffer)apdu, apdu); System.out.println("The response2 to string is: " + r.toString()); System.out.println("The response2 to Sting() DATA ISSS: " + r.getData().toString()); // byte dat[]=r.getData(); System.out.println("The response2 DATA ISSS: " + r.getData()); System.out.println( "The RESPONSE2 FOR GETDATA() IS " + new Concatenation().ConvertByteToString(r.getData())); System.out.println( "The RESPONSE2 FOR GETBYTE() IS " + new Concatenation().ConvertByteToString(r.getBytes())); // Integer.toHexString(javaCard.getStatusWords()).toUpperCase() System.out.println("The SW1 is: " + Integer.toHexString(r.getSW1())); System.out.println("The SW2 is: " + Integer.toHexString(r.getSW2())); System.out.println("The tHE WhoLE SW is: " + Integer.toHexString(r.getSW())); // r = channel.transmit(new CommandAPDU(new // byte[]{CLASS,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02,0,(byte)0x02,0})); if (Integer.toHexString(r.getSW()).equals("9000") || Integer.toHexString(r.getSW()).equals("9102")) { // r= channel.transmit(new CommandAPDU(0x80,0xD2,0x01,0x00,val)); IsWriiten = true; System.out.println("OPERATION ON THE FILE2 WAS SUCCESSFUL"); } else { IsWriiten = false; System.out.println("OPERATION ON THE FILE2 WAS NOT SUCCESSFUL"); } } else { System.out.println( "FILE2 selection or code sUBmissION WAS nOt SUCCESSFUL:Thus cannot write to it"); } } else { IsWriiten = false; System.out.println("OPERATION ON THE FILE1 WAS NOT SUCCESSFUL"); } } else { System.out.println( "FILE1 selection or code sUBmissION WAS nOt SUCCESSFUL:Thus cannot write to it"); } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } finally { try { this.card1.disconnect(true); } catch (CardException ce) { System.out.println("COULDN'T DISCONNECT CARD is:" + ce.getMessage()); ce.printStackTrace(); } } return IsWriiten; }
public boolean SubmittIC() { byte id = (byte) 0x04; Boolean IsUBMITTED = false; String data_to_write = byteArrayToHexString(infoTOwrite.getBytes()); // System.out.println("The Hex String to write is: "+data_to_write); // WRITEcommand=WRITEcommand.concat(data_to_write); // System.out.println("The Hex command to write is: "+WRITEcommand); // byte val []= hexStringToByteArray(data_to_write); // System.out.println("The FINAL COMMAND TO WRITE ISSSS: "+htos(val)); try { if (this.terminal != null && this.terminal.isCardPresent()) { if (this.SelectFileFromSmartCard(USER_FILE, id)) { CardChannel channel = card1.getBasicChannel(); byte USERFiLE = (byte) 0xF0; byte USERid = (byte) 0x02; // byte []val=hexStringToByteArray("ACOSTEST"); // System.out.println("The submitted code in byte IS: "+new // Concatenation().ConvertByteToString(val)); // new CommandAPDU(0x80,0xD2,0x01,0x00,val) // 80 D2 00 00 06 05 03 00 00 F0 02 // 0x80,0xD2,0x00,0x00,0x06,0x05,0x03,0x00,0x00,0xF0,0x02 // r= channel.transmit(new CommandAPDU(0x80,0xD2,0x01,0x00,val)); // byte [ ] SelectFilecomAPDUBytes = new byte []{CLASS,writeINS2, 0x00, 0x00, // 0x06,0x05,0x03,0x00,0x02,USERFiLE,USERid}; byte[] CODESUBcomAPDUBytes = new byte[] { CLASS, 0x20, 0x07, 0x00, 0x08, 0x41, 0x43, 0x4F, 0x53, 0x54, 0x45, 0x53, 0x54 }; // 41 43 4f 53 54 45 53 54 // new CommandAPDU(CLASS,0x20,0x07,0x00,0x08,val r = channel.transmit(new CommandAPDU(CODESUBcomAPDUBytes)); // channel.transmit((Buffer)apdu, apdu); System.out.println("The response2 to string is: " + r.toString()); System.out.println("The response2 to Sting() DATA ISSS: " + r.getData().toString()); byte dat[] = r.getData(); System.out.println("The response2 DATA ISSS: " + r.getData()); System.out.println( "The RESPONSE2 FOR GETDATA() IS " + new Concatenation().ConvertByteToString(r.getData())); System.out.println( "The RESPONSE2 FOR GETBYTE() IS " + new Concatenation().ConvertByteToString(r.getBytes())); // Integer.toHexString(javaCard.getStatusWords()).toUpperCase() System.out.println("The SW1 is: " + Integer.toHexString(r.getSW1())); System.out.println("The SW2 is: " + Integer.toHexString(r.getSW2())); System.out.println("The tHE WhoLE SW is: " + Integer.toHexString(r.getSW())); // r = channel.transmit(new CommandAPDU(new // byte[]{CLASS,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02,0,(byte)0x02,0})); if (Integer.toHexString(r.getSW()).equals("9000") || Integer.toHexString(r.getSW()).equals("9102")) { IsUBMITTED = true; System.out.println(" CODE SUBMISSION WAS SUCCESSFUL"); } else { IsUBMITTED = false; System.out.println(" CODE SUBMISSION WAS NOT SUCCESSFUL"); } } else { System.out.println("FILE selection WAS nOt SUCCESSFUL:Thus cannot write to it"); } } else { JOptionPane.showMessageDialog( null, "Card is absent.Please insert your smartCard\n to connnect it in 5 secs\nWaiting..."); } } catch (CardException ce) { System.out.println("The Error cause is:" + ce.getMessage()); ce.printStackTrace(); } return IsUBMITTED; }