我用netbeans制作了java card的经典applet。
当我对读取操作进行编程时,我检查APDU中的第一个字节是否为0x80,第二个字节是否为0xB0,然后从文件中从字节2和3读取的偏移量,然后从字节4读取的字节数
将APDU作为默认APDU
0x80 0xB0 0x00 0x03 0x60从当前文件读取60个字节,从偏移量3开始
当我尝试此命令时,它返回错误Input data length != Lc around line 12。
经过多次重试,我发现了问题所在
问题是编译器假设字节4是数据的长度,所以在我的命令中,他等待60个字节
当我搜索时,我发现字节4并不意味着发送数据的长度,当INS=B0
我不知道为什么会这样,当我尝试调试编译器时,甚至没有进入进程函数。
我的脚本文件是
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;read函数是
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
}首先选择程序,然后选择文件,然后尝试读取不工作的数据
但是如果我执行0x80 0xB0 0x00 0x00 0x02 0x00 0x00,它将从偏移量0读取2字节写入,尽管即使在标准中也不使用最终的0x00 0x00
我的问题是,为什么我必须要在命令中将数据的长度设置为需要红色
如何修复此错误?
发布于 2012-11-15 22:39:32
首先,为什么使用0x80作为CLS字节?实际上,0x80是为全局平台命令保留的。如果你的卡是2G的,你应该使用0xA0,如果你的卡是3G的,最好使用0x0x (通常0x00用于通道0)。第二次读取的二进制APDU不在APDU中,这意味着P3正在指定预期的数据长度,即:
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 readP2 - is偏移量
P3 -是预期的长度,您不应该在后面指定任何其他字节。如果P3等于0,则将传出256字节的数据
有关所有详细信息,请查看标准ETSI TS 102 221 (http://pda.etsi.org/pda/home.asp?wki_id=,m5nDbNrlEWZbXcW5h86B) -您不需要帐户,只需输入电子邮件地址即可下载。
希望这能有所帮助。
KR,-Nodir
发布于 2012-11-20 03:58:38
您没有正确使用Java Card API。下面我添加了一段代码,这段代码是我在脑海中输入的。如果代码不能运行,请尝试并对其进行一些编辑。
void read() {
final APDU apdu = APDU.getCurrentAPDU();
final byte[] buf = apdu.getBuffer();
if(current.isDF()) {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
final EFile file = (EFile) current;
final short fileOffset = Util.getShort(buf, ISO7816.OFFSET_P1);
if (fileOffset < 0 || fileOffset >= file.length) {
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
}
// Ne (encoded by Le) is the *maximum* size of the response data
final short ne = apdu.setOutgoing();
// Re is the actual number of the bytes to be returned
final short fileDataLeft = file.length - fileOffset;
final short re = ne < fileDataLeft ? ne : fileDataLeft;
apdu.setOutgoingLength(re);
apdu.sendBytes(file.data, fileOffset, re);
}https://stackoverflow.com/questions/13378212
复制相似问题