2017-04-20 13 views
-2

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... 
      } 

答えて

1

あなたは正しい経路にいると思います。ねえ、あなたが再び私のポストを確認することができます

@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); 
    } 
} 
+0

:私はあなたがcreateNew()方法でCipherを初期化する必要があると思いますか?ありがとう – Tony

+0

私はチェックした。あなたはCipherの使い方について質問しています。私は[このQ&A](http://stackoverflow.com/q/15554296/187808)がうまく説明していると思います。 –

+0

現在私はCipherを作成する多くの方法があります。オブジェクトを再利用できるようにGeneric Object Poolを使用したい。 – Tony

関連する問題