How to Encrypt a String Using AES with a Custom Key in Java?

Question

How can I encrypt a string using AES in Java with a custom key while addressing key length issues?

public static void main(String[] args) throws Exception {
    // Your code here
}

Answer

AES (Advanced Encryption Standard) is a symmetric encryption algorithm that requires specific key lengths - 128, 192, or 256 bits. Using an incorrect key length will lead to exceptions such as 'Invalid AES key length.' The following sections will guide you on how to correctly generate a valid key and perform AES encryption in Java.

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;

public class AesEncryption {
    public static void main(String[] args) throws Exception {
        String username = "[email protected]";
        String password = "Password1";
        String secretID = "BlahBlahBlah";
        String SALT2 = "deliciously salty";

        // Generate a secure key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] key = Arrays.copyOf(digest.digest((SALT2 + username + password).getBytes(StandardCharsets.UTF_8)), 16); // for AES-128

        // Generate the secret key specs.
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");

        // Instantiate the cipher
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // Optionally use CBC mode
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        byte[] encrypted = cipher.doFinal(secretID.getBytes(StandardCharsets.UTF_8));
        System.out.println("Encrypted string: " + bytesToHex(encrypted));

        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] original = cipher.doFinal(encrypted);
        String originalString = new String(original, StandardCharsets.UTF_8);
        System.out.println("Original string: " + originalString);
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}

Causes

  • The key length is not a valid size for AES. Allowed lengths are 16 bytes (128 bits), 24 bytes (192 bits), or 32 bytes (256 bits).
  • The combined length of SALT, username, and password exceeds the required key size for AES.

Solutions

  • To create a valid AES key, you can derive it from your input using a hashing algorithm like SHA-256, ensuring the byte length matches the required AES key size.
  • Use `Arrays.copyOf()` to trim the byte array to the necessary size before creating the `SecretKeySpec`.
  • It is critical to choose a secure AES mode (like CBC) and padding scheme (like PKCS5Padding).

Common Mistakes

Mistake: Not using a hashing algorithm to derive the key, leading to an invalid key size.

Solution: Use SHA-256 to ensure the key length is appropriate for AES.

Mistake: Using ECB mode may expose the data to risk of pattern analysis.

Solution: Prefer CBC mode for secure encryption.

Helpers

  • Java AES encryption
  • AES custom key
  • Java encryption example
  • AES key length
  • Java encryption tutorial

Related Questions

⦿How to Read a UTF-8 File without the BOM Marker in Java

Learn how to read a UTF8 encoded file in Java while removing the BOM marker from the output.

⦿How to Make Asynchronous HTTP Requests in Java

Learn how to implement asynchronous HTTP requests in Java allowing nonblocking execution with callbacks for improved performance.

⦿How to Calculate the Difference in Seconds Between Two Dates in Java

Learn how to calculate the difference in seconds between two dates in Java using java.time API and avoid common mistakes.

⦿Resolving the 'Access Restriction' Error When Using JavaFX in JRE 8

Learn how to fix the Access restriction error in Eclipse when working with JavaFX in Java 8 projects. Detailed solutions and tips provided.

⦿How to Decode a SHA-256 Hashed and Base64 Encoded String?

Learn how to decode a SHA256 hashed and base64 encoded string and understand key concepts surrounding hashing for better security practices.

⦿What is a Keystore and How Does it Relate to SSL?

Learn what a keystore is and its role in SSL communication. Understand the exception errors related to SSL and how to resolve them effectively.

⦿How to Manipulate the Alpha Component of a Color Int in Java for Android?

Learn how to adjust the alpha bytes of an Android color int in Java. Stepbystep guide with code snippets for transparency manipulation.

⦿How to Implement a Sorted Array List in Java While Preserving Duplicates

Discover how to create a sorted list in Java that maintains duplicates with expert insights and code examples.

⦿How to Retrieve Raw XML from SOAPMessage in Java

Learn how to extract raw XML from a SOAPMessage in Java JAXWS with a stepbystep guide and code examples.

⦿How to Validate a Date in Java: A Comprehensive Guide

Learn how to validate dates in Java. Explore methods to check for valid dates including examples and common pitfalls.

© Copyright 2025 - CodingTechRoom.com