Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

GCMParameterSpec — GCM mode parameters

GCMParameterSpec supplies Cipher with an initialization vector and authentication tag length. The object itself neither encrypts data nor ensures IV uniqueness.

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

Syntax

public GCMParameterSpec(int tLen, byte[] src)
public GCMParameterSpec(int tLen, byte[] src, int offset, int len)
int getTLen()
byte[] getIV()

GCMParameterSpec in javax.crypto.spec describes parameters for a GCM Cipher. tLen specifies the authentication tag length in bits, not bytes; it is not the key length.

A constructor accepts the entire IV or a portion of src selected by offset and len in bytes. It copies the input; getIV() also returns a fresh copy. getTLen() returns the tag length.

Negative tLen, null, or invalid slice bounds cause IllegalArgumentException. Successful construction does not guarantee that a cryptographic provider supports the selected tag length.

For AES/GCM/NoPadding, pass these parameters to Cipher.init(...). Never repeat an IV when encrypting with the same key: this enables forgery attacks. The example demonstrates array copying only, not secure IV generation.

Examples

import javax.crypto.spec.GCMParameterSpec;

public class Main {
    public static void main(String[] args) {
        byte[] iv = new byte[12];
        iv[0] = 7;
        GCMParameterSpec spec = new GCMParameterSpec(128, iv);

        iv[0] = 9;
        byte[] copy = spec.getIV();
        copy[0] = 11;

        System.out.println(spec.getTLen());
        System.out.println(spec.getIV().length);
        System.out.println(spec.getIV()[0]);
    }
}

Result

128
12
7

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