Java Symmetric AES Encryption Decryption using JCE

In this tutorial we will learn about AES symmetric encryption decryption using Java Cryptography Extension (JCE). In the previous tutorial we saw about encryption decryption using DES symmetric key algorithm. “Data Encryption Standard (DES)” is prone to brute-force attacks. It is a old way of encrypting data. It is replaced by “Advanced Encryption Standard (AES)”.

Main issue with DES was the short encryption key size. Shorter the key, it is easier to break it with brute force attack. AES can use 128, 192 or 256 bit encryption. The block size used by DES is 64 bits and by AES is 128 bits. Third difference is AES uses permutation substitution over the Feistel network used by DES.

If you are looking to choose between DES or AES for your real time application, AES is the way to go.

Example AES Symmetric Key Encryption Decryption

In comparison from the previous tutorial there are only two changes in the example program. One is the AES encryption and another is Base64 encoding.

If it were before Java 8, I would have used the Apache commons-code bundle for Base64 encoding. In Java 8 we have got new classes in java.util package for Base64 encoding and decoding. It is important to encode the binary data with Base64 to ensure it to be intact without modification when it is stored or transferred.


import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncryptionDecryptionAES {
	static Cipher cipher;

	public static void main(String[] args) throws Exception {
		KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
		SecretKey secretKey = keyGenerator.generateKey();
		cipher = Cipher.getInstance("AES");

		String plainText = "AES Symmetric Encryption Decryption";
		System.out.println("Plain Text Before Encryption: " + plainText);

		String encryptedText = encrypt(plainText, secretKey);
		System.out.println("Encrypted Text After Encryption: " + encryptedText);

		String decryptedText = decrypt(encryptedText, secretKey);
		System.out.println("Decrypted Text After Decryption: " + decryptedText);

	public static String encrypt(String plainText, SecretKey secretKey)
			throws Exception {
		byte[] plainTextByte = plainText.getBytes();
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		byte[] encryptedByte = cipher.doFinal(plainTextByte);
		Base64.Encoder encoder = Base64.getEncoder();
		String encryptedText = encoder.encodeToString(encryptedByte);
		return encryptedText;

	public static String decrypt(String encryptedText, SecretKey secretKey)
			throws Exception {
		Base64.Decoder decoder = Base64.getDecoder();
		byte[] encryptedTextByte = decoder.decode(encryptedText);
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
		String decryptedText = new String(decryptedByte);
		return decryptedText;

Example Program Output

Plain Text Before Encryption: AES Symmetric Encryption Decryption
Encrypted Text After Encryption: sY6vkQrWRg0fvRzbqSAYxepeBIXg4AySj7Xh3x4vDv8TBTkNiTfca7wW/dxiMMJl
Decrypted Text After Decryption: AES Symmetric Encryption Decryption

This Java tutorial was added on 02/11/2014.

Comments on "Java Symmetric AES Encryption Decryption using JCE" Tutorial:

  1. […] is the latest encryption standard over the DES. You can refer the encryption decryption with AES symmetric algorithm using JCE tutorial. All the above given steps and concept are same, we just replace the DES with […]

  2. […] This tutorial is a continuation of our Java security series. In the previous tutorial we saw about symmetric AES encryption and decryption using Java Cryptography Extension (JCE). […]

  3. hassan shahzad aheer says:

    when i change keyGenerator.init(128); to keyGenerator.init(192); or keyGenerator.init(256);

    it throws exception

    Exception in thread “main” Illegal key size or default parameters
    at javax.crypto.Cipher.checkCryptoPerm(
    at javax.crypto.Cipher.implInit(
    at javax.crypto.Cipher.chooseProvider(
    at javax.crypto.Cipher.init(
    at javax.crypto.Cipher.init(
    at algotesting.AlgoTesting.encrypt(
    at algotesting.AlgoTesting.main(

  4. ain says:

    i used netbean ide7, but theres an arror here Base64.Encoder encoder = Base64.getEncoder();

    would you help me?thank you

Comments are closed for this "Java Symmetric AES Encryption Decryption using JCE" tutorial.