2016-09-17 73 views
0

所以,我有一個非接觸式萬事達卡和一個Visa paywave手機。 我使用非接觸式HID Omnikey 5427 CK。卡的PSE不是它應該是的

這是我的代碼:`

static boolean cardReading = true; 

public static void main(String[] args) throws CardException, UnsupportedEncodingException { 

    while (cardReading == true) { 

    try { 
    TerminalFactory factory = TerminalFactory.getDefault(); 
    List<CardTerminal> terminals = factory.terminals().list(); 
    CardTerminal terminal = terminals.get(0); 

    if (!terminal.isCardPresent()) { 

     continue; 

    } 

    System.out.println("Terminals: " + terminals); 
    System.out.println("Used terminal: " + terminal); 

    Card card = terminal.connect("T=0"); 
    System.out.println("\n\nInserted card: " + card); 
    CardChannel channel = card.getBasicChannel(); 

    String pse = "00A404000E325041592E5359532E444446303100"; 
    CommandAPDU apdu = new CommandAPDU(pse.getBytes()); 
    ResponseAPDU r = channel.transmit(apdu); 

    System.out.println("Response: " + toHex(r.getData().toString()) + " " + r); 
    System.out.println("ADPU: " + toHex(apdu.getBytes().toString()) + " " + r.getSW() + " " + r.getSW1() + " " + r.getSW2() + " " + r.getNr()); 

    apdu = new CommandAPDU((byte)0x00, (byte)0xB2, (byte)0x01, (byte)0x0C, (byte)0x00); 
    r = channel.transmit(apdu); 

    cardReading = false; 
    Toolkit.getDefaultToolkit().beep(); 

    System.out.println("Terminals: " + terminals); 
    System.out.println("Used terminal: " + terminal); 
    System.out.println("\n\nInserted card: " + card); 
    System.out.println("Response: " + toHex(r.getData().toString()) + " " + r); 
    System.out.println("ADPU: " + toHex(apdu.getBytes().toString()) + " " + r.getSW() + " " + r.getSW1() + " " + r.getSW2() + " " + r.getNr()); 



    System.exit(1); 

    } catch(Exception e) { 

     continue; 

    } 

    } 

} 

public static byte[] hexStringToByteArray(String s) { 
    int len = s.length(); 
    byte[] data = new byte[len/2]; 
    for (int i = 0; i < len; i += 2) { 
     data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
          + Character.digit(s.charAt(i+1), 16)); 
    } 
    return data; 
} 

public static String toHex(String arg) { 
    return String.format("%040x", new BigInteger(1, arg.getBytes())); 
} 

`

所以輸出是:

Terminals: [PC/SC terminal HID OMNIKEY 5427 CK 0] 
Used terminal: PC/SC terminal HID OMNIKEY 5427 CK 0 


Inserted card: PC/SC card in HID OMNIKEY 5427 CK 0, protocol T=0, state OK 
Response: 0000000000000000005b42403163343630306630 ResponseAPDU: 65 bytes, SW=9000 
ADPU: 000000000000000000005b424035623665663230 36864 144 0 63 
Terminals: [PC/SC terminal HID OMNIKEY 5427 CK 0] 
Used terminal: PC/SC terminal HID OMNIKEY 5427 CK 0 


Inserted card: PC/SC card in HID OMNIKEY 5427 CK 0, protocol T=0, state OK 
Response: 0000000000000000005b42403565616235383039 ResponseAPDU: 14 bytes, SW=9000 
ADPU: 0000000000000000005b42403433323065373664 36864 144 0 12 

我不明白爲什麼是響應0000000000000000005b42403565616235383039 ... 請幫助。

問候,弗拉德。

+0

您期待什麼迴應?什麼不起作用?請澄清。 –

+0

@JimGarrison人們通常得到的反應是這樣的:6F2F840E325041592E5359532E4444463031A51DBF0C1A61184F07A0000000031010500A564953412044454249548701019000 –

+0

它包括AID什麼的。 –

回答

2

正如Michael Roland所說 - 您對十六進制字符串的處理是錯誤的。

元錯誤1: '字節[]的toString()' 使用

r.getData().toString() 

byte[].toString使用默認Object.toString()實現返回該類名,後跟@Object.hashCode()的值(例如"[[email protected]") - 這不是你想要進一步處理的。

您可以使用Arrays.toString()方法(不執行十六進制轉儲)或any other method

元錯誤2:使用「String.getBytes()」

String pse = "00A404000E325041592E5359532E444446303100"; 
CommandAPDU apdu = new CommandAPDU(pse.getBytes()); 

不產生所需的APDU對象作爲String.getBytes()不執行進制轉換的方法,但是在該平臺的一個字符集轉換默認字符集,例如:"1234".getBytes()給出{ 0x31, 0x32, 0x33, 0x34 }(而不是您所期望的{ 0x12, 0x34 })。

下面是執行一個簡單的代碼示例(幾乎)相同的代碼:

package test.java.so; 

import java.util.List; 

import javax.smartcardio.Card; 
import javax.smartcardio.CardChannel; 
import javax.smartcardio.CardTerminal; 
import javax.smartcardio.CommandAPDU; 
import javax.smartcardio.ResponseAPDU; 
import javax.smartcardio.TerminalFactory; 

import org.apache.commons.codec.binary.Hex; 

@SuppressWarnings("restriction") 
public class So39543402 { 

    public static void main(String[] args) throws Exception { 
       TerminalFactory factory = TerminalFactory.getDefault(); 
       List<CardTerminal> terminals = factory.terminals().list(); 
       CardTerminal terminal = terminals.get(0); 

       Card card = terminal.connect("*"); 
       CardChannel channel = card.getBasicChannel(); 

       String pse = "00A404000E325041592E5359532E444446303100"; 
       CommandAPDU apdu = new CommandAPDU(Hex.decodeHex(pse.toCharArray())); 
       exchangeApdu(channel, apdu); 

       apdu = new CommandAPDU(0x00, 0xB2, 0x01, 0x0C, 256); 
       exchangeApdu(channel, apdu); 
    } 

    private static ResponseAPDU exchangeApdu(CardChannel channel, CommandAPDU apdu) throws javax.smartcardio.CardException { 
     System.out.println("APDU: " + Hex.encodeHexString(apdu.getBytes())); 
     ResponseAPDU r = channel.transmit(apdu); 
     System.out.println("Response: " + Hex.encodeHexString(r.getBytes())); 
     return r; 
    } 

} 

請注意一些有趣的部分:

  • 此代碼使用Apache Commons Codec爲十六進制的轉換

  • 參數"*"用於CardTerminal.connect()比總是reque更通用蜇T=0協議

  • READ RECORD APDU進行了修改,預計256個字節的響應數據 - this is how this particular constructor works(您的代碼會產生一個ISO情況下,1命令APDU這可能不是你想要的)

,還應注意:

  • 不使用String.getBytes()方法不帶參數(即使你想單字符轉換爲字節)。總是指定所需的字符集(例如「US-ASCII」,「UTF-8」)

祝你好運!