AES cipher transformation without an explicit mode and padding

Specify the mode and padding when configuring AES encryption

Description

In Java and Kotlin, Cipher.getInstance("AES") leaves the mode and padding to the JCA provider. Oracle JDK 25’s SunJCE interprets it as AES/ECB/PKCS5Padding. SunPKCS11 also defaults to ECB and PKCS5Padding for several symmetric ciphers. Both languages use javax.crypto.Cipher.

Potential impact

ECB exposes repeated plaintext blocks and cannot detect modification. A change in provider defaults may also make existing ciphertext unreadable.

Remediation

Specify the algorithm, mode and padding. Use AES/GCM/NoPadding when you need confidentiality and integrity. Use a unique 96-bit nonce for each encryption under the same key. The nonce can be stored or sent with the ciphertext for decryption. Plan separately for migrating existing ECB data.

Examples

Before

java
import javax.crypto.Cipher;

Cipher cipher = Cipher.getInstance("AES");

After

key is an AES SecretKey generated and managed securely elsewhere. This excerpt initializes the cipher; ciphertext storage and decryption error handling are omitted.

java
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;

byte[] nonce = new byte[12];
new SecureRandom().nextBytes(nonce);

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, nonce));

The example initializes GCM with 12 random bytes from SecureRandom and a 128-bit authentication tag.

References