Rijndael support in Java

rbrayb picture rbrayb · Feb 25, 2009 · Viewed 38.9k times · Source

We have a requirement to do some Rijndael development in Java.

Any recommendations for articles, libraries etc. that would help us?

Any pointers to keystore maintenance and how store the keys securely?

Edit:

It would need to be open source. Essentially, it's just standard encrypt / decrypt of data using Rijndael.

Answer

Chochos picture Chochos · Mar 19, 2009

Java includes AES out of the box. Rijndael is AES. You don't need any external libraries. You just need something like this:

byte[] sessionKey = null; //Where you get this from is beyond the scope of this post
byte[] iv = null ; //Ditto
byte[] plaintext = null; //Whatever you want to encrypt/decrypt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
byte[] ciphertext = cipher.doFinal(plaintext);

And that's it, for encryption/decryption. If you are processing large amounts of data then you're better off reading chunks that are multiples of 16 bytes and calling update instead of doFinal (you just call doFinal on the last block).