// Initialise AES cipher
BlockCipher cipher = AES.createCipher(kek, false);
// Create GCM cipher with AES
GCMBlockCipher gcm = new GCMBlockCipher(cipher);
AEADParameters aeadParams = new AEADParameters(new KeyParameter(kek.getEncoded()),
AUTH_TAG_BIT_LENGTH,
iv,
null);
gcm.init(false, aeadParams);
byte[] cipherText = authEncrCEK.getCipherText();
byte[] authTag = authEncrCEK.getAuthenticationTag();
// Join encrypted CEK and authentication tag to produce cipher input
byte[] input = new byte[cipherText.length + authTag.length];
System.arraycopy(cipherText, 0, input, 0, cipherText.length);
System.arraycopy(authTag, 0, input, cipherText.length, authTag.length);
int keyBytesLength = gcm.getOutputSize(input.length);
byte[] keyBytes = new byte[keyBytesLength];
// Decrypt
int keyBytesOffset = gcm.processBytes(input, 0, input.length, keyBytes, 0);
// Validate authentication tag
try {
keyBytesOffset += gcm.doFinal(keyBytes, keyBytesOffset);
} catch (InvalidCipherTextException e) {
throw new JOSEException("Couldn't validate GCM authentication tag: " + e.getMessage(), e);
}