AES 256 in Java

Sheeraz Gul Oct 12, 2023
  1. AES 256 in Java
  2. Use AES 256 to Encrypt Data in Java
  3. Use AES 256 to Decrypt Data in Java
AES 256 in Java

The AES 256 is an encryption and decryption algorithm. This tutorial demonstrates how to implement AES 256 in Java to encrypt and decrypt the data.

AES 256 in Java

The AES is a symmetric encryption algorithm that is easy to implement in software, hardware, and restricted environments. It also provides a good defense against various attacks.

The AES can handle 128-bit blocks in the form of a block cipher using the keys of sizes 128, 192 and 256 bits. Each cipher will decrypt or encrypt data in blocks of 128 bits.

The AES uses the same encryption and decryption process in which the receiver and sender both must use and know the same secret keys. This tutorial demonstrates how to encrypt and decrypt data using AES 256.

Use AES 256 to Encrypt Data in Java

The Java code below implements the AES 256 encryption.

package delftstack;

import java.nio.charset.StandardCharsets;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AES_Encrypt {
  private static final String SECRET_KEY = "Delftstack";
  private static final String SALT = "Tutorial";

  public static String encrypt_AES(String StringToEncrypt) {
    try {
      byte[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
      IvParameterSpec Iv_Specifications = new IvParameterSpec(a);

      SecretKeyFactory Secret_Key_Factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
      KeySpec Key_Spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 65536, 256);
      SecretKey Temp_Secret_Key = Secret_Key_Factory.generateSecret(Key_Spec);
      SecretKeySpec Secret_Key = new SecretKeySpec(Temp_Secret_Key.getEncoded(), "AES");

      Cipher AES_Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      AES_Cipher.init(Cipher.ENCRYPT_MODE, Secret_Key, Iv_Specifications);
      return Base64.getEncoder().encodeToString(
          AES_Cipher.doFinal(StringToEncrypt.getBytes(StandardCharsets.UTF_8)));
    } catch (Exception e) {
      System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
  }

  public static void main(String[] args) {
    String Original_String = "www.delftstack.com";

    String Encrypted_String = AES_Encrypt.encrypt_AES(Original_String);

    System.out.println(Original_String);
    System.out.println(Encrypted_String);
  }
}

The output for the code above is:

www.delftstack.com
fV2B4Is2xva8C+SieUg01vRbGmZNbyTzV9+llBdv7kM=

Use AES 256 to Decrypt Data in Java

The code below demonstrates the AES 256 decryption in Java.

package delftstack;

import java.nio.charset.StandardCharsets;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AES_Decrypt {
  private static final String SECRET_KEY = "Delftstack";
  private static final String SALT = "Tutorial";

  public static String decrypt_AES(String strToDecrypt) {
    try {
      byte[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
      IvParameterSpec IV_Specifications = new IvParameterSpec(a);

      SecretKeyFactory Secret_Key_Factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
      KeySpec Key_Spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 65536, 256);
      SecretKey Temp_Secret_Key = Secret_Key_Factory.generateSecret(Key_Spec);
      SecretKeySpec Secret_Key = new SecretKeySpec(Temp_Secret_Key.getEncoded(), "AES");

      Cipher AES_Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
      AES_Cipher.init(Cipher.DECRYPT_MODE, Secret_Key, IV_Specifications);
      return new String(AES_Cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    } catch (Exception e) {
      System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
  }
  public static void main(String[] args) {
    String Encrypted_String = "fV2B4Is2xva8C+SieUg01vRbGmZNbyTzV9+llBdv7kM=";

    String Decrypted_String = AES_Decrypt.decrypt_AES(Encrypted_String);

    System.out.println(Encrypted_String);
    System.out.println(Decrypted_String);
  }
}

The code above will decrypt an encrypted string using AES 256. See output:

fV2B4Is2xva8C+SieUg01vRbGmZNbyTzV9+llBdv7kM=
www.delftstack.com
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Encryption