Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Cipher: authenticated encryption with AES-GCM

Cipher provides encryption and decryption through Java cryptographic providers. AES-GCM protects data confidentiality and detects modifications; correct use requires a unique IV for each key and verification of the authentication tag.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, iv));
cipher.updateAAD(aad);
byte[] ciphertextAndTag = cipher.doFinal(plaintext);

Cipher from javax.crypto is a class-based interface to cryptographic operations implemented by a provider. Cipher.getInstance("AES/GCM/NoPadding") selects the transformation: algorithm, mode, and padding. Specify the complete name rather than relying on provider defaults.

Cipher.init sets the key, parameters, and direction: Cipher.ENCRYPT_MODE means encryption, while Cipher.DECRYPT_MODE means decryption. GCMParameterSpec(128, iv) specifies a 128-bit authentication tag and the IV. Cipher.updateAAD adds authenticated but unencrypted data; in GCM, supply it before processing the message. Decryption requires identical AAD and parameters.

Cipher.doFinal completes the operation: GCM encryption appends the tag to the ciphertext, while decryption verifies it. A mismatch causes AEADBadTagException; reject the result and do not treat data as authentic before verification finishes successfully.

Never repeat an IV when encrypting with the same key. Before another encryption, call init again with a new IV. The fixed, publicly known key and IV in the example are only for a reproducible demonstration, not production; real keys require secure generation and storage.

Examples

import java.nio.charset.StandardCharsets;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CipherDemo {
    public static void main(String[] args) throws Exception {
        // Fixed key and IV: reproducible demonstration only, never production.
        var key = new SecretKeySpec(new byte[16], "AES");
        byte[] iv = new byte[12];
        byte[] aad = "record:1".getBytes(StandardCharsets.UTF_8);
        byte[] plaintext = "Hello".getBytes(StandardCharsets.UTF_8);
        var parameters = new GCMParameterSpec(128, iv);

        Cipher encryptor = Cipher.getInstance("AES/GCM/NoPadding");
        encryptor.init(Cipher.ENCRYPT_MODE, key, parameters);
        encryptor.updateAAD(aad);
        byte[] ciphertextAndTag = encryptor.doFinal(plaintext);

        Cipher decryptor = Cipher.getInstance("AES/GCM/NoPadding");
        decryptor.init(Cipher.DECRYPT_MODE, key, parameters);
        decryptor.updateAAD(aad);
        byte[] recovered = decryptor.doFinal(ciphertextAndTag);

        System.out.println(new String(recovered, StandardCharsets.UTF_8));
        System.out.println(ciphertextAndTag.length);
    }
}

Result

Hello
21

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources