Page 1 of 1
How to use jsr177 to access smart card
Posted: Sun Sep 24, 2017 11:10 pm
by notuo
I would like to use jsr177 to access smart card by sending APDU. But I don't know how APDUs are sent to my smartard? Does jsr177 also use similar pc/sc? Any help is greatly appreciated.
Re: How to use jsr177 to access smart card
Posted: Tue Oct 10, 2017 1:35 am
by roundtable
please show us more info about jsr177, URL or other way that we can download it.
Re: How to use jsr177 to access smart card
Posted: Tue Oct 10, 2017 3:33 am
by kuafu
Connect the card reader and the card and reset it。
Code: Select all
/**
*connectpcsc.java
*/
import java.util.List;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.TerminalFactory;
public class connectpcsc {
/**
* main function, listing all the PC/SC readers connected to your PC
*/
public static void main(String[] args) {
// show the list of available terminals
TerminalFactory factory = TerminalFactory.getDefault();
// list of readers (empty)
List<CardTerminal> terminals;
try {
// get list of readers form the terminal
terminals = factory.terminals().list();
// print list of readers to the console.
System.out.println(terminals.toString());
} catch (Exception e) {
// Print Stack-Trace in case of an error
e.printStackTrace();
}
}
}
Connect the card reader and card to send the APDU command
Code: Select all
import java.util.List;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
public class GetStatus {
public static void main(String[] args) {
TerminalFactory factory = TerminalFactory.getDefault();
try {
List<CardTerminal> terminal = factory.terminals().list();
System.out.println("terminal:" + terminal);
CardTerminal ter = terminal.get(0);
// establish a connection with the card
Card card = ter.connect("T=0");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
ResponseAPDU r = channel.transmit(new CommandAPDU(160, 242, 0, 0, 22)); //A0 F2 00 00 16
System.out.println("response: " + r.toString());
for(int j=0; j<8; j++) {
System.out.print(Integer.toHexString( (int)((r.getData()[j]+256) % 256)) + " ");
}
System.out.print("\n");
for(int j=0; j<card.getATR().getBytes().length; j++) {
System.out.print(Integer.toHexString( (int)((card.getATR().getBytes()[j]+256) % 256)) + " ");
}
// disconnect
card.disconnect(false);
} catch (CardException e) {
// TODO Auto-generated catch block
System.out.println("connection erro,or card not inserted.");
}
}
}
More detail
http://docs.oracle.com/javase/6/docs/jre/api/security/smartcardio/spec/javax/smartcardio/package-summary.html
Re: How to use jsr177 to access smart card
Posted: Tue Oct 10, 2017 3:43 am
by kuafu
notuo wrote:I would like to use jsr177 to access smart card by sending APDU. But I don't know how APDUs are sent to my smartard? Does jsr177 also use similar pc/sc? Any help is greatly appreciated.
https://stackoverflow.com/questions/891341/how-to-use-apdu-in-jsr-177-to-access-the-sim-cardMostly, JSR 177 is supposed to be used to extract the certificate that is on the SIM so you can encrypt/decrypt/sign data in a way that your MNO likes.
The difficulty in finding documentation comes from so few handset manufacturers having included a full JSR177 implementation in their phone.
That, in turn, presumably comes from the huge amount of external, security-related specs to read and understand before the JSR177 spec itself makes sense if you're not a cryptography protocols expert.
The TCK sources and supplemental Symbian/Nokia test code would be a good place to look but, unfortunately, none of that has been open-sourced yet.
Common commands like changing the SIM PIN code basically require an understanding of the binary protocol itself. Simply appending the correct sequence of bytes to an APDU GCF url will get it executed if the JSR177 implementation itself is correct.
If you have only looked at the Sun JavaME SDK, you should probably have a look at the APDU MIDlet example in the latest Series60 SDK and added documentation in Nokia's Java Developer's Library.