Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java卡中的AESKey链接列表/数组_Java_Applet_Cryptography_Javacard - Fatal编程技术网

Java卡中的AESKey链接列表/数组

Java卡中的AESKey链接列表/数组,java,applet,cryptography,javacard,Java,Applet,Cryptography,Javacard,这篇文章与我几天前提出的一个问题有关: 我想在Java卡中实现LinkedList,以存储AESKey。因此,我以这种方式编写了一个类KeyElement: package LinkedList; import javacard.security.AESKey; import javacard.security.KeyBuilder; class KeyElement { private KeyElement next; private short id; priv

这篇文章与我几天前提出的一个问题有关:

我想在Java卡中实现
LinkedList
,以存储
AESKey
。因此,我以这种方式编写了一个类
KeyElement

package LinkedList;

import javacard.security.AESKey;
import javacard.security.KeyBuilder;


class KeyElement {
    private KeyElement next;
    private short id;
    private AESKey key;
    private boolean isUsed;

    /**
     * Constructor.
     * 
     * @param upperBound
     *            An upper bound to indicate of many elements it cans contain at
     *            maximum. It is essential to instanciate the structure this way
     *            to reserve all the necessary memory at the installation time.
     */
    public KeyElement(short upperBound) {
       this.id = (short) 0x0000;
       this.key = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
           KeyBuilder.LENGTH_AES_256, false);
       this.isUsed = false;
       this.next = null;
       for (short i = (short) 0x0001; i < upperBound; i++) {
           KeyElement e = new KeyElement();
           this.addKeyElement(e);
       }
    }

    public KeyElement() {
       this.id = (short) 0x0000;
       this.key = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
           KeyBuilder.LENGTH_AES_256, false);
       this.isUsed = false;
       this.next = null;
    }

    public KeyElement getNext() {
       return this.next;
    }

    public short getID() {
       return this.id;
    }

    public boolean isUsed() {
       return isUsed;
    }

    public void setNext(KeyElement e) {
       this.next = e;
    }

    public static boolean addKey(KeyElement main, final short id, byte[] key) {
       for (KeyElement p = main; p != null; p = p.getNext()) {
            if (!p.isUsed()) {
              p.isUsed = true;
              p.id = id;
              p.key.setKey(key, (short) 0x0000);
              return true;
           }
       }
       return false;
    }



    public void addKeyElement(KeyElement e) {
       e.setNext(this.getNext());
       this.setNext(e);
    }


    public static boolean getKey(final KeyElement e, final short id,
        byte[] key) {
       for (KeyElement p = e; p != null; p = p.getNext()) {
           if (p.id == id) {
              p.key.getKey(key, (short) 0x0000);
              return true;
           }
       }
       return false;
    }
}
所以我写下了if/else条件,以确定需要多少时间:

  • 填写列表(
    INS
    =0x00)
  • 获取列表的最后一个元素(当
    INS
    =0x01时)
  • 获取列表的第一个元素(当
    INS
    =0x02时)
因此,使用上面给出的代码,我可以在Java卡上安装我的小程序而不会出现问题

但是当我运行applet时,我得到一个异常
0x0D00
,它引用了一个
NullPointerException

那么,什么?我是否必须断定我的
KeyElement
对象实例化失败?但是,由于它是在安装时生成的,我认为它应该在安装过程中向我返回一个错误,但它没有

更准确地说,它在步骤失败:

// Send result.
 try {
   if (apduBuffer[ISO7816.OFFSET_INS] == (byte) 0x00) {
      for (short i = 0x0000; i < (short) 0x0008; i++) {
         rand.generateData(rdm, (short) 0x0000, (short) rdm.length);
         KeyElement.addKey(e, i, rdm);
      }
...
//发送结果。
试一试{
if(apduBuffer[ISO7816.OFFSET_INS]==(字节)0x00){
对于(短i=0x0000;i<(短)0x0008;i++){
生成的随机数(rdm,(短)0x0000,(短)rdm.length);
addKey(e,i,rdm);
}
...

更准确地说,在
keyement.addKey(e,i,rdm);
这就是为什么
e
似乎会引发
NullPointerException
即使安装成功了…

我也没有详细浏览所有代码,因为你没有真正了解链接列表的要点

public KeyElement(short upperBound)

查看一些示例Java链表实现,并通过捕获异常或在分配前检查剩余空间来确保EERPOM使用的安全性

您尚未创建生成
NullPointerException
rand
实例。您需要使用:

RandomData.getInstance(byte algorithm) 
用算法

public static final byte    ALG_PSEUDO_RANDOM   1
public static final byte    ALG_SECURE_RANDOM   2

另外,请参阅在哪一步出现异常?安装,指令值?并请使用缩进。异常是用指令值引发的。我编辑了我的问题以精确说明它。我认为实现此构造函数是避免执行时内存泄漏的一个好方法。如果有固定的上限,则基本上没有区别到ArrayInDect,但由于我不能在Java卡中使用AESKey数组,它仍然允许有一个结构来处理它。但你是对的,我将重命名我的帖子。
public static final byte    ALG_PSEUDO_RANDOM   1
public static final byte    ALG_SECURE_RANDOM   2