2016-05-27 139 views
-2

我正在使用Java javax.smartcard库从智能卡(SIM卡)读取数据。但是我无法让我的代码工作来验证我的PIN码。这里是我的代码:使用APDU命令无法验证智能卡PIN码

public class CopyOfCardReader { 

public static final CommandAPDU SELECT_FILE_EEEE = new CommandAPDU(new byte[] { 0x00, (byte) 0xA4, 0x01, 0x0C, 0x02, (byte) 0xEE, (byte) 0xEE }); 
public static final CommandAPDU SELECT_MASTER_FILE = new CommandAPDU(new byte[] { 0x00, (byte) 0xA4, 0x00, 0x0C }); 
public static final CommandAPDU DISABLE_PIN = new CommandAPDU(new byte[] { 0x00, (byte) 0x26, 0x00, 0x00 }); 

public static void main(String[] args) { 

    try { 

     TerminalFactory factory = TerminalFactory.getDefault(); 
     List<CardTerminal> terminals = null; 

     terminals = factory.terminals().list(); 

     System.out.println("Terminals: " + terminals); 
     CardTerminal terminal = terminals.get(0); 
     Card card = terminal.connect("T=0"); 
     System.out.println("card: " + card); 
     ATR atr = card.getATR(); 
     System.out.print("ATR: "); 

     for (byte b : atr.getBytes()) { 
      System.out.print(b); 
     } 
     System.out.println(); 
     CardChannel channel = card.getBasicChannel(); 


     byte[] pin = "1234".getBytes(); 
     byte arg0 = (byte) 0x00; 
     byte arg1 = (byte) 0x20; 
     byte arg2 = (byte) 0x00; 
     byte arg3 = (byte) 0x01; 
     CommandAPDU command = new CommandAPDU(arg0, arg1, arg2, arg3, pin); 
     ResponseAPDU request = channel.transmit(command); 
     System.out.println("answer pin verify request: " + request.toString()); // response 

     ResponseAPDU rmaster = channel.transmit(SELECT_MASTER_FILE); 
     System.out.println("answer rmaster file: " + rmaster.toString()); // response 
     System.out.println(); 

     ResponseAPDU rPinDiable = channel.transmit(new CommandAPDU(new byte[] { (byte) 0x00, (byte) 0x26, 0x00, 0x00 })); 
     System.out.println("answer DISABLE PIN: " + rPinDiable.toString()); // response 

     ResponseAPDU rverify2 = channel.transmit(new CommandAPDU(0x00, 0x20, 0x00, 0x01, new byte[] { 0x31, 0x32, 0x33, 0x34, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF })); 
     System.out.println("answer rverify2 pin2: " + rverify2.toString()); // response 

     byte[] pin2 = "1234".getBytes(); 
     byte arg02 = (byte) 0x01; 
     byte arg12 = (byte) 0x26; 
     byte arg22 = (byte) 0x00; 
     byte arg32 = (byte) 0x01; 
     CommandAPDU command2 = new CommandAPDU(arg02, arg12, arg22, arg32, pin2); 
     ResponseAPDU request2 = channel.transmit(command2); 
     System.out.println("answer pin disable request: " + request2.toString()); // response 

     byte[] baReadUID = new byte[5]; 
     baReadUID = new byte[] { (byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; 
     command = new CommandAPDU(baReadUID); 
     ResponseAPDU r = channel.transmit(new CommandAPDU(new byte[] { 0X00, (byte) 0XA4, 0X00, 0X00, 0X02, 0X3F, 0X00 })); 
     System.out.println("answer r UID: " + r.toString()); // response 
     System.out.print("UUID: "); 
     for (byte b : r.getData()) { 
      System.out.print(b); // response 
     } 
     System.out.println(); 

     card.disconnect(false); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}} 

及以下为节目输出:

Terminals: [PC/SC terminal Gemalto USB SmartCard Reader 0] 
card: PC/SC card in Gemalto USB SmartCard Reader 0, protocol T=0, state OK 
ATR: 5959-106064-126-1110036-155151-1120 
answer pin verify request: ResponseAPDU: 2 bytes, SW=6708 
answer rmaster file: ResponseAPDU: 2 bytes, SW=9000 

answer DISABLE PIN: ResponseAPDU: 2 bytes, SW=6708 
answer rverify2 pin2: ResponseAPDU: 2 bytes, SW=6a88 
answer pin disable request: ResponseAPDU: 2 bytes, SW=6708 
answer r UID: ResponseAPDU: 37 bytes, SW=9000 
UUID: 9833-126212033-1252630-917-1281113-125236-15-11815-11731585-1-586-11210-12511 

阅读主文件和UUID是成功的,但所有其他指令都失败了。有人知道我的代码有什么问题吗?请帮忙。

+0

你的AID似乎是错的......'6A82'意思是“无法找到应用程序”。 – vojta

+0

好吧,我删除了该命令sellecting应用程序。除此之外,为什么我的PIN验证不工作? 为什么我得到错误6708“长度不正确”? –

回答

1

您一定要阅读GMS 11.11

关于VERIFY部分,尝试:

byte[] pin = { (byte)0x31, (byte)0x32, (byte)0x33, (byte)0x34, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF}; 

作为PIN是8个字节长与0xFF(被填充较短值0x310x320x33 ...字节表示ASCII字符'1''2''3' ...)。

这就是67XX状态字所告知的(在这种情况下,不正确的参数P3包含长度)。有关可能遇到的状态码,请参阅第9.4节。

的几点思考:

  • 你APDU命令应该有CLA字节为0xA0(即第一个字节 - 的arg0一个)。

  • 大部分其他命令都很奇怪 - 我建议遵循GSM 11.11。

  • 考虑用逗号分隔打印的字节(你现在得到的是不可读的)。

  • 当心你可以通过这么激烈的研究来破坏你的卡。

祝你好运!