Showing posts with label Decryption. Show all posts
Showing posts with label Decryption. Show all posts

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

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[]