Sunday, September 29, 2019

JAVA RSA Generate KeyPair, Encryption, Decryption and Performance Testing


Motivation

To try RSA Enc/Dec and check its performance.

Simple Note

Test file TestRSAUtils.java

Implementation RSAUtils.java

Performance testing TestRSAPerformance.java

Refactoring

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)

Hmm...


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

Summary

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.

Conclusions

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.

References

Java: How can I generate PrivateKey from a string?

Load RSA public key from file

Friday, September 27, 2019

JAVA AES Performance Testing


Motivation

Want to know the better choice of coding.

Simple Note

Test functions in
TestAESPerformance.java

Also use a simple helper class for performance testing
PerformanceTestHelper.java


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

Results



Summary

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)

Conclusion

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

References

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


Motivation

Convenient, that's all

Simple Note

Test File:
  TestAESImpl.java


Implementation:
  AESImpl.java


References

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 TestAESUtils.java

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

References

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:
  TestAESUtils.java

Utils Implementation
  AESUtils.java
  CryptoUtils.java

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

InvalidKeyException Illegal key size

and

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

Motivation

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
  ConcurrentParallel.java
Implementation to Run Multiple Callable
  CallableParallelExecutor.java
Wrapper Class to wrap Callable
  CallableWrapper.java



Thursday, September 19, 2019