Java AES 256 Encryption Decryption Example

Introduction

Advanced encryption standard (AES) is the most secure encryption standard compared to RSA, which is vulnerable to brute force attacks.

This is the main reason that AES was established by the National Institute of Standard and Technology (NIST) in 2001 and is used worldwide to date.

In the previous post, we have seen how to implement AES 128 encryption and decryption algorithm. In this tutorial, you will learn how to encrypt and decrypt data using AES 256, which works the same way as AES 128 with few minor changes, and its implementation has been covered in another tutorial.

When working with AES, we have to provide three inputs that will be used in the encryption process, and they include the message, secret key, an initialization vector (IV).

The block cipher algorithm uses permutations to process the data, and the more the key size, the more the permutations.

AES 256 algorithm takes a 128-bit input data and processes it in 14 rounds, and with the possibility of breaking the AES algorithm being feasible, the use of AES 256 is considered compared to 128 and 192 as it requires high computing power to exploit which most users do not have.

Generate a shared key

We can use several ways to generate a key in java, and we will see the getInstance() method of the KeyGenerator class in java.

The method accepts a string representing the algorithm to be used in the encryption and decryption process, and we only need to pass string AES as the parameter.

This class will create an instance of the RSA transformation with a default key length of 128 bits. However, we want our algorithm to use a key length of 256 bits and to achieve this call the init() method from the KeyGenerator object we have just created and pass an in value of 256.

The generateKey() method will return a SecretKey, a Key that we will use to encrypt and decrypt because it is shared in both functionalities.

Convert the generated key to string using the encodeToString() method of Base64.Encoder and log the result to the console to verify that the key was generated.

Output:

generated key = X0M+BcA9o4RudG4H46L0qK8wJyyfWVynA0FKA8Awgmg=

Encrypt a random text

Create a Cipher object using the getInstance() method and pass AES/CBC/PKCS5Padding, representing the transformation we want to perform.

There are other forms of modes and padding that can be used with the AES transformation, and to achieve this, you can have a look at Java Security Standard Algorithm Names Specification

Since we are using cipher block chaining mode, we need to provide an initialization vector. To achieve this, create IvParameterSpec object and pass a new array of bytes of size 16 to its constructor.

The byte array of size 16 makes it equal to a block size of 128 bits, and you should create a new one when working with a different message.

Call the init() method of the cipher object and pass Cipher.ENCRYPT_MODE to indicate that we want to encrypt a message, the SceretKey we generated, and the IvParameterSpec object.

The doFinal() method encrypts the message and returns an array of bytes that we convert to a string and log to the console to verify that the message was encrypted.

Output:

generated key = P+g/Lg9VJAxwGdI7rulS83mbs2LXHMcW5SMxzuOLQJ4=

Encrypted message = 8cOldh6oBNdzn6ah6iz+hg==

Decrypt the encrypted text

To decrypt the encrypted text, we only need to create a new Cipher object as we did when encrypting a message and call the init() method.

The only method parameter that changes is the mode since we are performing decryption, and the first parameter should be changed to Cipher.DECRYPT_MODE.

When we call the doFinal() method and pass the encrypted message, it decrypts and returns an array of bytes.

The data returned represents bytes of our decrypted message, and we can convert the message to a string by creating a new string object and passing it to the constructor.

To verify that our message was decrypted print the results of the new string to the console, and you will see the original message that we created.

Output:

generated key = P+g/Lg9VJAxwGdI7rulS83mbs2LXHMcW5SMxzuOLQJ4=

Encrypted message = 8cOldh6oBNdzn6ah6iz+hg==

decrypted message =Hello world

Conclusion

In this tutorial, you have learned how to encrypt and decrypt a random text using AES 256, a popular encryption standard used worldwide. You have also learned how to add a custom key size using the init() method of the KeyGenerator, and this method also works for 128 and 192 key sizes. Feel free to check out the other articles on AES and RSA encryption and decryption to learn how they work.

Related Posts

  • 16 October

    Java AES Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction AES stands for advanced encryption standard and is the most commonly used symmetric algorithm to encrypt sensitive data and can be used in both software and hardware. The AES algorithm is symmetric, meaning that it uses only one key for encryption and […]

  • 11 September

    RSA Encryption and Decryption in Java

    Table of ContentsIntroductionGenerate RSA key pairEncrypt a random textDecrypt the random textConclusion Introduction RSA is a short form for Rivest, Shamir, and Adleman, are the people who first publicly described it in 1977. It is an algorithm for asymmetric cryptography which involves the use of two keys. A public key, which can be known to […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.