[英]Cant verify smart card PIN using APDU command
我正在使用 Java javax.smartcard 庫從智能卡(SIM 卡)讀取數據。 但是我無法讓我的代碼來驗證我的 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 成功,但所有其他指令都失敗。 有人知道我的代碼有什么問題嗎? 請幫忙。
您絕對應該閱讀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
填充( 0x31
、 0x32
、 0x33
... 字節表示 ASCII 字符'1'
、 '2'
、 '3'
...)。
這就是67XX
狀態字所說的(在這種情況下保存長度的參數P3
不正確)。 有關您可能遇到的狀態代碼,請參閱第 9.4 節。
一些隨機筆記:
您的 APDU 命令應該將CLA
字節設置為0xA0
(即第一個字節arg0
字節)。
您的大多數其他命令都很奇怪——我建議遵循 GSM 11.11。
考慮用逗號分隔打印的字節(你現在得到的是不可讀的)。
請注意,如此激烈的研究可能會損壞您的卡。
祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.