密码未初始化

时间:2017-04-20 15:27:17

标签: java generics

我决定使用通用对象池来跟随A Generic and Concurrent Object Pool 重用密码对象。不同之处在于文章使用的是Connection,但我使用的是Cipher

Eracom

Pool<Cipher> pool = new BoundedBlockingPool<Cipher>(5, new CipherPickerValidator(), new CipherPicker("xxxx"));  

     public Key unwrapKey(byte[] tmkByte) throws Exception {
            Cipher cipher = pool.get();
            System.out.println("Cipher :" + cipher);
            try{
            cipher.init(Cipher.DEPT_MODE, mkkey, alSpec);
            }catch(Exception e)
            {
                System.out.println(e);

            }
            byte[] de = cipher.doFinal(tmkByte);
            SecretKey tmk = new SecretKeySpec(de, "De");
            return tmk;
        }

BoundedBlockingPool

public BoundedBlockingPool(
        int size,
        Validator<T> validator,
        ObjectFactory<T> objectFactory) {
    super();

    this.objectFactory = objectFactory;
    this.size = size;
    this.validator = validator;

    objects = new LinkedBlockingQueue<T>(size);

    initializeObjects();

    shutdownCalled = false;
}

@Override
public T get(long timeOut, TimeUnit unit) {
    if (!shutdownCalled) {
        T t = null;

        try {
            t = objects.poll(timeOut, unit);

            return t;
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        return t;
    }

    throw new IllegalStateException(
            "Object pool is already shutdown");
}

@Override
public T get() {
    if (!shutdownCalled) {
        T t = null;

        try {
            t = objects.take();
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        return t;
    }

    throw new IllegalStateException(
            "Object pool is already shutdown");
}

@Override
public void shutdown() {
    shutdownCalled = true;

    executor.shutdownNow();

    clearResources();
}

private void clearResources() {
    objects.stream().forEach((t) -> {
        validator.invalidate(t);
    });
}

@Override
protected void returnToPool(T t) {
    if (validator.isValid(t)) {
        executor.submit(new ObjectReturner(objects, t));
    }
}

@Override
protected void handleInvalidReturn(T t) {

}

@Override
protected boolean isValid(T t) {
    return validator.isValid(t);
}

private void initializeObjects() {
    for (int i = 0; i < size; i++) {
        objects.add(objectFactory.createNew());
    }
}

private class ObjectReturner<E>
        implements Callable<Void> {
    private final BlockingQueue<E> queue;
    private E e;

    public ObjectReturner(BlockingQueue<E> queue, E e) {
        this.queue = queue;
        this.e = e;
    }

    @Override
    public Void call() {
        while (true) {
            try {
                queue.put(e);
                break;
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
            }
        }

        return null;
    }
}

CipherPicker

public CipherPicker(String instances) {
    super();
    this.instance = instances;
}

@Override
public Cipher createNew() {
    try {
        System.out.print("Instances : " + instance);
        return Cipher.getInstance(this.instance);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException se) {
        throw new IllegalArgumentException(
                "Unable to create new cipher", se);
    }
}

错误

Cipher :javax.crypto.Cipher@45c74f8
<log realm="GenerateIPEK" at="Thu Apr 20 18:20:27.737 MYT 2017">
  <error>
    <exception name="Cipher not initialized">
    java.lang.IllegalStateException: Cipher not initialized
    at javax.crypto.Cipher.checkCipherState(Cipher.java:1750)
    at javax.crypto.Cipher.doFinal(Cipher.java:2157)
    at com.rh.host.EracomJCE.unwrapKey(EracomJCE.java:65)
    at com.rh.host.tm.GenerateIPEK.doPrepare(GenerateIPEK.java:70)
    at com.rh.host.tm.TxnSupport.prepare(TxnSupport.java:36)
    at org.jpos.transaction.TransactionManager.prepare(TransactionManager.java:473)
    at org.jpos.transaction.TransactionManager.prepare(TransactionManager.java:526)
    at org.jpos.transaction.TransactionManager.run(TransactionManager.java:257)
    at java.lang.Thread.run(Thread.java:745)
    </exception>
  </error>

有人能告诉我这样做的正确方法是什么?被困了好几天了。我在正确的道路上吗?非常感谢!

修改

假设我有两种方法,将在此之后调用。所以我应该这样写?

    public Key unwrapKey(byte[] tmkByte) throws Exception {
                Cipher cipher = pool.get();
                byte[] de = cipher.doFinal(tmkByte);
                SecretKey tmk = new SecretKeySpec(de, "De");
                return tmk;    
            }

   public Key wrapKey(byte[] tByte) throws Exception {
                Cipher cipher = pool.get();  
                // byte,SecretKey...  
            }

1 个答案:

答案 0 :(得分:1)

我认为你正走在正确的道路上。我认为您需要在Cipher方法中初始化createNew()

@Override
public Cipher createNew() {
    try {
        System.out.print("Instances : " + instance);
        Cipther result = Cipher.getInstance(this.instance);
        result.init(...); // use the right init params, e.g. opmode, crypto key
        return result;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException se) {
        throw new IllegalArgumentException(
                "Unable to create new cipher", se);
    }
}