使用Android NFC将超过261个字节发送到Java卡 [英] Send more than 261 bytes to Java Card with Android NFC
问题描述
我想通过NFC(类 IsoDep
)将长度为1699字节的APDU发送到Java卡智能卡。我收到错误消息
I want to send an APDU with a length of 1699 bytes via NFC (class IsoDep
) to a Java Card smartcard. I get the error
java.io.IOException:收发长度超出了支持的最大值
java.io.IOException: Transceive length exceeds supported maximum
我的手机是三星Galaxy S7。
My phone is a Samsung Galaxy S7.
我在卡上的applet中使用了扩展长度。我已验证该卡支持扩展长度。我通过向卡发送一个4000字节的APDU通过pyapdutool对此进行了测试。
I use extended-length in my applet on the card. I have verified that the card supports extended length. I tested this via pyapdutool by sending an APDU with 4000 bytes to the card.
我发现在编写此代码时,结果为 false
:
I found that when I write this code, the result is false
:
final Tag t = (Tag) tag;
myTag = IsoDep.get(t);
boolean result = myTag.isExtendedLengthApduSupported();
我的清单中有此内容:
<activity
android:name=".test"
android:label="@string/title_test"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar" >
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
</intent-filter>
</activity>
如何通过Android NFC发送1699字节(或任何长度大于261字节的长度)的APDU
How can I send an APDU with 1699 bytes (or any length greater than 261 bytes) through Android NFC?
推荐答案
简短答案:您不能轻易做到这一点。
Short answer: You can't easily do that.
您已经发现 IsoDep
不支持设备上的扩展长度APDU(即 isoDep.isExtendedLengthApduSupported()
返回 false
)。实际上,这不是并不意味着您不能通过 IsoDep
对象发送扩展长度的APDU。实际上,这仅意味着 IsoDep
对象不会将扩展长度的APDU正确地分割为两个以上的ISO-DEP块,因此,长度超过261个字节的APDU将被假定超过了发送缓冲区的大小。您仍然应该能够发送长度小于< = 261字节的扩展长度APDU。
You already found out that IsoDep
does not "support" extended-length APDUs on your device (i.e. isoDep.isExtendedLengthApduSupported()
returns false
). In fact this does not mean that you can't send extended-length APDUs through the IsoDep
object. It actually only means that the IsoDep
object won't properly split extended-length APDUs across more than two ISO-DEP blocks and consequently an APDU that is longer than 261 bytes will be assumed to exceed the transmit buffer size. You should still be able to send extended-length APDUs with sizes <= 261 bytes.
所以 isoDep.isExtendedLengthApduSupported()
实际上指示您是否可以在一个ISO-DEP收发中发送超过261个字节。
So isoDep.isExtendedLengthApduSupported()
actually indicates if you can send more than 261 bytes in one ISO-DEP transceive.
您可以采取的克服此问题的措施是不使用 IsoDep
对象,而是在 NfcA $ c之上手动实现ISO-DEP(ISO / IEC 14443-4传输协议) $ c>对象(如果您的卡基于NFC-A / ISO / IEC 14443 Type A)或
NfcB
对象(如果您的卡基于NFC-B / ISO / IEC 14443 B型和(如果您的设备支持通过 NfcB
对象交换数据)。然后,您可以将扩展长度的APDU拆分为对于NFC控制器的收发缓冲区足够小的ISO-DEP块(通常为253个字节,包括标头字节,不包括CRC字节)。但是,自己处理ISO-DEP协议还意味着您必须注意正确的ISO-DEP激活,处理块编号,块确认,超时,等待时间扩展等。这相当复杂,尤其是超时由于Android NFC堆栈存在延迟,因此不容易观察。
What you could do to overcome this problem is to not use the IsoDep
object at all and, instead, manually implement ISO-DEP (the ISO/IEC 14443-4 transmission protocol) on top of the NfcA
object (if your card is based on NFC-A / ISO/IEC 14443 Type A) or the NfcB
object (if your card is based on NFC-B / ISO/IEC 14443 Type B and if your device supports exchanging data over the NfcB
object). You could then split your extended-length APDUs into ISO-DEP blocks that are small enough for the transceive buffer of the NFC controller (typically 253 bytes including the header bytes, excluding the CRC bytes). However, handling the ISO-DEP protocol yourself also means that you have to take care of proper ISO-DEP activation, of handling block numbering, block acknowledgements, timeouts, waiting-time extension, etc. Which is rather complicated, and particularly timeouts are not easy to observe due to the delays through the Android NFC stack.
这篇关于使用Android NFC将超过261个字节发送到Java卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!