gpt4 book ai didi

java - 向 PN532 (ACR122u) 发送 TgInitAsTarget 命令时出现 Apdu 错误

转载 作者:行者123 更新时间:2023-12-01 12:45:20 25 4
gpt4 key购买 nike

我正在尝试在卡模拟模式下将 TgInitAsTarget 命令发送到 PN532。当我尝试发送此消息时:

FF000000 27 D48C 05 0400 123456 20 000000000000000000000000000000000000 00000000000000000000 00 00

我收到 apdu 错误:

  • 线程“main”中出现异常 java.lang.IllegalArgumentException:apdu 的长度必须至少为 2 个字节
  • 位于 javax.smartcardio.ResponseAPDU.check(来源未知)
  • 位于 javax.smartcardio.ResponseAPDU。(来源未知)
  • 位于 sun.security.smartcardio.ChannelImpl.transmit(来源未知)

下面的代码是我使用的代码。这段代码有错吗?

//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、WriteRegister 和 SetParams 部分在 this 帖子之后给出了很好的响应。孔代码:

    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而不是*连接终端。请参阅this回答。第二件事是需要在读卡器顶部放置一张卡。当我将 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...

关于java - 向 PN532 (ACR122u) 发送 TgInitAsTarget 命令时出现 Apdu 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24761190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com