APDU读取文件java卡程序

APDU读取文件java卡程序

本文介绍了APDU读取文件java卡程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用netbeans制作了java card classic applet

i made java card classic applet using netbeans

当我编写读取操作时,我检查APDU中的第一个字节是0x80然后第二个是 0xB0 然后从字节2和3获取我将从文件中读取的偏移量,然后从字节4中读取要读取的字节数

when i program the read operation i check for the first byte in the APDU to be 0x80 then the second to be 0xB0 then take the offset that i will read from in the file from the byte 2 and 3 then take the number of bytes to be read from byte 4

将APDU作为默认值

0x80 0xB0 0x00 0x03 0x60 this从偏移数字3开始读取当前文件中的60个字节

0x80 0xB0 0x00 0x03 0x60 this read 60 bytes from the current file starting by offset number 3

当我尝试此命令时它返回错误输入数据长度!= Lc在第12行附近

when i try this command it returned error Input data length != Lc around line 12.

在一些重试后我发现问题

after some retrying i find the problem

问题在于编译器假设字节4是数据的长度所以在我的命令中他等待60个字节

the problem is that the compiler assume that byte 4 is the length of data so in my command he wait for 60 bytes

当我搜索时我发现字节4并不意味着发送数据长度时 INS = B0

when i search i find that byte 4 does not mean the sending data length when the INS=B0

我不知道为什么会这样做,当我尝试调试时编译器甚至没有进入过程函数。

i do not know why it made like this and when i try to debug the compiler did not enter the process function even.

我的脚本文件是

0x00 0xA4 0x04 0x00 0X06 0X87 0XF1 0X3F 0X5E 0X22 0X47 0x7F;
0x80 0xA4 0x00 0x00 0x02 0x3F 0x00 0x7F;
0x80 0xA4 0x00 0x00 0x02 0x50 0x15 0x7F;
0x80 0xA4 0x00 0x00 0x02 0x53 0x00 0x7F;
0x80 0xA4 0x00 0x00 0x02 0x50 0x31 0x7F;
0x80 0xB0 0x00 0x00 0x33 0x7F ;
powerdown;

读取函数是

 void read(APDU apdu)
    {
        if(current.isDF())//can not read DF file
        {
            ISOException.throwIt((short)27014);
        }
        EFile f = (EFile)current;
       byte[]data=apdu.getBuffer();
        short offset = Util.getShort(data, (short)2);
        if(offset < 0 || offset > f.length)//can not read
        {
            ISOException.throwIt((short)27270);
        }
        data=apdu.getBuffer();
        short len = (short)(data[4]&0xFF);
       if(offset + len > f.length)//can not read
       {
            ISOException.throwIt((short)26368);
       }
        apdu.setOutgoing();
        apdu.setOutgoingLength(len);
        apdu.sendBytesLong(f.data, (short)(f.offset + offset),len);//return the data

    }

第一个选择程序,然后选择文件,然后尝试读取无效的数据

the first one to select the program and then select files and then try to read data that is not work

但如果我做
0x80 0xB0 0x00 0x00 0x02 0x00 0x00 它读取2个字节从偏移0写入,尽管最后甚至在标准中也没有使用0x00 0x00

but if i do0x80 0xB0 0x00 0x00 0x02 0x00 0x00 it read 2 bytes write from offset 0 although the final 0x00 0x00 is not used even in the standard

我的问题为什么我必须将命令中的数据作为所需数据的长度是红色的

my problem why i must to but data in the command to be as length of the data needed to be red

我该如何解决这个错误?

how can i fix this error?

推荐答案

首先,为什么使用0x80作为CLS字节?实际上0x80是为Global Platform命令保留的。如果您的卡是2G,您应该使用0xA0,如果您的卡是3G,最好使用0x0x(通道0通常为0x00)。第二个 - 读取二进制APDU是OUT APDU,这意味着P3指定了预期的数据长度,即:

First of all why are you using 0x80 as CLS byte? Actually 0x80 is reserved for Global Platform commands. If your card is 2G you should use 0xA0 and if your card is 3G better to use 0x0x (usually 0x00 for channel 0). The second - Read Binary APDU is OUT APDU it means the P3 is specifying expected data length i.e.:

0x00 0xB0 P1 P2 P3

其中:P1编码:

|   b8   |   B7   |   b6   |   b5   |   b4   |   b3   |   b2   |   b1   |   Meaning
----------------------------------------------------------------------------------------------------------------------
|   0    |   X    |   X    |   X    |   X    |   X    |   X    |   X    | b7-b1 is the offset to the first byte
|        |        |        |        |        |        |        |        | to read – P2 is the low part of the offset
----------------------------------------------------------------------------------------------------------------------
|   1    |   0    |   0    |   X    |   X    |   X    |   X    |   X    | SFI referencing used,  b1-b5 are the SFI
|        |        |        |        |        |        |        |        | and P2 is the offset to the first byte to read

P2 - 偏移量

P3 - 预期长度,之后不应指定任何其他字节。如果P3等于0,那么将传输256字节的数据

P3 - is expected length and you should not specify any other bytes after. If P3 is equal to 0 then 256 bytes of data will be transferred out

有关所有细节,请查看标准ETSI TS 102 221(http://pda.etsi.org /pda/home.asp?wki_id=,m5nDbNrlEWZbXcW5h86B) - 您不需要帐户,只需输入电子邮件地址即可下载。

For all details please check standard ETSI TS 102 221 (http://pda.etsi.org/pda/home.asp?wki_id=,m5nDbNrlEWZbXcW5h86B) - you don't need account, just enter email address and you'll be able to download it.

希望它会有所帮助。

KR,
-Nodir

KR,-Nodir

这篇关于APDU读取文件java卡程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:56