Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES-256 encryption with BouncyCastle Lightweight API

I have written some (functional) AES encryption code using Java's built in encryption libraries, as follows, but I'd like to use a 256-bit key. However, I'd like to do this without the user having to install to Unlimited Strength Cryptography Policy files.

Now, I've heard that using the BouncyCastle Lightweight API can allow me to do this, but unfortunately I'm having a great deal of trouble getting my head around it, and am struggling to fit any documentation that helps me.

Here is a my current code, in which 'content' is the byte array to be encrypted:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
int keySize = 128;
kgen.init(keySize);
SecretKey key = kgen.generateKey();
byte[] aesKey = key.getEncoded();
SecretKeySpec aesKeySpec = new SecretKeySpec(aesKey, "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);
byte[] encryptedContent = aesCipher.doFinal(content);

How would I go about re-implementing this with the BouncyCastle Lightweight API? Can anyone help me out and/or point me in the direction of some simple example code?

I'm also interesting in any other solutions that allow 256-bit key AES encryption without the need for the user to install the unlimited strength policy files.

Many thanks!

like image 364
DivineOmega Avatar asked Mar 04 '12 12:03

DivineOmega


1 Answers

This question and answer is a useful starting point.

256bit AES/CBC/PKCS5Padding with Bouncy Castle

The next best place to look is the test code for the LW APIs and then the JCE Provider code. The JCE Provider code is a wrapper around the LW libraries - so if you want to know how to do it, that's the best place to see it.

By the JCE Provider code, I mean the BC implementation.

like image 171
Jon Avatar answered Oct 19 '22 16:10

Jon