使用READ BINARY读取超过256个字节 [英] Use READ BINARY to read more than 256 bytes

查看:356
本文介绍了使用READ BINARY读取超过256个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javax.smartcardio

I am trying to read a smartcard(German Gesundheitskarte) using javax.smartcardio

在EFPD的定义中,其长度指定为850字节。内容应该是一个经过gzip压缩的ISO5589-15编码的XML字符串,如此处

In the definition of the EF "PD" its length is specified as 850 bytes. The content should be a gzipped ISO5589-15 encoded XML string as specified here

作为CommandAPDU我发送

As CommandAPDU I send

00 B0 00 00 00

获取前256个字节。
发送后

to get the first 256 bytes. After sending

00 B0 00 FF 00

我得到接下来的256个字节。

I get the next 256 bytes.

但我如何得到其余的?

我如何知道二进制数据何时结束?

How will I know when the binary data ends?

德国规范第1部分 |
德国规范第2部分

推荐答案

READ BINARY APDU允许 2 字节用于文件偏移量,以P1和P2,并使用Le作为长度,对于 READ BINARY 响应中的字节数。 P1是高字节,或最高有效字节。然而,保留P1的最高位以指示P1是否还包含短文件标识符。它应保持在 0 的值,如果您已经在读取文件,则最大偏移量为32Ki - 1.

READ BINARY APDUs allow 2 bytes for the file offset, coded in P1 and P2, and use Le for the length, for READ BINARY the number of bytes in the response. P1 is the high byte, or the most significant byte. The topmost bit of P1 is however reserved to indicate if P1 also contains a short file identifier. It should remain at value 0 if you are already reading a file, leaving you with a maximum offset of 32Ki - 1.

我无法阅读您已链接的规格,但我们假设您的卡上的 READ BINARY APDU的工作方式相同。

I can't read the specs that you've linked but let's assume that the READ BINARY APDU on your card works the same way.

您读取前256个字节的命令似乎是正确的,注意 Le == 0x00 表示读取的是256个字节。

Your command to read the first 256 bytes seems correct, noting that Le==0x00 indicates a read for 256 bytes.

要读取从偏移量256,512等开始的字节,请开始递增P1,例如:

To read the bytes beginning at offset 256, 512, etc., start incrementing P1, e.g.:

00 B0 01 00 00
00 B0 02 00 00
00 B0 03 00 00

从偏移量257(0x101)开始读取256个字节:

To read 256 bytes beginning at offset 257 (0x101):

00 B0 01 01 00

抵消600(0x258):

Offset 600 (0x258):

00 B0 02 58 00

在您的代码中,如果您正在使用Java int 来存储偏移量,你通常最终会用这样的东西递增P1:

In your code, if you're using Java int to store the offset, you'll usually end up incrementing P1 with something like this:

int offset;
int P1, P2;

while (continueReading)
{
    // ...
    P1 = (offset >> 8) & 0xFF;
    P2 = offset & 0x00FF;
    // ...
    // send APDU
}

如何指示文件的大小取决于实现。通常,您可以从EF上的SELECT返回的文件控制信息(FCI)结构中获取文件大小( 00 A4 00 00 02 fileId )。但是,文件的大小也可以嵌入文件的内容中。如果可能,您不应该依赖状态字来告诉您文件的大小。

How the size of a file is indicated depends on the implementation. Usually you can get the file size from the File Control Information (FCI) structure returned by a SELECT on the EF (00 A4 00 00 02 fileId). The size of the file may however also be embedded in the contents of the file. If possible you should not rely on status words to tell you the size of the file.

添加:Le ,Ne和奇数INS

重要的是你只需要增加你在实际接收的字节数内的偏移量。响应数据(RDATA)。请注意,如果P3 = Le,则Le编码Ne,这是响应数据的最大大小。您可能会收到少于此数。

It's important that you only increase the offset with the amount of bytes that you actually receive within the response data (RDATA). Note that if P3 = Le that Le encodes Ne, which is the maximum size of the response data. You may receive less than that.

如果文件大小为32Ki或更大,那么您需要使用带有奇数INS的READ BINARY( B7 )读取32Ki以上的数据。在这种情况下,RDATA也可能包含开销。显然, - 反过来 - 可能会影响偏移计算和读取到文件末尾的计算。

If the file size is 32Ki or more then you need to use READ BINARY with odd INS (B7) to read the data above 32Ki. In that case the RDATA may also contain overhead. Obviously that - in turn - may influence the offset calculations and the calculations to read to then end of the file.

这篇关于使用READ BINARY读取超过256个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆