密码未初始化
问题描述:
我决定使用通用对象池来重复使用密码对象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 :[email protected]
<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...
}
答
我想你是在正确的道路上。我认为你需要在createNew()
方法来初始化Cipher
:
@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);
}
}
嘿,你能再次检查我的帖子?谢谢 – Tony
我查了。您正在询问有关如何使用密码的问题。我认为[这个问题](http://*.com/q/15554296/187808)很好地解释了它。 –
目前我有很多方法可以创建密码。我想使用通用对象池,因此可以重用对象。 – Tony