我无法将数据写入字节数组中索引高于 128 的位置。 代码如下。
private void Write1(APDU apdu) throws ISOException
{
apdu.setIncomingAndReceive();
byte[] apduBuffer = apdu.getBuffer();
byte j = (byte)apduBuffer[4]; // Return incoming bytes lets take 160
Buffer1 = new byte[j]; // initialize a array with size 160
for (byte i=0; i<j; i++)
Buffer1[(byte)i] = (byte)apduBuffer[5+i];
}
它给我错误 6F 00(这意味着到达文件末尾)。
我正在使用:
- 智能卡类型=接触卡
- 使用 java card 2.2.2 和 jcop 使用 apdu
最佳答案
您的代码包含几个问题:
正如“pst”所指出的,您使用的是带符号的
byte
值,该值最多只能达到 128 - 请改用short
每次调用
Write1
方法时,您都会创建一个新缓冲区Buffer1
。在 JavaCard 上通常没有自动垃圾收集 - 因此内存分配应该只在安装应用程序时进行一次。如果您只想处理 adpu 缓冲区中的数据,只需从那里使用它。如果您想将数据从一个字节数组复制到另一个字节数组,最好使用javacard.framework.Util.arrayCopy(..)
。您正在调用
apdu.setIncomingAndReceive();
但忽略返回值。返回值为您提供了您可以读取的数据字节数。
以下代码来自API文档,展示了常用的方式:
short bytesLeft = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
if (bytesLeft < (short)55) ISOException.throwIt( ISO7816.SW_WRONG_LENGTH );
short readCount = apdu.setIncomingAndReceive();
while ( bytesLeft > 0){
// process bytes in buffer[5] to buffer[readCount+4];
bytesLeft -= readCount;
readCount = apdu.receiveBytes ( ISO7816.OFFSET_CDATA );
}
关于applet - 如何在JavaCard中存储大于128字节的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8470992/