jdk1.6 Java language Connection PCSC reader sends APDU instructions

Source: Internet
Author: User

The Java language Operator Reader jdk1.6 provides a package javax.smartcardio that connects directly to the PCSC reader, and to the reader for instruction Interaction

Package Javax.smartcardio More Information

Http://docs.oracle.com/javase/6/docs/jre/api/security/smartcardio/spec/javax/smartcardio/package-summary.html

Java language Code Instance

/**
*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 ();
}
}
}

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 = 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.");
}
}
}

jdk1.6 Java language Connection PCSC reader sends APDU instructions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.