我试图在卡仿真模式下将TgInitAsTarget命令发送到PN532。当我试图发送这个:
FF000000 27 D48C 05 0400 123456 20 000000000000000000000000000000000000 00000000000000000000 00 00
我得到一个apdu错误:
- 线程"main"异常java.lang.IllegalArgumentException: apdu必须至少2字节长 在javax.smartcardio.ResponseAPDU
- 。检查(未知源) 在javax.smartcardio.ResponseAPDU
- 。(未知源) 在sun.security.smartcardio.ChannelImpl
- 。传输(未知来源)
下面的代码是我使用的代码。这个代码有问题吗?
//TgInitAsTarget
byte[] bytes4 = { (byte) 0xD4,(byte) 0x8C,(byte) 0x05,(byte) 0x04,(byte) 0x00,
(byte) 0x12,(byte) 0x34,(byte) 0x56,(byte) 0x20,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00 };
CommandAPDU command4 = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes4);
ResponseAPDU response4 = channel.transmit(command4);
System.out.println(bytesToHex(response4.getBytes()));
它看起来像Readregister, writereregister和SetParams部分给出了一个很好的响应后这篇文章。孔码:
List<CardTerminal> terminals = null;
TerminalFactory factory = TerminalFactory.getDefault();
terminals = factory.terminals().list();
CardTerminal terminal = terminals.get(0);
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
//Read register
byte[] bytes1 = {(byte) 0xD4, 0x06, 0x63, 0x05, 0x63, 0x0D, 0x63, 0x38 };
CommandAPDU command1 = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes1);
ResponseAPDU response1 = channel.transmit(command1);
System.out.println(bytesToHex(response1.getBytes()));
//Update registers
int xx = 47;
int yy = 00;
int zz = 01;
xx = xx | 0x004;
yy = yy & 0x0EF;
zz = zz & 0x0F7;
byte xxByte = toByte(hexStringToByteArray("0x"+Integer.toHexString(xx)));
byte yyByte = toByte(hexStringToByteArray("0x0"+Integer.toHexString(yy)));
byte zzByte = toByte(hexStringToByteArray("0x0"+Integer.toHexString(zz)));
//WriteRegister
byte[] bytes2 = { (byte) 0xD4, 0x08, 0x63, 0x02, (byte) 0x80,
0x63, 0x03, (byte) 0x80, (byte) 0x63, (byte) 0x05,
xxByte, 0x63, 0x0D,yyByte, 0x63, 0x38, zzByte};
CommandAPDU command2 = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes2);
ResponseAPDU response2 = channel.transmit(command2);
System.out.println(bytesToHex(response2.getBytes()));
//SetParameters
byte[] bytes3 = {(byte) 0xD4,(byte) 0x12,(byte) 0x30};
CommandAPDU command3 = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes3);
ResponseAPDU response3 = channel.transmit(command3);
System.out.println(bytesToHex(response3.getBytes()));
//TgInitAsTarget
byte[] bytes4 = { (byte) 0xD4,(byte) 0x8C,(byte) 0x05,(byte) 0x04,(byte) 0x00,
(byte) 0x12,(byte) 0x34,(byte) 0x56,(byte) 0x20,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00 };
CommandAPDU command4 = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes4);
ResponseAPDU response4 = channel.transmit(command4);
System.out.println(bytesToHex(response4.getBytes()));
任何帮助,回应或建议是感激的!!提前感谢!
终于有了解决办法。我将回答我自己的问题,以防其他人有同样的问题。
我将端子连接到direct
而不是*
。看看这个答案。第二件事是需要在读卡器的顶部放置一张卡片。当我把Galaxy 5放在阅读器上时,它的工作代码如下:
byte[] commandAPDU4 = {(byte)0xFF, 0x00, 0x00, 0x00 , 0x27 , (byte)0xD4, (byte)0x8C , 0x05 , 0x04, 0x00 , 0x12, 0x34, 0x56 , 0x20 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00};
byte[] responseAPDU4 = card.transmitControlCommand(IOCTL_SMARTCARD_ACR122_ESCAPE_COMMAND, commandAPDU4 );
System.out.println(bytesToHex(responseAPDU4) + "...");
//Response: D58D08E0809000...