AES-256 encryption with BouncyCastle Lightweight API

30,531

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.

Share:
30,531
DivineOmega
Author by

DivineOmega

Jordan is a passionate programmer, well-versed in a number of technical subjects. His main interests are in the fields of cryptocurrency, peer-to-peer networking and web applications, with a strong lean towards free open source software. He has a Computer Science degree with Honours and 5+ years of industry experience in web development, Linux server administration and IT support.

Updated on March 26, 2020

Comments

  • DivineOmega
    DivineOmega about 4 years

    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!