Sunday, September 29, 2019

JAVA RSA Generate KeyPair, Encryption, Decryption and Performance Testing


To try RSA Enc/Dec and check its performance.

Simple Note

Test file


Performance testing


Move AESUtils.encrypt and AESUtils.decrypt to CryptoUtils since they are no different from RSA encrypt/decrypt.

Still wrap them in AESUtils/RSAUtils for convenience.
(Can import less class) (Just one class less)


Performance Testing

Test over
PublicKey/PrivateKey, Encryption/Decryption,
Create Cipher each time/Reuse Cipher,
Key Size 1024/2048/4096/8192 bits
total 32 cases, 100 runs each

Results listed in the google sheet
JAVA RSA Performance Testing

Not very accurate but...


1. PublicKey Enc/Dec is faster than PrivateKey Enc/Dec

2. Key Size affects, the longer the slower.

3. Reuse Cipher similar to create Cipher each time, sometimes even slower.


1. Use proper Key Size, 2048 probably a good choice.

2. Can create Cipher each time, no need to cache it.

3. Can also cache and reuse Cipher, will slightly increase performance in most cases.


Java: How can I generate PrivateKey from a string?

Load RSA public key from file

Friday, September 27, 2019

JAVA AES Performance Testing


Want to know the better choice of coding.

Simple Note

Test functions in

Also use a simple helper class for performance testing

Test Cases

100 runs each

1. testCreateInstanceEachTimeCommon: Simple AES with pre-generated key, no salt, create Cipher each time

2. testReuseInstanceCommon: Simple AES with pre-generated key, no salt, reuse Cipher

3. testCreateInstanceEachTimePBK: Use SecretKeyFactory and PBEKeySpec to generate SecretKey with PBKDF2WithHmacSHA256 algorithm, generate key and create Cipher each time

4. testReuseInstancePBK: Use SecretKeyFactory and PBEKeySpec to generate SecretKey with PBKDF2WithHmacSHA256 algorithm, reuse Cipher



1. Use pre-generated key is much faster
(testCreateInstanceEachTimeCommon is much faster than testCreateInstanceEachTimePBK).

2. Reuse Cipher is much faster.

3. Once the Cipher is created, the speed of Encryption are similar between Common and PBK
(the 90% time of testReuseInstancePBK even faster than testReuseInstanceCommon)


Do not create Cipher each time, try to reuse it.


Round double
Round a double to 2 decimal places

List Initial Size (need to get the fastest 90% so...)
Initial size for the ArrayList

Thursday, September 26, 2019

JAVA AES Encrypt Decrypt with Any Key, IV and Salt


Convenient, that's all

Simple Note

Test File:



Java MessageDigest supported algorithms? MD5, SHA-1, SHA-256
Class MessageDigest

How to Dupe a char array
Does calling clone() on an array also clone its contents?

Wednesday, September 25, 2019

JAVA AES Encryption Decryption with IV, Salt and any length of Key

Simple Note

See the method TestEncryptDecryptWithKeyIvSalt in

and getCipher(int mode, char[] key, byte[] ivBytes, byte[] saltBytes) in


Java 256-bit AES Password-Based Encryption

Java AES and using my own Key

Java AES Key Generation, Encryption and Decryption

Simple Note

Test File:

Utils Implementation

If you get Illegal key size error, probably need to download JCE Jars, please refer to

InvalidKeyException Illegal key size


Java Security: Illegal key size or default parameters?

Other References

The article teach me use char[] over String
Why is char[] preferred over String for passwords?

This one teach me basic Encryption and Decryption
Initial bytes incorrect after Java AES/CBC decryption

How to convert between char[] and byte[]
Converting char[] to byte[]

Monday, September 23, 2019

Use ExecutorService to Run Multiple Callable and Get Results In Order


ExecutorService can run multiple Runnable/Callable, but there is no method for get their Results / Exception in order
(Similar to Promise.all in JavaScript)

So try to build a simple solution.

Simple Note

Test / Sample
Implementation to Run Multiple Callable
Wrapper Class to wrap Callable

Thursday, September 19, 2019