最新发布 java smartcardio 读卡器操作

发布时间: 2022-12-30,浏览量:434
package socket;


import com.jfinal.core.converter.IConverter;

import java.util.List;

import javax.smartcardio.*;


public  class test {

    private static CardChannel cardChannel;
    private static Card card;
    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
            factory = TerminalFactory.getDefault();// 得到一个默认的读卡器工场
            CardTerminals ss=factory.terminals();
            CardTerminal cardTerminal = factory.terminals().getTerminal("ACS ACR1281 1S Dual Reader PICC 0");// 从工厂获得插在电脑上的读卡器列表,get读卡器列表
            cardTerminal.waitForCardPresent(0L);// 等待放置卡片
            card = cardTerminal.connect("*");// 连接卡片
            System.out.println("card:" + card);

            cardChannel = card.getBasicChannel();// 打开通道


           // ResponseAPDU r = channel.transmit(new CommandAPDU(160, 242, 0, 0, 22)); //A0 F2 00 00 16
            //FFCA00 0004


//            CommandAPDU command1=new CommandAPDU(0xff, 0xca, 0x00, 0x00,0x04);
//            String r1=exec(command1);
//            System.out.println("ResponseAPDU: "+ r1);


            CommandAPDU command2=new CommandAPDU(new byte[] {0x00, (byte)0xa4, 0x04, 0x00, 0x05,(byte) 0xd1,0x56, 0x00, 0x01, 0x37 });
            String r2=exec(command2);
            System.out.println("ResponseAPDU: "+ r2);

            CommandAPDU command3=new CommandAPDU(new byte[] {0x00, (byte)0xa4, 0x00, 0x00, 0x02,(byte) 0xef,0x05 });
            String r3=exec(command3);
            System.out.println("ResponseAPDU: "+ r3);

            CommandAPDU command4=new CommandAPDU(new byte[] {0x00, (byte)0xb2, 0x08, 0x04, 0x00});
            String r4=exec(command4);
            System.out.println("ResponseAPDU: "+ r4);
            String idcard= hexStringToString(r4.substring(4,r4.length()-4));
            System.out.println(idcard);
//
//            ResponseAPDU r = cardChannel.transmit(new CommandAPDU(255, 202, 00, 00,00)); //A0 F2 00 00 16
//            System.out.println("ResponseAPDU: "+ r.toString());
//
//            for(int j=0; j<r.getData().length; j++) {
//                System.out.print(Integer.toHexString( ((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)) + " ");
//            }
            card.disconnect(false);

        } catch (Exception e) {
            // Print Stack-Trace in case of an error
            e.printStackTrace();
            System.out.println("connection erro,or card not inserted.");
        }
    }
    public static String exec(CommandAPDU command)  {
        try {
            ResponseAPDU r = cardChannel.transmit(command); //A0 F2 00 00 16

            return bytesToHexString(r.getBytes());
        }
        catch(CardException ex){

            ex.printStackTrace();
            System.out.println("read card error.");
            return "";
        }
    }

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public static String bytesToHexString(byte[] bytes) {

        StringBuilder sb = new StringBuilder();

        int a = 0;

        for (byte b : bytes) { // 使用除与取余进行转换

            if (b < 0) {

                a = 256 + b;

            } else {

                a = b;

            }

            //sb.append("0x");

            sb.append(HEX_CHAR[a / 16]);

            sb.append(HEX_CHAR[a % 16]);

            //sb.append(" ");

        }

        return sb.toString().toUpperCase();

    }
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "gbk");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

	}

热门文章 经典语录

热门文章 热门文章

查看更多