If someone else reads this, there will be no need to reinitialize the cipher for each call using RSA. Although the cipher pool can be used to improve performance.
I checked the quick boot to check this out.
It seems that it is enough to synchronize on cipher.doInit() and use one instance of Cipher for decryption.
private static Queue<String> results = new ConcurrentLinkedQueue<String>(); @Test public void test() throws InterruptedException { String plainText = "some text to verify data integrity"; String encryptedText = Encrypt.encrypt(plainText); for (int i = 0; i < 5000; i++) { new Thread( () -> { results.add(Decrypt.decrypt(encryptedText)); }) .start();; } Thread.sleep(5000); assertTrue(results.size() == 5000); while(!results.isEmpty()) { assertTrue(results.poll().equals(plainText)); } }
source share