What is Java AES Encryption and Decryption?


Java AES (Advanced Encryption Standard) Encryption and Decryption is a method of encrypting and decrypting data using the AES algorithm in Java programming language. AES is a symmetric encryption algorithm that uses a secret key to encrypt and decrypt data. It is widely used for securing sensitive data such as passwords, credit card numbers, and other confidential information.

There are several methods to perform AES encryption and decryption in Java:

  • Using Java Cryptography Extension (JCE) API: The JCE API provides a set of classes and interfaces for cryptographic operations in Java. To perform AES encryption and decryption using JCE API, we need to follow these steps:

  • Generate a secret key using a key generator.

  • Create a cipher object and initialize it with the secret key.
  • Encrypt or decrypt the data using the cipher object.

Here is an example code for AES encryption and decryption using JCE API:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

public class AESEncryptionDecryption {

    public static void main(String[] args) throws Exception {

        String plainText = "This is a secret message";
        System.out.println("Plain Text: " + plainText);

        // Generate a secret key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();

        // Create a cipher object and initialize it with the secret key
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // Encrypt the data
        byte[] encryptedData = cipher.doFinal(plainText.getBytes());
        System.out.println("Encrypted Text: " + Base64.getEncoder().encodeToString(encryptedData));

        // Initialize the cipher object for decryption
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        // Decrypt the data
        byte[] decryptedData = cipher.doFinal(encryptedData);
        System.out.println("Decrypted Text: " + new String(decryptedData));
    }
}
  • Using Bouncy Castle API: Bouncy Castle is a popular open-source cryptographic library for Java. It provides a wide range of cryptographic algorithms including AES. To perform AES encryption and decryption using Bouncy Castle API, we need to follow these steps:

  • Generate a secret key using a key generator.

  • Create a cipher object and initialize it with the secret key.
  • Encrypt or decrypt the data using the cipher object.

Here is an example code for AES encryption and decryption using Bouncy Castle API:

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.Security;
import java.util.Base64;

public class AESEncryptionDecryption {

    public static void main(String[] args) throws Exception {

        Security.addProvider(new BouncyCastleProvider());

        String plainText = "This is a secret message";
        System.out.println("Plain Text: " + plainText);

        // Generate a secret key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();

        // Create a cipher object and initialize it with the secret key
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // Encrypt the data
        byte[] encryptedData = cipher.doFinal(plainText.getBytes());
        System.out.println("Encrypted Text: " + Base64.getEncoder().encodeToString(encryptedData));

        // Initialize the cipher object for decryption
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        // Decrypt the data
        byte[] decryptedData = cipher.doFinal(encryptedData);
        System.out.println("Decrypted Text: " + new String(decryptedData));
    }
}

In both examples, we have used ECB (Electronic Codebook) mode for encryption and PKCS7Padding for padding the data. However, it is recommended to use other modes such as CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode) for better security.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.