JavaCard中如何存储大于128字节的数据

发布于 2024-12-20 20:52:09 字数 546 浏览 5 评论 0原文

我无法在字节数组中索引高于 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卡2.2.2的接触卡和使用apdu的jcop

I can't write data at index above 128 in byte array.
code is given below.

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];
}

It gives me error 6F 00 (It means reach End Of file).

I am using:

  • smart card type = contact card
  • using java card 2.2.2 with jcop using apdu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云归处 2024-12-27 20:52:09

您的代码包含几个问题:

  1. 正如 'pst' 已经指出的那样,您使用的是带符号的 byte 值,该值最多只能工作到 128 - 请改用 short< /p>

  2. 您正在创建一个每个新缓冲区 Buffer1调用您的 Write1 方法。在 JavaCard 上通常没有自动垃圾收集 - 因此内存分配只应在应用程序安装时进行一次。如果您只想处理 adpu 缓冲区中的数据,只需从那里使用它即可。如果您想将数据从一个字节数组复制到另一个字节数组中,最好使用 javacard.framework.Util.arrayCopy(..)。

  3. 您正在调用 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 );
}

Your code contains several problems:

  1. As already pointed out by 'pst' you are using a signed byte value which works only up to 128 - use a short instead

  2. Your are creating a new buffer Buffer1 on every call of your Write1 method. On JavaCard there is usually no automatic garbage collection - therefore memory allocation should only be done once when the app is installed. If you only want to process the data in the adpu buffer just use it from there. And if you want to copy data from one byte array into another better use javacard.framework.Util.arrayCopy(..).

  3. You are calling apdu.setIncomingAndReceive(); but ignore the return value. The return value gives you the number of bytes of data you can read.

The following code is from the API docs and shows the common way:

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 );
}
笙痞 2024-12-27 20:52:09
short j = (short) apdu_buffer[ISO7816.OFFSET_LC] & 0xFF
short j = (short) apdu_buffer[ISO7816.OFFSET_LC] & 0xFF
枕头说它不想醒 2024-12-27 20:52:09

详细阐述 pst 的答案。一个字节有 2^8 位数字,或者更确切地说是 256 位数字。但如果您使用有符号数字,它们将在一个循环中工作。因此,128 实际上将是 -128,129 将是 -127,依此类推。

Elaborating on pst's answer. A byte has 2^8 bits numbers, or rather 256. But if you are working with signed numbers, they will work in a cycle instead. So, 128 will be actually -128, 129 will be -127 and so on.

蝶…霜飞 2024-12-27 20:52:09

更新:虽然以下答案对于普通 Java 是“有效”的,但请参阅 Roberts 答案以获取 Java 卡特定信息以及其他问题/方法。


在 Java 中,byte 的值在 [-128, 127] 范围内,因此,当您说“160”时,这不是代码的内容真的给了你:)

也许你想使用:

int j = apduBuffer[4] & 0xFF;

将值apduBuffer[4]“向上转换”为int,同时将原始字节数据视为< em>未签名 价值。

同样,i 也应该是 int (以避免讨厌的溢出和循环永远错误),并且 System.arraycopy 方法也很方便...

(我不知道这是否是唯一/真正的问题 - 或者上面的方法是否是 Java Card - 但这肯定是一个问题并且符合“128限制” ”提到。)

快乐编码。

Update: While the following answer is "valid" for normal Java, please refer to Roberts answer for Java Card-specific information, as well additional concerns/approaches.


In Java a byte has values in the range [-128, 127] so, when you say "160", that's not what the code is really giving you :)

Perhaps you'd like to use:

int j = apduBuffer[4] & 0xFF;

That "upcasts" the value apduBuffer[4] to an int while treating the original byte data as an unsigned value.

Likewise, i should also be an int (to avoid a nasty overflow-and-loop-forever bug), and the System.arraycopy method could be handy as well...

(I have no idea if that is the only/real problem -- or if the above is a viable solution on a Java Card -- but it sure is a problem and aligns with the "128 limit" mentioned.)

Happy coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文